-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
101 lines (101 loc) · 2.81 KB
/
app.js
File metadata and controls
101 lines (101 loc) · 2.81 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const canvas = document.querySelector("canvas");
const file = document.getElementById("file");
const ctx = canvas.getContext("2d");
const colorOptions = Array.from(
document.getElementsByClassName("color-option")
);
const saveBtn = document.getElementById("save-btn");
const eraseBtn = document.getElementById("erase-btn");
const resetBtn = document.getElementById("reset-btn");
const modeBtn = document.getElementById("mode-btn");
canvas.width = 800;
canvas.height = 800;
const lineWidth = document.getElementById("line-width");
ctx.lineWidth = lineWidth.value;
const lineColor = document.getElementById("color");
ctx.strokeStyle = lineColor.value;
let isPainting = false;
let isFilling = false;
let x = 0;
let y = 0;
function onMove(event) {
if (isPainting) {
ctx.lineTo(event.offsetX, event.offsetY);
ctx.stroke();
return;
}
ctx.beginPath();
ctx.moveTo(event.offsetX, event.offsetY);
}
function startPainting() {
isPainting = true;
}
function stopPainting() {
isPainting = false;
}
function onChange(event) {
ctx.lineWidth = event.target.value;
}
function changeColor(event) {
ctx.strokeStyle = event.target.value;
}
function onColorClick(event) {
ctx.strokeStyle = event.target.dataset.color;
color.value = event.target.dataset.color;
}
function onModeClick() {
if (isFilling) {
isFilling = false;
modeBtn.innerText = "Fill";
} else {
isFilling = true;
modeBtn.innerText = "Draw";
}
}
function onCanvasClick(event) {
if (isFilling) {
ctx.fillStyle = color.value;
ctx.fillRect(0, 0, 800, 800);
}
x = event.offsetX;
y = event.offsetY;
}
function onReset() {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, 800, 800);
}
function onErase() {
ctx.strokeStyle = "white";
isFilling = false;
modeBtn.innerText = "Fill";
}
function onFileChange(event) {
const file = event.target.files[0];
const url = URL.createObjectURL(file);
const image = new Image();
image.src = url;
image.onload = function () {
ctx.drawImage(image, x, y, 100, 100);
file.value = null;
};
}
function onSaveClick() {
const url = canvas.toDataURL();
const a = document.createElement("a");
a.href = url;
a.download = "myToken";
a.click();
}
canvas.addEventListener("mousemove", onMove);
canvas.addEventListener("mousedown", startPainting);
canvas.addEventListener("mouseup", stopPainting);
canvas.addEventListener("mouseleave", stopPainting);
canvas.addEventListener("click", onCanvasClick);
lineWidth.addEventListener("change", onChange);
lineColor.addEventListener("change", changeColor);
modeBtn.addEventListener("click", onModeClick);
resetBtn.addEventListener("click", onReset);
eraseBtn.addEventListener("click", onErase);
colorOptions.forEach((color) => color.addEventListener("click", onColorClick));
file.addEventListener("change", onFileChange);
saveBtn.addEventListener("click", onSaveClick);