javascript - 如何从图像 Canvas 中裁剪颜色部分?

标签 javascript jquery angularjs ionic-framework html5-canvas

我正在研究基于 ionic 的应用程序。

我想要像用户用手指在图像( Canvas )上填充红色的功能。所以我已经完成了填充功能,但我想从 Canvas 上裁剪填充部分。我附上了一张图片以供引用。

enter image description here

我想裁剪上图中的红色部分。我用谷歌搜索但没有找到任何解决方案。

最佳答案

创建图像 mask 。

如果您正在渲染选择区域(红色),那么解决方案很简单。

创建与绘图大小相同的第二个 Canvas ,但不要将其添加到 DOM。将红色标记内容绘制到该 Canvas 上

在显示 Canvas 上首先渲染图像然后渲染那个标记 使用“叠加”等合成模式在图像上 Canvas ,以便可以看到原始图像并且标记区域为红色。

现在您有两层,一层是图像,另一层是蒙版,您可以使用它来获取标记内容的副本。

为此创建第三个 Canvas ,将原始图像绘制到其上,然后将合成模式设置为“destination-in”。然后在上面画上面具。只有标记的像素会保留。

查看示例以获取更多详细信息

setTimeout(example,0); // ensures that the run us after parsing
function example(){
  const ctx = canvas.getContext("2d");
  var w = canvas.width;
  var h = canvas.height;
  var cw = w / 2;  // center 
  var ch = h / 2;

  var selectLayer = CImageCtx(w,h); // creates a canvas 
  var selectedContent = CImageCtx(w,h); // the selected content
  document.body.appendChild(selectedContent);
  var image = new Image;  // the image
  image.src = " /image/QhFct.png";
  // updates the masked result
  function updateSelected(){
    var ctx = selectedContent.ctx;
    ctx.drawImage(image,0,0);
    ctx.globalCompositeOperation = "destination-in";
    ctx.drawImage(selectLayer,0,0);
    ctx.globalCompositeOperation = "source-over";
  }
  function update(){
      // if mouse down then 
      if(mouse.but){
        // clear the mask if on the right image
        if(mouse.oldBut === false && mouse.x > 256){
           selectLayer.ctx.clearRect(0,0,w,h);
           mouse.but = false;
        }else{
           // draw the red 
           selectLayer.ctx.fillStyle = "red";
           fillCircle(mouse.x, mouse.y, 20, selectLayer.ctx);
        }
        // update the masked result
        updateSelected();
      }

      // clear the canvas
      ctx.clearRect(0,0,w,h);
      // draw the image
      ctx.drawImage(image,0,0);
      // then draw the marking layer over it with comp overlay
      ctx.globalCompositeOperation = "overlay";
      ctx.drawImage(selectLayer,0,0);
      ctx.globalCompositeOperation = "source-over";

      mouse.oldBut = mouse.but;
      requestAnimationFrame(update);
  }
  requestAnimationFrame(update);
}






//#############################################################################
// helper functions not part of the answer
//#############################################################################
const mouse  = {
  x : 0, y : 0, but : false,
  events(e){
    const m = mouse;
    const bounds = canvas.getBoundingClientRect();
    m.x = e.pageX - bounds.left - scrollX;
    m.y = e.pageY - bounds.top - scrollY;
    m.but = e.type === "mousedown" ? true : e.type === "mouseup" ? false : m.but;
  }
};
(["down","up","move"]).forEach(name => document.addEventListener("mouse" + name,mouse.events));
const CImage = (w = 128, h = w) => (c = document.createElement("canvas"),c.width = w,c.height = h, c);
const CImageCtx = (w = 128, h = w) => (c = CImage(w,h), c.ctx = c.getContext("2d"), c);
const fillCircle = (l,y=ctx,r=ctx,c=ctx) =>{if(l.p1){c=y; r=leng(l);y=l.p1.y;l=l.p1.x }else if(l.x){c=r;r=y;y=l.y;l=l.x}c.beginPath(); c.arc(l,y,r,0,Math.PI*2); c.fill()}
body { font-family : arial; }
canvas { border : 2px solid black; }
Draw on image and the selected parts are shown on the right<br>
Click right image to reset selection<br>
<canvas id="canvas" width=256 height=256></canvas>

已经屏蔽了。

如果红色蒙版已应用于图像,那么除了根据图像的红色程度执行阈值过滤器外,您无能为力。但即便如此,您也会遇到较暗区域和已经包含红色的区域的问题。

除非你有原始图像,否则你的结果会很差。

如果您有原始图像,则必须访问图像数据并通过比较每个像素并仅选择不同的像素来创建新图像作为蒙版。这将迫使您仅使用相同的域图像(或使用 CORS 交叉源 header )

关于javascript - 如何从图像 Canvas 中裁剪颜色部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47409950/

相关文章:

javascript - 插入 HTML 元素的节点

javascript - 子组件调用具有http请求的父函数后如何更新子组件中父组件的变量

javascript - 如何等待AJAX​​请求的结果?

javascript - 在另一个模块中的指令内引用的模块中定义的 Controller

javascript - $window.open 仍在phonegap应用程序中打开url( Angular )

angularjs - Ionic - 以编程方式禁用单个 ionic 项目的滑动

javascript - jQuery:将相同的事件绑定(bind)到窗口和另一个元素

javascript - d3.js:如何获取拖动的事件信息

jquery - 使用 JQuery 检查父部分中是否存在具有特定数据属性的子 div

javascript - 从区域中取消附加 JQuery 拖放项而不删除或隐藏它