简体   繁体   English

如何在HTML5画布中以alpha = 0进行绘制以“擦除”

[英]How do you draw with alpha = 0 in an html5 canvas to 'erase'

How do you draw with alpha = 0 to an HTML5 Canvas? 如何以alpha = 0绘制到HTML5画布? Imagine I'm making a photoshop clone, I have a layer that's solid red. 想象一下我正在制作一个Photoshop克隆,我有一个纯红色的图层。 I pick the eraser tool and draw with. 我选择橡皮擦工具并进行绘制。 It draws in rgba(0,0,0,0) letting me see through to the background. 它绘制了rgba(0,0,0,0)让我看透了背景。 How do I do this in HTML5 Canvas? 如何在HTML5 Canvas中做到这一点?

Here's some code. 这是一些代码。

 var rand = function(v) { return Math.random() * v; }; var canvas = document.getElementsByTagName("canvas")[0]; var ctx = canvas.getContext("2d"); // fill the canvas with black ctx.fillStyle = "red"; ctx.fillRect(0, 0, canvas.width, canvas.height); // Erase some circles (draw them in 0,0,0,0); ctx.fillStyle = "rgba(0,0,0,0)"; ctx.globalCompositeOperation = "copy"; for (var ii = 0; ii < 5; ++ii) { ctx.beginPath(); ctx.arc(rand(canvas.width), rand(canvas.height), rand(50) + 20, 0, 360, false); ctx.fill(); } /* source-over source-in source-out source-atop destination-over destination-in destination-out destination-atop lighter darker copy xor */ 
 canvas { margin: 10px; border: 1px solid black; background-color: yellow; } 
 <div>Want red with yellow circles</div> <canvas></canvas> 

This doesn't work. 这行不通。 All canvas operations are considered to be infinitely large which means drawing each circle (arc) with globalCompositeOperation set to "copy" effectively erases everything outside of each circle. 所有画布操作都被认为是无限大的,这意味着在将globalCompositeOperation设置为“复制”的情况下绘制每个圆(弧)会有效擦除每个圆之外的所有内容。

I might be able to setup clipping to match the circle but ideally I'd like to be able to erase with an anti-aliased circle, same as a photoshop brush. 我也许可以设置裁剪以匹配圆,但是理想情况下,我希望能够使用抗锯齿的圆来擦除,就像Photoshop笔刷一样。

You'll want to use: 您将要使用:

ctx.fillStyle = "rgba(0,0,0,1)"; // (Drawing with 0 alpha pretty much means doing nothing)
ctx.globalCompositeOperation = "destination-out";

Working Example 工作实例

Keep in mind to save the previous globalCompositeOperation and restore it, or transparency won't work properly, later on. 请记住,稍后保存以前的globalCompositeOperation并将其还原,否则透明性将无法正常工作。

The problem is that "Drawing with alpha=0 on a canvas just overlays a invisible layer of "ink", by default. 问题是,默认情况下,“在画布上以alpha=0进行绘制只会覆盖不可见的“墨水”层。

If you have to erase fluently, so when the mouse was clicked and moved this line should be erased, this might be a solution: 如果您必须流畅地擦除,那么当单击并移动鼠标时,应该擦除此行,这可能是一种解决方案:

var canvas = document.getElementById("myCanvas");
var eraseWidth = 5;

$("#myCanvas").mousedown(function(canvas){          //the mousedown (writing) handler, this handler does not draw, it detects if the mouse is down (see mousemove)
    x = canvas.pageX-this.offsetLeft;
    y = canvas.pageY-this.offsetTop;
});

$("#myCanvas").mousemove(function(canvas){
    context.beginPath();
    var x2 = x-(eraseWidth/2);          //x2 is used to center the erased rectangle at the mouse point
    var y2 = y-(eraseWidth/2);          //y2 is used to center the erased rectangle at the mouse point
    context.clearRect(x2, y2, eraseWidth, eraseWidth);              //clear the rectangle at the mouse point (x2 and y2)
    context.closePath();
};

basically what this does is clear a rectangle when the mouse is moved, everytime the mousehandler sends a mousemove event and uses the x and y coordinates for the center of the canvas to clear the recangle. 基本上,这是在移动鼠标时清除矩形,每次鼠标处理程序发送mousemove事件并使用x和y坐标作为画布的中心来清除矩形。 the result is a cleared (erased) line. 结果是清除(擦除)行。

ok, you can see the rectangles if you move too fast, but my project was a concept, so it did the trick for me ;) 好的,如果移动得太快,您可以看到矩形,但是我的项目是一个概念,所以对我来说很成功;)

If you're working on something akin to a photoshop clone, then it's probably best for you to create a canvas for each layer. 如果您正在从事类似于Photoshop克隆的工作,那么最好为每个图层创建一个画布。 I think that would greatly simplify everything for you, while giving you better performance in return. 我认为这将大大简化您的工作,同时为您提供更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM