-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfx-canvas.js
65 lines (52 loc) · 1.51 KB
/
gfx-canvas.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
function GFX_Canvas(canvas) {
let ctx = canvas.getContext("2d");
let textures = {};
ctx.font = "8px ElGato";
function drawTexture(name, x, y) {
const t = textures[name];
ctx.drawImage(t.tinted ? t.buffer : t.img, x, y);
}
function drawSubTexture(name, sx, sy, sw, sh, dx, dy) {
const t = textures[name];
ctx.drawImage(t.tinted ? t.buffer : t.img, sx, sy, sw, sh, dx, dy, sw, sh);
}
function loadTexture(name, image) {
textures[name] = {
img: image,
size: { x: image.width, y: image.height },
tinted: false,
};
}
function tintTexture(name, color) {
const t = textures[name];
if (t.buffer === undefined) {
t.buffer = document.createElement("canvas");
t.buffer.width = t.img.width;
t.buffer.height = t.img.height;
t.btx = t.buffer.getContext("2d");
}
t.btx.globalCompositeOperation = "copy";
t.btx.drawImage(t.img, 0, 0);
t.btx.fillStyle = "rgb(" + color[0]*255 + "," + color[1]*255 + "," + color[2]*255 + ")";
t.btx.globalCompositeOperation = "multiply";
t.btx.fillRect(0, 0, t.img.width, t.img.height);
t.btx.globalCompositeOperation = "destination-atop";
t.btx.drawImage(t.img, 0, 0);
t.tinted = true;
}
function untintTexture(name) {
textures[name].tinted = false;
}
function getTextureSize(name) {
return textures[name].size;
}
return {
drawTexture,
drawSubTexture,
loadTexture,
tintTexture,
untintTexture,
getTextureSize,
};
}
export { GFX_Canvas };