如何从图像画布中裁剪颜色部分?

戈瓦迪约

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

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

在此处输入图片说明

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

盲人67

创建图像蒙版。

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

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

The on the display canvas render the image first and then render that marking canvas over the image with composite mode like "overlay" so that the original image can be seen and the marked areas are red.

Now you have two layers, one is the image and the other the mask you can use to get a copy of the marked content.

To do that create a 3rd canvas, draw the original image onto it, then set the composite mode to "destination-in". Then draw the mask over it. Only the marked pixels will remain.

See the example for more details

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 = " https://i.stack.imgur.com/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>

Already masked.

If the red mask is already applied to the image then there is not much you can do apart from do a threshold filter depending on how red the image is. But even then you are going to have problems with darker areas, and areas that already contain red.

除非您拥有原始图像,否则效果会很差。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在画布中对角裁剪图像

来自分类Dev

裁剪画布中显示的图像

来自分类Dev

画布缩放时如何裁剪图像

来自分类Dev

如何在iOS中裁剪大图像的中心部分?

来自分类Dev

如何比较图像上的颜色并裁剪差异?

来自分类Dev

如何从保存的base64图像中裁剪未使用的画布空间?

来自分类Dev

在 NiFi 中如何裁剪图像?

来自分类Dev

如何调整大小然后使用画布裁剪图像

来自分类Dev

如何按路径而不是区域裁剪画布图像

来自分类Dev

使用JCrop裁剪图像绘制到画布中

来自分类Dev

使用JCrop裁剪绘制到画布中的图像

来自分类Dev

如何裁剪libgdx中重叠图像的一部分?

来自分类Dev

如何使用 angular 2 和 typescript 裁剪图像、调整裁剪框的大小并将其保存在另一个画布中

来自分类Dev

如何缩放画布中的图像?

来自分类Dev

Tkinter画布无法显示整个图像,一小部分被裁剪掉

来自分类Dev

如何从图像中裁剪出最大的矩形

来自分类Dev

如何从图像中裁剪边框/空白?

来自分类Dev

如何裁剪iOS中圆圈内的图像

来自分类Dev

如何在Titanium中以圆形裁剪图像?

来自分类Dev

如何在Android中裁剪图像?

来自分类Dev

如何动态裁剪图像中的数字?

来自分类Dev

如何在Pygame中“裁剪”旋转图像

来自分类Dev

如何从图像中裁剪边框/空白?

来自分类Dev

如何在png图像中裁剪空白?

来自分类Dev

HTML-Bootstrap-如何裁剪图像的中心部分

来自分类Dev

在OpenCV中清除(不裁剪)图像的非ROI部分

来自分类Dev

如何从整个图像的缓存像素数据中获取画布ImageData的矩形部分?

来自分类Dev

如何计算颜色像素数或找出图像(js)中的颜色部分?

来自分类Dev

如何在 tkinter 的画布中反转颜色?

Related 相关文章

热门标签

归档