-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlayeredCanvas.js
76 lines (64 loc) · 1.6 KB
/
layeredCanvas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
layeredCanvas v0.1
by Federico Jacobi
federicojacobi.com
Abstraction layer on canvas to mimic the use of layers
*/
layeredCanvas = function ( id ) {
this.layers = [];
var extend = function ( defaults, options ) {
var extended = {} , prop;
for (prop in defaults) {
if (Object.prototype.hasOwnProperty.call(defaults, prop))
extended[prop] = defaults[prop];
}
for (prop in options) {
if (Object.prototype.hasOwnProperty.call(options, prop))
extended[prop] = options[prop];
}
return extended;
};
this.addLayer = function( obj ) {
layer = extend( {
id: Math.random().toString(36).substr(2, 5),
show: true,
render: function( canvas, ctx ) {}
}, obj );
if ( this.getLayer( layer.id ) !== false ) {
console.log( 'Layer already exists' );
console.log( obj );
return false;
}
this.layers.push( layer );
return this;
};
this.getLayer = function( id ) {
var length = this.layers.length;
for ( var i = 0; i < length; i++ ) {
if ( this.layers[i].id === id )
return this.layers[i];
}
return false;
};
this.removeLayer = function( id ) {
var length = this.layers.length;
for ( var i = 0; i < length; i++ ) {
if ( this.layers[i].id === id ) {
removed = this.layers[i];
this.layers.splice( i, 1 );
return removed;
}
}
return false;
};
this.render = function() {
var canvas = this.canvas;
var ctx = this.ctx2d;
this.layers.forEach( function( item, index, array ) {
if ( item.show )
item.render( canvas, ctx );
});
};
this.canvas = document.getElementById( id );
this.ctx2d = this.canvas.getContext( '2d' );
};