简体   繁体   English

HTML5 Canvas-如何填充()特定上下文

[英]HTML5 Canvas - How to fill() a specific context

I'm trying to make a website where an image is drawn on Canvas, then later the user is able to press a button to ctx.fill() certain parts of it with color. 我正在尝试创建一个在Canvas上绘制图像的网站,然后用户可以按下按钮ctx.fill()用颜色对其某些部分进行着色。 I'm running into issues where I can only ctx.fill() the most recently created shape which often isn't the shape I want. 我遇到的问题是,我只能ctx.fill()最新创建的形状,而该形状通常不是我想要的形状。

Here's an example. 这是一个例子。 In this code (live at http://build.rivingtondesignhouse.com/piol/test/ ) I'm trying to draw the first rectangle, then save() it on the stack, then draw the second rectangle (and don't save it), then when my fill() function is called I want to restore() the first rectangle and ctx.fill() it with a different pattern. 在这段代码中(位于http://build.rivingtondesignhouse.com/piol/test/ ),我试图绘制第一个矩形,然后将它保存()到堆栈上,然后绘制第二个矩形(不要保存),然后在调用我的fill()函数时,我想使用不同的模式来还原()第一个矩形和ctx.fill()。 It doesn't work! 它不起作用!

In practice, I'm actually trying to fill the gray part of this complex shape with any color the user chooses AFTER the image has been drawn, but I think the technique is the same. 实际上,我实际上是尝试在绘制图像后用用户选择的任何颜色填充此复杂形状的灰色部分,但是我认为技术是相同的。 (http://build.rivingtondesignhouse.com/piol/test/justTop.html) (http://build.rivingtondesignhouse.com/piol/test/justTop.html)

Thanks in advance for any help!!! 在此先感谢您的帮助!!!

Here's the code: 这是代码:

<script type="text/javascript">
var canvas;
var ctx;

function init() {
    canvas = document.getElementById("canvas");
    ctx = canvas.getContext("2d");
    draw();
}


function draw() {   
    ctx.fillStyle = '#FA6900';
    ctx.shadowOffsetX = 5;
    ctx.shadowOffsetY = 5;
    ctx.shadowBlur    = 4;
    ctx.shadowColor   = 'rgba(204, 204, 204, 0.5)';
    ctx.fillRect(0,0,15,150);
    ctx.save();

    ctx.fillStyle = '#E0E4CD';
    ctx.shadowOffsetX = 10;
    ctx.shadowOffsetY = 10;
    ctx.shadowBlur    = 4;
    ctx.shadowColor   = 'rgba(204, 204, 204, 0.5)';
    ctx.fillRect(30,0,30,150);
}

function fill(){
    var image = new Image();
    image.src = "http://www.html5canvastutorials.com/demos/assets/wood-pattern.png";
    image.onload = drawPattern;
    function drawPattern() {
        ctx.restore();
        ctx.fillStyle = ctx.createPattern(image, "repeat");
        ctx.fill();
    }
}

init();

There are a few misunderstands that we need to clear up before I can answer the question. 在我可以回答这个问题之前,我们需要消除一些误解。

save() and restore() do not save and restore the canvas bitmap. save()restore()不会保存和还原画布位图。 Instead they save and restore all properties that are set on the canvas context and that's all! 相反,它们保存并还原在画布上下文中设置的所有属性,仅此而已!

For example 例如

ctx.fillStyle = 'red';
ctx.save(); // save the fact that the fillstyle is red
ctx.fillStyle = 'blue'; // change the fillstyle
ctx.fillRect(0,0,5,5); // draws a blue rectangle
ctx.restore(); // restores the old context state, so the fillStyle is back to red
ctx.fillRect(10,0,5,5); // draws a red rectangle // draws a red rectangle

See that code live here . 看到这里的代码。

So you aren't saving a rectangle (or anything drawn) by calling save() . 因此,您不必通过调用save()save()矩形(或任何绘制的内容save() The only way you you can save the bitmap is by drawing it (or part of it) to another canvas (using anotherCanvasContext.drawImage(canvasIWantToSave, 0, 0) ) or by saving enough information that you can redraw the entire scene with the appropriate changes. 保存位图的唯一方法是通过将位图(或部分位图)绘制到另一个画布上(使用anotherCanvasContext.drawImage(canvasIWantToSave, 0, 0) )或保存足够的信息,以便可以使用适当的位图重绘整个场景。变化。

Here is an example of one way you could re-structure your code so that it does what you want: http://jsfiddle.net/xwqXb/ 这是一个示例的示例,您可以通过这种方法重新构造代码,使其执行所需的操作: http : //jsfiddle.net/xwqXb/

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

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