-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
137 lines (120 loc) · 3.76 KB
/
Copy pathscript.js
File metadata and controls
137 lines (120 loc) · 3.76 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const canvas = document.getElementById("drawingCanvas");
const ctx = canvas.getContext("2d");
const startButton = document.getElementById("startButton");
const resetButton = document.getElementById("resetButton");
const image = document.getElementById("image");
const timerElement = document.getElementById("timer");
let preventScroll = false;
let timeLeft = 15;
let timer;
let isDrawing = false;
let timerStarted = false;
let imageLoaded = false;
startButton.addEventListener("click", () => {
preventScroll = true;
startTimer();
});
function drawBackground() {
const img = new Image();
img.src = 'Frame.png'
img.onload = () => {
imageWidth = img.width;
imageHeight = img.height;
imageX = (canvas.width - img.width) / 2;
imageY = (canvas.height - img.height) / 2;
ctx.drawImage(img, imageX, imageY);
imageLoaded = true;
};
}
function startTimer() {
if (timerStarted) return;
timerStarted = true;
startButton.style.display = "none";
timer = setInterval(() => {
timeLeft--;
timerElement.textContent = timeLeft;
if (timeLeft <= 0) {
clearInterval(timer);
canvas.style.pointerEvents = "none";
image.style.visibility = "hidden"
document.getElementById("drawingCanvas").style.opacity = "100%";
}
}, 1000);
}
function resetCanvas() {
clearInterval(timer);
ctx.clearRect(0, 0, canvas.width, canvas.height);
timeLeft = 15;
timerElement.textContent = timeLeft;
canvas.style.pointerEvents = "auto";
startButton.style.display = "inline-block";
timerStarted = false;
drawBackground();
}
function getMousePos(event) {
const rect = canvas.getBoundingClientRect();
return {
x: (event.clientX - rect.left) * (canvas.width / rect.width),
y: (event.clientY - rect.top) * (canvas.height / rect.height)
};
}
canvas.addEventListener("mousedown", (event) => {
if (!timerStarted || !imageLoaded) return;
isDrawing = true;
const pos = getMousePos(event);
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
});
canvas.addEventListener("mouseup", () => isDrawing = false);
canvas.addEventListener("mousemove", (event) => {
if (!isDrawing || !imageLoaded) return;
const pos = getMousePos(event);
ctx.lineTo(pos.x, pos.y);
ctx.strokeStyle = "black";
ctx.lineWidth = 3;
ctx.lineCap = "round";
ctx.stroke();
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
});
function getTouchPos(event) {
const rect = canvas.getBoundingClientRect();
const touch = event.touches[0];
return {
x: (touch.clientX - rect.left) * (canvas.width / rect.width),
y: (touch.clientY - rect.top) * (canvas.height / rect.height)
};
}
canvas.addEventListener("touchstart", (event) => {
if (preventScroll) {
event.preventDefault()
}
if (!timerStarted || !imageLoaded) return;
isDrawing = true;
const pos = getTouchPos(event);
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
});
canvas.addEventListener("touchmove", (event) => {
if (preventScroll) {
event.preventDefault();
}
if (!isDrawing || !imageLoaded) return;
const pos = getTouchPos(event);
ctx.lineTo(pos.x, pos.y);
ctx.strokeStyle = "black";
ctx.lineWidth = 3;
ctx.lineCap = "round";
ctx.stroke();
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
});
canvas.addEventListener("touchend", () => {
isDrawing = false;
if (preventScroll) {
preventScroll = false;
}
});
startButton.addEventListener("click", startTimer);
resetButton.addEventListener("click", resetCanvas);
window.onload = drawBackground;