Skip to content Skip to sidebar Skip to footer

Html5 Canvas Drawing In Real Time

Question: How can I make putImageData() update the canvas in real time, as various parts of the image have been computed? I am working on a JavaScript/TypeScript application to dr

Solution 1:

Try wrapping putImageData and the calculation for a single row to a setTimeout call to execute it asynchronously (post accept edit: see the final code in the question, this won't work since i will be undefined in the putImageData row)

public draw() {
    var that = this;
    var drawRow = function() {
            for(var j: number = 0; j < that.Size.Width; ++j) { // Calc px. for one row
                that.setPixelColor(j, color); // sets a color in this.Pixels (works)
            }

            // TODO specify the dirty region in this call
            that.Context.putImageData(that.ImageData, 0, i); // Draw the row on the canvas?
        };
    for(var i: number = 0; i < this.Size.Height; ++i) {    // Loop over each row
        setTimeout(drawRow, 0);
    }
}

Post a Comment for "Html5 Canvas Drawing In Real Time"