views:

216

answers:

1

Hi,

I'm using the Canvas object with javascript. Just doing some tests to see how fast I can set pixels in a draw loop.

On mac, it works great in FF, safari, chrome. On windows, I get a flickering effect on FF and chrome. It looks like somehow the canvas implementation on windows is different than on mac for the different browsers? (not sure if that's true).

This is the basic code I'm using to do the drawing (taken from the article below - I've optimized the below to tighten the draw loop, it runs pretty smooth now):

var canvas = document.getElementById('myCanvasElt');
var ctx = canvas.getContext('2d');
var canvasData = ctx.getImageData(0, 0, canvas.width, canvas.height);
for (var x = 0; x < canvasData.width; x++) {
    for (var y = 0; y < canvasData.height; y++) {
        // Index of the pixel in the array
        var idx = (x + y * canvas.width) * 4;
        canvasData.data[idx + 0] = 0;
        canvasData.data[idx + 1] = 255;
        canvasData.data[idx + 2] = 0;
        canvasData.data[idx + 3] = 255;
    }
}
ctx.putImageData(canvasData, 0, 0);

again, browers on windows will flicker a bit. It looks like the canvas implementation is trying to clear the canvas to white before the next drawing operation takes place (this does not happen on mac). I'm wondering if there is a setting I can change in the Canvas object to modify that value (double-buffering, clear before draw, etc)?

This is the article I am using as reference: http://hacks.mozilla.org/2009/06/pushing-pixels-with-canvas/

Thanks

A: 

The problem is with the way the browsers use the native graphics APIs on the different OSes. And even on the same OS, using different APIs (for example GDI vs. Direct2D in Windows) would also produce different results.

Franci Penov
Yeah I guess there's nothing to be done about it - there's no way to tell Canvas to change the way the browser implements redrawing, right? Like a canvas.pleaseDoubleBuffer="true" flag?
Or I mean rather - it just seems like the canvas on some implementations is erasing to white after each draw - if that could be turned off, all would be perfect. Don't think there's any such thing to do that though.
Unfortunately, I am not aware of a way to control how the canvas uses the underlying OS APIs.
Franci Penov