-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.js
66 lines (50 loc) · 2.06 KB
/
draw.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
let wordCanvas = document.createElement("canvas");
wordCanvas.style = "position: absolute; left: 0; top: 2em; z-index: 1; width: 100%; height: calc(100% - 2em);";
let context = wordCanvas.getContext("2d", { alpha: true });
let draw = (sectionedWords) => {
// const canvasRect = wordCanvas.getBoundingClientRect();
const canvasWidth = wordCanvas.width;
const canvasHeight = wordCanvas.height;
// console.log("draw canvas width ehgith:", canvasWidth, canvasHeight);
context.clearRect(0, 0, canvasWidth, canvasHeight);
let drawFrame = () => {
context.save();
context.globalAlpha = 0.2;
context.fillStyle = "black";
context.fillRect(0.01 * canvasWidth, 0.01 * canvasHeight, 0.2 * canvasWidth, 0.9 * canvasHeight);
context.globalAlpha = 1;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = "#fff";
context.font = "36px Vsans-serif";
context.fillText("Word List", (0.21 / 2) * canvasWidth, 0.035 * canvasHeight);
context.restore();
};
let drawWord = (wordInfo, position) => {
context.save();
context.textAlign = "left";
context.fillStyle = "#FFF";
context.textBaseline = "middle";
context.font = "30px Vsans-serif";
let x = 0.02 * canvasWidth;
context.fillText(`${position + 1}. `, x, (2.5 * 0.035 + 0.035 * position) * canvasHeight);
x += context.measureText(`${position + 1}. `).width;
for (const section of wordInfo.sections) {
let text = section.str;
context.fillStyle = section.color;
context.fillText(text, x, (2.5 * 0.035 + 0.035 * position) * canvasHeight);
x += context.measureText(text).width;
}
context.restore();
};
drawFrame();
sectionedWords.forEach((wordInfo, index) => {
drawWord(wordInfo, index);
});
}
let initCanvas = (width, height) => {
wordCanvas.width = width;
wordCanvas.height = height;
let div = $(".canvasArea");
div.appendChild(wordCanvas);
}