-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.js
63 lines (54 loc) · 1.6 KB
/
context.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
const valueReturningMethods = [
"getImageData",
"createImageData",
"isPointInStroke",
"isPointInPath"
]
// chains a canvas function, but returns the wrapper parameter
let chainMethod = function(wrapper, fn, isGetter) {
if (isGetter) {
return (...args) => fn.apply(wrapper.context, args)
}
else return (...args) => {
fn.apply(wrapper.context, args)
return wrapper
}
}
// chains a property as a function
// -> ctx.text(value) instead of ctx.text = value
let chainProperty = function(wrapper, key) {
return (value) => {
if (typeof value == "undefined") {
return wrapper.context[key]
}
wrapper.context[key] = value
return wrapper
}
}
// Our wrapper around the Native HTML 2D Rendering Context
// adds the ability to chain context methods
export default class Context {
constructor(width = 500, height = 500) {
// todo: remove the default width and heights
this.canvas = document.createElement("canvas")
this.canvas.width = width
this.canvas.height = height
let context = this.context = this.canvas.getContext("2d")
// extend the canvas's context
// for-in iterates over prototype methods, as well
for (let key in context) {
let value = context[key]
let isGetter = false
// detect if it's a method or a property
if (typeof value == "function") {
if (valueReturningMethods.includes(key)) {
isGetter = true
}
this[key] = chainMethod(this, value, isGetter)
}
else if (key != "canvas") {
this[key] = chainProperty(this, key)
}
}
}
}