简体   繁体   English

HTML5 canvas globalAlpha无法正常工作

[英]HTML5 canvas globalAlpha not working

I need create smooth drawing lines with transparent without clearRect method i try use: globalAlpha and strokeStyle with rgba like this: 我需要使用没有clearRect方法的透明对象来创建光滑的绘图线,并尝试使用: globalAlphastrokeStyle与rgba像这样:

ctx.strokeStyle = "rgba( redChannel, greenChannel, blueChannel, AlphaChannel )"; ctx.strokeStyle = "rgba( redChannel, greenChannel, blueChannel, AlphaChannel )";

But did not work both methods. 但是两种方法都没有用。 How i can drawing transparent lines without clearRect method. 我如何可以在没有clearRect方法的情况下绘制透明线。 While i use clearRect before each drawing globalAlpha works, but i need that working without it. 虽然我在每次绘制globalAlpha之前都使用clearRect ,但是我需要在没有它的情况下进行工作。

my code example: 我的代码示例:

 var el = document.getElementById('c'); var ctx = el.getContext('2d'); ctx.lineWidth = 10; ctx.lineJoin = ctx.lineCap = 'round'; ctx.globalAlpha = "0.2"; //ctx.strokeStyle = "rgba(255, 0, 0, 150)"; ctx.strokeStyle = "red"; var isDrawing, points = [ ]; el.onmousedown = function(e) { isDrawing = true; points.push({ x: e.clientX, y: e.clientY }); }; el.onmousemove = function(e) { if (!isDrawing) return; //ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); points.push({ x: e.clientX, y: e.clientY }); ctx.beginPath(); ctx.moveTo(points[0].x, points[0].y); for (var i = 1; i < points.length; i++) { ctx.lineTo(points[i].x, points[i].y); } ctx.stroke(); ctx.closePath(); }; el.onmouseup = function() { isDrawing = false; points.length = 0; }; 
 canvas { border: 1px solid #ccc } 
 <canvas id="c" width="500" height="300"></canvas> 

You are redrawing the entire path every time the mouse moves, so even though the globalAlpha value is set to 0.2 the layering effect makes the line appear solid. 每次鼠标移动时,您都将重绘整个路径,因此,即使将globalAlpha值设置为0.2,分层效果也会使线条看起来很坚实。

Option 1: use clearRect to clear the path onmousemove then redraw; 选项1:使用clearRect清除onmousemove路径,然后重绘;

Option 2: draw only the last segment of the path, but overlaps are visible: 选项2:仅绘制路径的最后一段,但是可以看到重叠:

 var el = document.getElementById('c'); var ctx = el.getContext('2d'); ctx.lineWidth = 10; ctx.lineJoin = ctx.lineCap = 'round'; ctx.globalAlpha = "0.2"; //ctx.strokeStyle = "rgba(255, 0, 0, 150)"; ctx.strokeStyle = "red"; var isDrawing, points = [ ]; el.onmousedown = function(e) { isDrawing = true; points.push({ x: e.clientX, y: e.clientY }); }; el.onmousemove = function(e) { if (!isDrawing) return; //ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); points.push({ x: e.clientX, y: e.clientY }); ctx.beginPath(); //draw just the last segment if(points.length>1) { ctx.moveTo(points[points.length-2].x, points[points.length-2].y); ctx.lineTo(points[points.length-1].x, points[points.length-1].y); } ctx.stroke(); ctx.closePath(); }; el.onmouseup = function() { isDrawing = false; points.length = 0; }; 
 canvas { border: 1px solid #ccc } 
 <canvas id="c" width="500" height="300"></canvas> 

Option 3: set the opacity of the canvas element 选项3:设置canvas元素的不透明度

 var el = document.getElementById('c'); var ctx = el.getContext('2d'); ctx.lineWidth = 10; ctx.lineJoin = ctx.lineCap = 'round'; //ctx.globalAlpha = "0.2"; //ctx.strokeStyle = "rgba(255, 0, 0, 150)"; ctx.strokeStyle = "red"; var isDrawing, points = [ ]; el.onmousedown = function(e) { isDrawing = true; points.push({ x: e.clientX, y: e.clientY }); }; el.onmousemove = function(e) { if (!isDrawing) return; //ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); points.push({ x: e.clientX, y: e.clientY }); ctx.beginPath(); ctx.moveTo(points[0].x, points[0].y); for (var i = 1; i < points.length; i++) { ctx.lineTo(points[i].x, points[i].y); } ctx.stroke(); ctx.closePath(); }; el.onmouseup = function() { isDrawing = false; points.length = 0; }; 
 canvas { border: 1px solid #ccc; opacity:0.2; } 
 <canvas id="c" width="500" height="300"></canvas> 

Option 3 : use two canvas, on main - draw only on mouse release, and on second preview canvas, just show progress, so drawing will be visible even on mouse move. 选项3 :在主画布上使用两个画布-仅在释放鼠标时绘制,而在第二个预览画布上,仅显示进度,因此即使在鼠标移动时绘图也将可见。 Also for preview canvas we need to do some tricks like clean previous line segment, so everything looks good. 另外,对于预览画布,我们需要做一些技巧,例如清理先前的线段,因此一切看起来都不错。

 //main canvas var el = document.getElementById('c'); var ctx = el.getContext('2d'); //preview var el2 = document.getElementById('c2'); var ctx2 = el2.getContext('2d'); ctx.lineWidth = 10; ctx2.lineWidth = 10; ctx.lineJoin = ctx.lineCap = 'round'; ctx2.lineJoin = ctx2.lineCap = 'round'; ctx.strokeStyle = "rgba(255, 0, 0, 0.5)"; ctx2.strokeStyle = "rgba(255, 0, 0, 0.5)"; var isDrawing, points = [ ]; el2.onmousedown = function(e) { isDrawing = true; points.push({ x: e.clientX, y: e.clientY }); ctx.beginPath(); ctx2.beginPath(); }; el2.onmousemove = function(e) { if (!isDrawing) return; points.push({ x: e.clientX, y: e.clientY }); //draw just the last segment if(points.length>1) { ctx.moveTo(points[points.length-2].x, points[points.length-2].y); ctx.lineTo(points[points.length-1].x, points[points.length-1].y); //start preview ctx2.beginPath(); //clean from last line ctx2.globalCompositeOperation = "destination-out"; ctx2.strokeStyle = "rgba(255, 0, 0, 1)"; ctx2.moveTo(points[points.length-2].x, points[points.length-2].y); ctx2.lineTo(points[points.length-1].x, points[points.length-1].y); ctx2.stroke(); //rest ctx2.strokeStyle = "rgba(255, 0, 0, 0.5)"; ctx2.globalCompositeOperation = "source-over"; //draw new line segment ctx2.moveTo(points[points.length-2].x, points[points.length-2].y); ctx2.lineTo(points[points.length-1].x, points[points.length-1].y); ctx2.stroke(); } }; el2.onmouseup = function() { ctx2.clearRect(0, 0, ctx2.canvas.width, ctx2.canvas.height); ctx.stroke(); isDrawing = false; points.length = 0; }; 
 canvas { border: 1px solid #ccc; position:absolute; } 
 <canvas id="c" width="500" height="200"></canvas> <canvas id="c2" width="500" height="200"></canvas> 

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

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