javascript - Multiple calls to canvas.putImageData in HTML5 -
i'm building application makes user-specified number of calls canvas.putimagedata in javascript. between each call putimagedata, image data modified. time needed modify data varies between calls. user needs able stop animation while it's running (i'm using button). used:
for (var i=0; < n; i++) { canvas.putimagedata(imgdata, 0, 0); modify(imgdata); } however, doesn't display each frame, last one. work put alert after canvas.putimagedata, that's super annoying. tried using setinterval , cancelinterval i'm sure many suggest, doesn't work me 2 reasons:
- the time needed modify image data varies.
- setinterval asynchronous; if delay use short, calls modify(imgdata) pile on stack , users won't able stop animation when want.
how can make work correctly?
use repeating settimeout():
var shouldstop = false; function iteration() { canvas.putimagedata(imgdata, 0, 0); modify(imgdata); if (!shouldstop) { window.settimeout(iteration, 1000); } } function stop() { shouldstop = true; } this display , modify image data each second.
another solution delegate modify algorithm web worker.
Comments
Post a Comment