views:

952

answers:

1

http://gist.github.com/232194

I know it's something wrong with my transformations in drawGuy. Can anyone help me figure out how to rotate just the image? Currently it rotates fine, but I something with the transforms is distorting it, so that it doesn't follow the mouse correctly.

+4  A: 

Instead of translating forward and then translating backward again, simply push your current state onto the stack before you traslate/rotate, and when you're done - pop your state back off the stack. This is how most graphical applications make use of translations/rotations.

Also, you're translating by x, y, then additionally calling ctx.drawImage(guy, x, y). This is going to, in effect, double the offset. I'd either get rid of the translate call, or change the position arguments for the drawImage call to 0, 0.

function drawGuy() {
    ctx.save();                                                                                                                        
    ctx.translate(x,y);     
    ctx.rotate(angle * Math.PI / 180);                                                                               
    ctx.drawImage(guy, 0, 0); 
    ctx.restore();                                            
}

Check out the spec about context.save() and context.restore() (the way canvas does state push/pop), here.

JasonWyatt
Awesome, thanks. But now the image doesn't rotate...
Timothy McDowell
One more edit.. you'll need to increment your rotation angle. That's why the rotation isn't happening as an animation. It's happening, just not changing over time.
JasonWyatt
Oh yeah >_> silly me, sorry. Thanks for the help!
Timothy McDowell
Vote up/accept the answer if it's good enough? please :)
JasonWyatt