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.
views:
952answers:
1
+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
2009-11-11 19:29:33
Awesome, thanks. But now the image doesn't rotate...
Timothy McDowell
2009-11-11 19:33:22
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
2009-11-11 19:50:08
Oh yeah >_> silly me, sorry. Thanks for the help!
Timothy McDowell
2009-11-11 20:57:50
Vote up/accept the answer if it's good enough? please :)
JasonWyatt
2009-11-11 21:33:56