Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems clearing canvas when animating a clipping region
    primarykey
    data
    text
    <p>I'm trying to accomplish an effect similar to what you might see on the cartoon Chowder (<a href="http://youtu.be/EDcICGLpybQ?t=29s" rel="nofollow">example link</a>) , where shapes serve as masking layers for a texture underneath that stays static. I've begun playing around with this idea by creating a render loop that clears the canvas, saves it's state, then draws a rectangular clipping region, followed by drawing the background texture that occupies the entire width and height of the canvas.</p> <p>Here's the draw function:</p> <pre><code>function draw() { context.clearRect(0,0, 640, 480); context.save(); x += velocityX; y += velocityY; context.rect(x, y, 40, 40); context.clip(); context.drawImage(image, 0,0, 640, 480); context.restore(); } </code></pre> <p>Basically it just runs at 60 frames per second, updating the position of the rectangle and clipping a background image inside the clipping region. (I know the code isn't structured perfectly, but I was just experimenting to see if this effect was even possible on the canvas).</p> <p><a href="http://jsfiddle.net/JERje/86/" rel="nofollow">http://jsfiddle.net/JERje/86/</a></p> <p>The problem I seem to be having is that the clipping area from the previous iteration of the loop hangs around creating the weird effect that you see in the fiddle above. I've tried reordering everything in the <code>draw()</code> step of the loop, but the only thing that seems to work is the <code>canvas.width = canvas.width</code> trick for clearing the screen. I'd like to avoid this method of clearing the screen, since it doesn't seem to work in IE, and it also destroys the canvas <code>state</code>. <code>clearRect()</code> should work to clear the screen. What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload