简体   繁体   English

绘制后更改画布上的笔颜色

[英]Changing Pen Color on Canvas after draw

I'm trying to change the color of the contents of the canvas after it is drawn.我试图在绘制后更改画布内容的颜色。 So if you start drawing a green circle, you could then decide later to make your previously drawn circle into a red a circle.因此,如果您开始绘制一个绿色圆圈,您可以稍后决定将之前绘制的圆圈变成红色圆圈。

I'm using the signaturePad plugin here: https://github.com/szimek/signature_pad我在这里使用signaturePad插件: https : //github.com/szimek/signature_pad

I have some of the functionality built, but the pen color change doesn't change previously drawn elements.我已经构建了一些功能,但笔颜色的变化不会改变以前绘制的元素。 Here's a fiddle: http://jsfiddle.net/Z6g5Z/这是一个小提琴: http : //jsfiddle.net/Z6g5Z/

Thanks for your help!谢谢你的帮助! The fiddle is prob.小提琴是有问题的。 the best way to see the issue, but the JS and markup are below.查看问题的最佳方式,但 JS 和标记如下。

var canvas = $("#can")[0];
var signaturePad = new SignaturePad(canvas, {
    minWidth: 2,
    maxWidth: 5,
    penColor: "rgb(66, 133, 244)"
});

$('#clear').click(function(){
    signaturePad.clear();
});

$('.global-color li').click(function(){
    $('.on').removeClass('on');
    var $t = $(this);
    $t.addClass('on');
    var selectedColor = $t.data('color');
    signaturePad.penColor = hexToRgb(selectedColor);
});


<ul class="global-color">
    <li class="yellow-pick" data-color="#f8c90d">yellow</li>
    <li class="green-pick" data-color="#3dae49">green</li>
    <li class="orange-pick" data-color="#e87425">orange</li>
    <li class="blue-pick on" data-color="#009cc5">blue</li>
</ul>
<div>
    <input id="clear" type="button" value="clear" />
</div>
<canvas id="can" width="200px" height="200px"></canvas>

In your color change handler, have all canvas change its (non-transparent) pixels to the new color.在您的颜色更改处理程序中,让所有画布将其(非透明)像素更改为新颜色。
For this, most simple is to use globalComposite operation mode 'source-in', and fill over the canvas with the new color :为此,最简单的是使用 globalComposite 操作模式“source-in”,并用新颜色填充画布:

// set all pixels of the image to this color
function setCurrentColor(canvas, color) {
   var context = canvas.getContext('2d');
   context.save();
   context.fillStyle = color;
   context.globalCompositeOperation = 'source-in';
   context.fillRect(0,0,canvas.width, canvas.height);
   context.restore();
}

i updated your demo : http://jsfiddle.net/gamealchemist/Z6g5Z/3/我更新了你的演示: http : //jsfiddle.net/gamealchemist/Z6g5Z/3/

You can by changing composite mode and fill in the color you want on the existing content.您可以通过更改合成模式并在现有内容上填充您想要的颜色。

With a library like this you have no guarantee of the inner workings of it so it may work today and may not work tomorrow - if the author decides to change the way it behaves internally.对于这样的库,您无法保证它的内部工作原理,因此它今天可能工作,明天可能无法工作 - 如果作者决定改变其内部行为方式。

So with that disclaimer you can use the following code to change the color of the existing drawing - this works in general with all canvas drawings and changes non-transparent pixels to what you draw on top:因此,根据免责声明,您可以使用以下代码更改现有绘图的颜色 - 这通常适用于所有画布绘图,并将非透明像素更改为您在顶部绘制的内容:

function changeColor() {
    ctx.save();
    ctx.globalCompositeOperation = 'source-atop';
    ctx.fillStyle = selectedColor;
    ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    ctx.restore();
}

However, since we are going to tap into the library there are at least one other limitation here.但是,由于我们要使用库,因此这里至少还有一个其他限制。 The library seem to use a second canvas on top where it registers the mouse.该库似乎在其注册鼠标的顶部使用了第二个画布。 When pen is up it transfer that drawing to the main canvas.当笔上升时,它会将绘图转移到主画布上。 The drawback with this, for us, is that the pen change won't happen until we draw something new;对我们来说,这样做的缺点是在我们绘制新东西之前不会发生笔变化; we can't change the pen color visually just by using the above function - for that you would have to patch the library to add for example a method which did all the steps needed internally.我们无法仅通过使用上述函数来直观地更改笔的颜色 - 为此,您必须修补库以添加例如执行内部所需的所有步骤的方法。

But we do get close by adding the following setting:但是我们确实通过添加以下设置来接近:

var signaturePad = new SignaturePad(canvas, {
    minWidth: 2,
    maxWidth: 5,
    penColor: "rgb(66, 133, 244)",
    onBegin: changeColor             /// add callback here
});

Live demo here现场演示在这里

Hope this helps!希望这可以帮助!

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

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