2010-09-24 123 views
9

有一种实现橡皮擦的方法(除了使用白色铅笔?)。HTML5帆布橡皮擦

我正在使用分层,在画布下方有一个图像,所以,如果橡皮擦涂成白色,用户会注意到,因为下面的图像不是纯白色的。

回答

0

设置铅笔颜色到透明:

context.fillStyle = "rgba(255,0,0,0)"; 

rgba()格式,最后一个是α通道,0是完全透明的

+1

它没有工作。 “橡皮擦铅笔”剧照以红色(实际绘图铅笔的颜色)绘制。 – Valentina 2010-09-24 19:38:07

+0

尝试将颜色设置为“透明”(http://dev.w3.org/csswg/css3-color/#transparent),或者'strokestyle'而不是'fillstyle' – Javier 2010-09-24 19:43:26

+0

=(没有,我已经试过了context.strokeStyle =“rgba(0,0,0,0)”; and context.fillStyle =“rgba(0,0,0,0)”;。任何想法? – Valentina 2010-09-24 19:46:46

16

基本上,将globalCompositeOperation设置为copy,并使用不透明度为0的颜色(例如, "rgba(0,0,0,0)"你正在尝试。

的问题是回答在这里更详细一点:

"Erasing" in html5 canvas

3
function eraser() 
{         
context.strokeStyle = "rgb(255, 255, 255)"; 
context.globalCompositeOperation = "copy"; 
context.strokeStyle = ("rgba(255,255,255,255)"); /* or */ context.fillStyle = "rgba(255,0,0,0)"; 
} 

无论是在我的情况下工作!

0

代码用于使用globalCompositeOperation“目的地-out”和“源极 - 之上”

var handleMouseMove = function (event) { 
    midPt = new createjs.Point(oldPt.x + stage.mouseX>>1, oldPt.y+stage.mouseY>>1); 

    if(curTool.type=="eraser"){ 

     var tempcanvas = document.getElementById('drawcanvas'); 
     var tempctx=tempcanvas.getContext("2d"); 
     tempctx.beginPath(); 
     tempctx.globalCompositeOperation = "destination-out"; 
     tempctx.arc(midPt.x, midPt.y, 20, 0, Math.PI * 2, false);  
     tempctx.fill(); 
     tempctx.closePath(); 
     tempctx.globalCompositeOperation = "source-over"; 
     drawingCanvas.graphics.clear(); 

     // keep updating the array for points 
     arrMidPtx.push(midPt.x); 
     arrMidPty.push(midPt.y); 
     stage.addChild(drawingCanvas); 
     stage.update(); 

    } 

透明橡皮擦我已经使用easeljs这里但是代码是独立于它,并且可以与任何画布HTML5绘制的javascript被集成代码

+0

请问您能否进一步解释代码 – utdev 2017-04-27 19:49:30