简体   繁体   English

切换画笔颜色html5画布

[英]Toggle brush colours html5 canvas

I'm trying to create a drawing board with two different colour brushes. 我正在尝试用两个不同的颜色画笔创建一个绘图板。 You can select the brush you want by clicking on the appropriate button. 您可以通过单击适当的按钮来选择所需的画笔。

Note: This only needs to work on iOS Safari. 注意:这仅需要在iOS Safari上运行。

The two brushes are: 这两个画笔是:

  1. A Yellow Highlighter 黄色荧光笔
  2. A solid brush colour 单色笔刷颜色

The issue I am facing is that selecting a different brush, alters the colour of the existing brush strokes on the canvas. 我面临的问题是选择其他画笔会改变画布上现有画笔笔触的颜色。

How can I have each brush not affect the other? 我怎样才能使每支刷子都不互相影响?

Code: 码:

 var el = document.getElementById('c'); var ctx = el.getContext('2d'); var isDrawing; var _highlight = false; function marker(){ ctx.lineWidth = 10; ctx.strokeStyle = 'rgba(0,0,0,1)'; } function highlight(){ ctx.lineWidth = 15; ctx.strokeStyle = 'rgba(255,255,0,0.4)'; ctx.globalCompositeOperation = 'destination-atop'; } document.getElementById("marker").addEventListener("click", function(){ _highlight = false; }); document.getElementById("clear").addEventListener("click", function(){ ctx.clearRect(0, 0, el.width, el.height); ctx.restore(); ctx.beginPath(); }); document.getElementById("highlight").addEventListener("click", function(){ _highlight = true; }); el.onmousedown = function(e) { isDrawing = true; if(_highlight){ highlight(); ctx.lineJoin = ctx.lineCap = 'round'; ctx.moveTo(e.clientX, e.clientY); }else{ marker(); ctx.lineJoin = ctx.lineCap = 'round'; ctx.moveTo(e.clientX, e.clientY); } }; el.onmousemove = function(e) { if (isDrawing) { if(_highlight){ highlight(); ctx.lineTo(e.clientX, e.clientY); ctx.stroke(); }else{ marker(); ctx.lineTo(e.clientX, e.clientY); ctx.stroke(); } } }; el.onmouseup = function() { isDrawing = false; }; 
 canvas#c { border: 1px solid #ccc; background:url(http://i.imgur.com/yf6d9SX.jpg); position: relative; left: 0; top: 0; z-index: 2; } 
 <canvas id="c" width="930" height="500"></canvas> <br> <button id="marker">Marker</button> <button id="highlight">Highlight</button> <button id="clear">Clear</button> 

You are accumulating all lines to the same path, so every time you stroke the current stroke color will be used for all of them, including the previous lines. 您将所有行累积到同一路径,因此,每次笔划时,所有笔划都将使用当前笔划颜色,包括前几行。

Try adding beginPath() at your mouse down event as well as in your pen change. 尝试在鼠标按下事件以及换笔时添加beginPath()

There are several other issues though in this code not addressed here, including: 尽管此代码中未解决其他一些问题,包括:

  • Composite mode when pen changes 笔更换时的复合模式
  • Mouse positions must be corrected to relative position of canvas 鼠标位置必须更正到画布的相对位置

(For marker effect you can also use the new blending mode "multiply" instead of "destination-atop", does not work in IE though). (对于标记效果,您也可以使用新的混合模式“乘”而不是“目的地优先”,但是在IE中不起作用)。

 var el = document.getElementById('c'); var ctx = el.getContext('2d'); var isDrawing; var _highlight = false; function marker() { ctx.lineWidth = 10; ctx.strokeStyle = 'rgba(0,0,0,1)'; ctx.globalCompositeOperation = 'source-over'; } function highlight() { ctx.lineWidth = 15; ctx.strokeStyle = 'rgba(255,255,0,0.4)'; ctx.globalCompositeOperation = "multiply"; if (ctx.globalCompositeOperation !== "multiply") // use multiply if available ctx.globalCompositeOperation = 'destination-over'; // fallback mode } document.getElementById("marker").addEventListener("click", function() { _highlight = false; }); document.getElementById("clear").addEventListener("click", function() { ctx.clearRect(0, 0, el.width, el.height); ctx.beginPath(); }); document.getElementById("highlight").addEventListener("click", function() { _highlight = true; }); el.onmousedown = function(e) { var pos = getMouse(e); isDrawing = true; ctx.beginPath(); _highlight ? highlight() : marker(); ctx.lineJoin = ctx.lineCap = 'round'; ctx.moveTo(pos.x, pos.y); }; el.onmousemove = function(e) { if (isDrawing) { var pos = getMouse(e); ctx.lineTo(pos.x, pos.y); ctx.stroke(); } }; el.onmouseup = function() { isDrawing = false; }; function getMouse(e) { var rect = el.getBoundingClientRect(); return {x: e.clientX - rect.left, y: e.clientY - rect.top} } 
 canvas#c { border: 1px solid #ccc; background: url(http://i.imgur.com/yf6d9SX.jpg); position: relative; left: 0; top: 0; z-index: 2; } 
 <canvas id="c" width="930" height="500"></canvas> <br> <button id="marker">Marker</button> <button id="highlight">Highlight</button> <button id="clear">Clear</button> 

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

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