-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.js
77 lines (70 loc) · 2.36 KB
/
tool.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
68
69
70
71
72
73
74
75
76
77
import g from "./global.js";
import Shape from "./shape.js";
class Tool {
constructor() {
const buttons = document.querySelectorAll("#tool .mode");
for (let i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", () => {
g.currentMode = buttons[i].value;
if (!buttons[i].classList.contains("selected")) {
for (let j = 0; j < buttons.length; j++) {
buttons[j].classList.remove("selected");
}
buttons[i].classList.add("selected");
}
});
}
const color = document.getElementById("color");
color.addEventListener("input", () => {
g.currentColor = color.value;
g.ctx.fillStyle = color.value;
});
const toggleGrid = document.getElementById("toggle-grid");
toggleGrid.addEventListener("change", () => {
g.grid = toggleGrid.checked;
});
const exportShape = document.getElementById("export");
exportShape.addEventListener("click", () => {
const txt = JSON.stringify(g.shapes);
const file = new File([txt], `ccbt-${Date.now()}.json`, { type: "text/plain" });
const url = URL.createObjectURL(file);
const link = document.createElement("a");
link.href = url;
link.download = file.name;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
});
const importShape = document.getElementById("import");
importShape.addEventListener("click", (e) => {
const input = document.createElement("input");
input.type = "file";
input.onchange = () => {
const file = input.files[0];
const reader = new FileReader();
reader.addEventListener("load", (e) => {
const shapes = JSON.parse(e.target.result);
for (let i = 0; i < shapes.length; i++) {
let s = new Shape(
shapes[i].x,
shapes[i].y,
shapes[i].type,
shapes[i].color
);
g.shapes.push(s);
}
g.shapesIndex = g.shapes.length - 1;
});
reader.readAsText(file);
};
input.click();
});
const clear = document.getElementById("clear");
clear.addEventListener("click", () => {
g.shapes = [];
g.ctx.clearRect(0, 0, g.CANVAS_WIDTH, g.CANVAS_HEIGHT);
});
}
}
export default Tool;