javascript - 重新填充 Canvas 删除区域的区域

标签 javascript jquery html canvas globalcompositeoperation

我正在尝试使用下面的代码片段填充图像中的颜色,以在 Canvas 图像上填充颜色。它成功地在 Canvas 上填充颜色。现在,我尝试使用此代码片段删除用户触摸时的填充颜色,以删除 Canvas 图像上的颜色。它删除颜色并在触摸的位置设置透明区域。现在我想在用户触摸时用颜色重新填充该区域,但由于透明像素,它不允许我在该区域上着色。那么有没有什么方法可以用颜色重新填充像素或者有没有其他方法可以从 Canvas 图像中删除颜色?任何答复将不胜感激。

在 Canvas 图像上填充颜色的代码片段

var ctx = canvas.getContext('2d'),
    img = new Image;
img.onload = draw;
img.crossOrigin = 'anonymous';
img.src = "https://dl.dropboxusercontent.com/s/1alt1303g9zpemd/UFBxY.png";

    function draw(color) {
      ctx.drawImage(img, 0, 0);
    }
    canvas.onclick = function(e){
       var rect = canvas.getBoundingClientRect();
       var x = e.clientX-rect.left,
           y = e.clientY-rect.top;

        ctx.globalCompositeOperation = 'source-atop';
        ctx.fillStyle = 'blue';
        ctx.beginPath();
        ctx.arc(x-5,y-5,10,0,2*Math.PI);
        ctx.fill();

      }

用于删除 Canvas 图像上的颜色的代码片段

(function() {
    // Creates a new canvas element and appends it as a child
    // to the parent element, and returns the reference to
    // the newly created canvas element


    function createCanvas(parent, width, height) {
        var canvas = {};
        canvas.node = document.createElement('canvas');
        canvas.context = canvas.node.getContext('2d');
        canvas.node.width = width || 100;
        canvas.node.height = height || 100;
        parent.appendChild(canvas.node);
        return canvas;
    }

    function init(container, width, height, fillColor) {
        var canvas = createCanvas(container, width, height);
        var ctx = canvas.context;
        // define a custom fillCircle method
        ctx.fillCircle = function(x, y, radius, fillColor) {
            this.fillStyle = fillColor;
            this.beginPath();
            this.moveTo(x, y);
            this.arc(x, y, radius, 0, Math.PI * 2, false);
            this.fill();
        };
        ctx.clearTo = function(fillColor) {
            ctx.fillStyle = fillColor;
            ctx.fillRect(0, 0, width, height);
        };
        ctx.clearTo(fillColor || "#ddd");

        // bind mouse events
        canvas.node.onmousemove = function(e) {
            if (!canvas.isDrawing) {
               return;
            }
            var x = e.pageX - this.offsetLeft;
            var y = e.pageY - this.offsetTop;
            var radius = 10; // or whatever
            var fillColor = '#ff0000';
            ctx.globalCompositeOperation = 'destination-out';
            ctx.fillCircle(x, y, radius, fillColor);
        };
        canvas.node.onmousedown = function(e) {
            canvas.isDrawing = true;
        };
        canvas.node.onmouseup = function(e) {
            canvas.isDrawing = false;
        };
    }

    var container = document.getElementById('canvas');
    init(container, 531, 438, '#ddd');

})();

最佳答案

警告未经测试的代码!

// create a clipping region using your erasing rect's x,y,width,height
context.save();
context.beginPath();
context.rect(erasingRectX,erasingRectY,erasingRectWidth,erasingRectHeight);
context.clip();

// redraw the original image.
// the image will be redrawn only into the erasing rects boundary
context.drawImage(yourImage,0,0);

// compositing: new pixels draw only where overlapping existing pixels
context.globalCompositeOperation='source-in';

// fill with your new color
// only the existing (clipped redrawn image) pixels will be colored
context.fillStyle='red';
context.fillRect(0,0,canvas.width,canvas.height);

// undo the clipping region
context.restore();

关于javascript - 重新填充 Canvas 删除区域的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32861219/

相关文章:

javascript - 单击其中之一后如何交换 href 属性的位置?

javascript - 如何将普通 JS 中的二叉树绘制为 HTML+CSS?

javascript - 网页如何防止粘贴事件?

javascript - 如何使用表格排序插件-Jquery 搜索表格中的项目

jquery - 如何捕获json格式的特定元素

javascript - 无法理解为什么我的 Javascript 函数不起作用?

javascript - 使用变量 url 创建 href

javascript - jQuery Select2 没有在任何地方关闭(也不在他们自己的网站上)

javascript - 我可以使用 ES6/2015 模块导入在 'global' 范围内设置引用吗?

在 Div 中发布的 Javascript