-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas_wrapper.js
67 lines (56 loc) · 1.41 KB
/
canvas_wrapper.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
function contructor() {
this.refsize_el = null;
this.canvas = null;
this.context = null;
this.width_units = 1;
return this;
}
export default function UnbrumedCanvas() {
constructor.apply(this);
this.pxsize = {
width: 0,
height: 0,
};
/**
* @param {HTMLElement} canvas
* @param {HTMLElement|window} refwidth/height reference element
* @param {number} units draw height in unis
*/
this.init = (canvas, ref, units) => {
this.canvas = canvas;
this.width_units = units;
this.context = canvas.getContext("2d");
this.refsize_el = reference;
};
/**
* units to pixels
*
* @returns {number} canvas absolute units converted into pixels
*/
this.utopx = (units) => {
return Math.floor(this.unitsize() * units);
};
this.pxtou = (pixels) => {
return pixels / this.unitsize();
};
this.unitsize = () => {
return this.pxsize.width / this.width_units;
};
this.uwidth = () => {
return this.width_units;
};
this.uheight = () => {
return this.pxtou(this.pxsize.height);
};
this.update = () => {
if (this.refsize_el) {
const width = this.refsize_el.clientWidth ?? this.refsize_el.innerWidth;
const height =
this.refsize_el.clientHeight ?? this.refsize_el.innerHeight;
this.pxsize.width = width;
this.pxsize.height = height;
this.canvas.width = width;
this.canvas.height = height;
}
};
}