Yax
Yax

Reputation: 436

Lock/unlock HTML5 canvas

Is there any possibility of locking canvas element? In my app I draw complex images and sometimes I use bitmaps. Using bitmaps with canvas isn't quite comfortable - all the drawing that should be placed on canvas after bitmap is placed in bitmaps .onload.

It would be a lot easier if I could lock and unlock canvas so it couldn't be updated for some time.

AFAIK there is no built-in function for lock/unlock. Do you know any simple way of implementing it?

Upvotes: 1

Views: 3292

Answers (2)

ParPar
ParPar

Reputation: 7549

$("canvas").attr("disable","False");

// bitmap .onload...

$("canvas").attr("disable","True");

Or

bitmap.onload = function(){
    var canvas = window.getCanvas();
});

Upvotes: 0

pimvdb
pimvdb

Reputation: 154838

I don't know whether this is bulletproof or not, but you could temporarily make .stroke etc. noops: http://jsfiddle.net/eGjak/247/.

var toLock = "stroke fill".split(" ");

function lock() {
    $.each(toLock, function(i, name) { // or another way to loop the array
        ctx[name] = function() {};
    });
}

function unlock() {
    $.each(toLock, function(i, name) {
        ctx[name] = ctx.constructor.prototype[name];
    });
}

Upvotes: 1

Related Questions