-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawer.js
More file actions
71 lines (67 loc) · 2.54 KB
/
drawer.js
File metadata and controls
71 lines (67 loc) · 2.54 KB
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
(function () {
//merge function: merges two objects together.
//if the key exists in A, don't override it.
var merge = function (a, b) {
if(typeof b != 'object') return b;
if(!a) return b;
for(var key in b) {
if(!(key in a)) a[key] = b[key];
}
return a;
};
/*
the Drawer constructor
all it does is set up the properties that will be used later
It grabs args.canvas,
args.width,
args.height
and that should be it.
and also, if the args.canvas is a DOM Node, appends to the body
*/
function Drawer (args) {
args = merge(args, {canvas: null, width: 100, height: 100});
this._args = args;
this.canvas = typeof args.canvas === 'string' ? document.querySelector(args.canvas) : document.createElement('canvas');
this.canvas.width = args.width;
this.canvas.height = args.height;
this.context = this.canvas.getContext('2d');
if(typeof args.canvas !== 'string' && this.canvas.nodeType) {
try {
document.body.appendChild(this.canvas);
} catch(e) {
//we didn't fucked up, you did. *insanity wolf*
throw new Error("ERROR: Cannot append to body.");
}
}
};
// registers a name in the current instance,
// this name is a object with 'draw' and 'addEventListener' properties
// the 'draw' method calls up drawFn with the canvas2dcontext as the 'this' of the function
// the addEventListener, well... it does what you think it does.
Drawer.prototype.register = function (name, args, drawFn) {
var defaults = typeof args === 'object' && args.reduce(function (def, cur) {
if(typeof cur === 'string') {
def[cur] = 0;
} else {
def[cur[0]] = cur[1];
}
return def;
}, {});
var context = this.context;
var that = this;
this[name] = typeof args === 'object' ? {
draw: function (opt) {
return drawFn.call(context, merge(opt, defaults));
},
addEventListener: function (event, fn) {
return that.addEventListener(event, fn);
}
} : args;
};
// binds a event onto the canvas element
Drawer.prototype.addEventListener = function (event, fn) {
return this.canvas.addEventListener(event, fn);
};
// expose our hand-crafted helper Drawer
window.Drawer = Drawer;
})();