html5 - HTML Canvas - JavaScript dynamic rotation issue -
i trying make simple game html canvas , javascript running unique issue. code incomplete , perhaps missing obvious, trying apply fluctuating rotate transformation lightsaber image follows position of mouse. problem seems moving canvas diagonally @ same time applying rotation leaves pieces of old frames left behind.
the essential idea called continuously upon registration of "mousemove" event:
ctx.clearrect(0, 0, w, h); ctx.translate(ax, ay); ctx.rotate(math.pi/180); // draw image ctx.translate(-ax, -ay); ctx.drawimage(saberimg, x-90, y-438); to illustrate: https://jsfiddle.net/2ynfq5k0/
update: click right mouse button rotate right, , mouse wheel rotate left.
you aren't resetting canvas state, clearrect still operating under rotation, resulting in not clearing canvas.
as general rule, if you're working transformations (translate, rotate, ...) should always put ctx.save() before, , ctx.restore() after. saves , restores canvas' state regard transformations , styles. great bigger projects, want sure code in 1 place doesn't interfere code elsewhere, , canvas equivalent "not polluting global scope".
Comments
Post a Comment