SVG Фильтры и пиксели холста - PullRequest
0 голосов
/ 25 марта 2020

Я пытаюсь получить пиксели холста, к которым были применены фильтры.

В этом примере ниже я применяю фильтр к mycanvas1, рисую на нем изображение, а затем рисую этот холст на другом холсте mycanvas2:

async function loadImageFromBase64(base64) {
  return new Promise(resolve => {
    const image = new Image();
    image.onload = () => resolve(image);
    image.src = base64;
  });
}

async function main() {
  const image = await(loadImageFromBase64(imageBase64));
  const mycanvas1 = document.getElementById('mycanvas1');
  const myctx1 = mycanvas1.getContext('2d');
  const mycanvas2 = document.getElementById('mycanvas2');
  const myctx2 = mycanvas2.getContext('2d');
  
  myctx1.drawImage(image, 0, 0);
  myctx2.drawImage(mycanvas1, 0, 0);
}

window.onload = main;

const imageBase64 = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pg0KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDE1LjAuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPg0KPCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj4NCjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiDQoJIHdpZHRoPSIxMjhweCIgaGVpZ2h0PSIxMjhweCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDEyOCAxMjg7IiB4bWw6c3BhY2U9InByZXNlcnZlIj4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Q0NDQ0NDOyIgd2lkdGg9IjI2IiBoZWlnaHQ9IjEiLz4NCjwvZz4NCjwvc3ZnPg0K';
<!DOCTYPE html>
<html>
<head>
  <title>My Filter</title>
  <meta charset="utf-8">
  <style>
    #mycanvas1 {
      filter: url(#myfilter);
      border: 3px solid blue;
    }

    #mycanvas2 {
      border: 3px solid red;
    }
</style>
</head>
<body>
  <canvas id="mycanvas1"></canvas>
  <canvas id="mycanvas2"></canvas>

  <svg>
    <filter id="myfilter" x="0" y="0" width="100%" height="100%">
      <feColorMatrix type="luminanceToAlpha"/>
    </filter>
  </svg></body>
</html>

Но фильтр, похоже, не применяется к пикселям холста. Они постобработаны, поэтому второй холст содержит изображение без фильтра. Есть ли способ получить пиксели холста с примененным фильтром?

Мне это нужно, потому что мне нужно выполнить некоторые операции после фильтра в javascript.

...