hardware acceleration - Accelerated canvas context translation: bug, or my mistake? -
i found odd behavior while working on pet game. wanted draw few objects on canvas, of them required image / icon rotated. quite common use case, usual solution make use of context's rotate method. going blow used translate nicely , consistently put images in desired place.
this worked fine, until tried chrome on windows laptop, hardware acceleration enabled. images started blink , teleport across screen. found related acceleration (turning off accelerated graphics fixes problem) , best guess when updating frame renderer assumes drawing calls independent , can executed in parallel. when context transforms take place not case because context state changes.
example of undesired behavior: having canvas element id 'screen' try following:
var canvas = document.getelementbyid("screen"), ctx = canvas.getcontext("2d"); var drawrect = function () { ctx.fillstyle = this.color; ctx.translate(this.x+10, this.y+10); ctx.rotate(this.rotation); ctx.fillrect(-10, -10, 20, 20); ctx.rotate(-this.rotation); ctx.translate(-this.x-10, -this.y-10); }; var red = { x: 22, y: 22, rotation: 0, color: "#ff0000", draw: drawrect }; var blu = { x: 22, y: 111, rotation: 0, color: "#0000ff", draw: drawrect }; function main_loop() { ctx.clearrect(0, 0, 450, 450); frameid = requestanimationframe(main_loop); red.draw(); red.x += 1; red.rotation +=0.1; blu.draw(); blu.x += 1; blu.rotation -=0.1; } main_loop();
working example: http://jsfiddle.net/1u2d7uhr/7/ (tested on chrome, chromium, firefox; accelerated chrome glitches, others not)
i able 'fix' removing translations , rendering rotating elements separate canvas, (after rotations) drawn onto main one. seems hackish me though.
is code error on part?
in case right way render rotations (perhaps question should go codereview, i'm not sure if case)?
or buggy behavior on browser side? understand logic behind can surprising (and cause confusion) developers. or one...
Comments
Post a Comment