-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawstring.js
More file actions
304 lines (247 loc) · 8.95 KB
/
drawstring.js
File metadata and controls
304 lines (247 loc) · 8.95 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const canvas = document.getElementById('drawingCanvas');
const ctx = canvas.getContext('2d');
const splashScreen = document.getElementById('splashScreen');
// Constants
const DEVICE_PIXEL_RATIO = window.devicePixelRatio || 1;
const LINE_WIDTH = 2;
const DOUBLE_TAP_TIMEOUT = 300;
const DOUBLE_TAP_DISTANCE_THRESHOLD = 20;
const MAX_RGB_VALUE = 255;
function resizeCanvas() {
const width = window.innerWidth;
const height = window.innerHeight;
canvas.width = width * DEVICE_PIXEL_RATIO;
canvas.height = height * DEVICE_PIXEL_RATIO;
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
ctx.scale(DEVICE_PIXEL_RATIO, DEVICE_PIXEL_RATIO);
}
// Initial canvas resize
resizeCanvas();
// Resize canvas when the window is resized
window.addEventListener('resize', resizeCanvas);
const ongoingTouches = [];
let drawing = false;
let splashScreenVisible = true;
let lastTapTime = 0;
let isDoubleTap = false;
let currentMode = null; // 'simple' or 'fancy'
function hideSplashScreen() {
splashScreen.style.display = 'none';
splashScreenVisible = false;
}
function initSimpleMode() {
currentMode = 'simple';
// Hide fancy mode elements
document.getElementById('toolbar').style.display = 'none';
document.getElementById('brushPreview').style.display = 'none';
// Apply simple mode styles
document.body.style.background = '';
document.body.className = '';
}
function initFancyModeUI() {
currentMode = 'fancy';
// Show fancy mode elements
document.getElementById('toolbar').style.display = 'flex';
// Apply fancy mode styles
document.body.style.background = 'white';
document.body.className = 'fancy-mode';
// Initialize fancy drawing functionality
initFancyMode();
}
function handleStart(evt) {
// Check if the event target is a UI element (button, input, etc.) that should not trigger drawing
// This must be checked FIRST to prevent any drawing from button touches
if (evt.target.tagName === 'BUTTON' ||
evt.target.tagName === 'INPUT' ||
evt.target.closest('#toolbar')) {
// Don't preventDefault for buttons - let the click event fire normally
return;
}
// Handle splash screen button clicks
if (splashScreenVisible) {
// If splash screen is visible and it's not a button, don't do anything
evt.preventDefault();
return;
}
evt.preventDefault();
if (isDoubleTap) {
isDoubleTap = false;
return;
}
// Only continue with simple mode drawing logic if in simple mode
if (currentMode !== 'simple') return;
const touches = evt.changedTouches || [evt];
for (let i = 0; i < touches.length; i++) {
const touch = touches[i];
const x = touch.clientX;
const y = touch.clientY;
// Validate touch coordinates
if (!isValidCoordinate(x) || !isValidCoordinate(y)) continue;
const colour = `rgb(${Math.floor(Math.random() * MAX_RGB_VALUE)},${Math.floor(Math.random() * MAX_RGB_VALUE)},${Math.floor(Math.random() * MAX_RGB_VALUE)})`;
ongoingTouches.push({
id: touch.identifier || 'mouse',
x: x,
y: y,
colour: colour
});
drawLine(x, y, x, y, colour);
}
drawing = true;
}
function handleMove(evt) {
// Don't prevent default for UI elements
if (evt.target.tagName === 'INPUT' || evt.target.closest('#toolbar')) {
return;
}
evt.preventDefault();
if (splashScreenVisible || !drawing || currentMode !== 'simple') return;
const touches = evt.changedTouches || [evt];
for (let i = 0; i < touches.length; i++) {
const touch = touches[i];
const x = touch.clientX;
const y = touch.clientY;
// Validate touch coordinates
if (!isValidCoordinate(x) || !isValidCoordinate(y)) continue;
const idx = ongoingTouchIndexById(touch.identifier || 'mouse');
if (idx >= 0) {
const colour = ongoingTouches[idx].colour;
drawLine(ongoingTouches[idx].x, ongoingTouches[idx].y, x, y, colour);
ongoingTouches[idx].x = x;
ongoingTouches[idx].y = y;
}
}
}
function handleEnd(evt) {
evt.preventDefault();
if (splashScreenVisible || currentMode !== 'simple') return;
const touches = evt.changedTouches || [evt];
for (let i = 0; i < touches.length; i++) {
const touch = touches[i];
const idx = ongoingTouchIndexById(touch.identifier || 'mouse');
if (idx >= 0) {
ongoingTouches.splice(idx, 1);
}
}
// Clean up any orphaned touches and stop drawing if no touches remain
cleanupTouches();
if (ongoingTouches.length === 0) {
drawing = false;
}
}
function handleCancel(evt) {
evt.preventDefault();
if (splashScreenVisible || currentMode !== 'simple') return;
const touches = evt.changedTouches || [evt];
for (let i = 0; i < touches.length; i++) {
const idx = ongoingTouchIndexById(touches[i].identifier || 'mouse');
if (idx >= 0) {
ongoingTouches.splice(idx, 1);
}
}
// Clean up any orphaned touches
cleanupTouches();
if (ongoingTouches.length === 0) {
drawing = false;
}
}
function handleDoubleTap(evt) {
if (splashScreenVisible || currentMode !== 'simple') return;
const now = new Date().getTime();
const timeSinceLastTap = now - lastTapTime;
if (evt.touches && evt.touches.length === 1) {
// Get the current tap location
const x = evt.touches[0].clientX;
const y = evt.touches[0].clientY;
// Get the last tap location
const lastX = parseFloat(canvas.dataset.lastTapX || 0);
const lastY = parseFloat(canvas.dataset.lastTapY || 0);
// Calculate the distance between the current and last tap
const distance = Math.sqrt(Math.pow(x - lastX, 2) + Math.pow(y - lastY, 2));
// Define a threshold for how close the taps need to be (e.g., 30 pixels)
const distanceThreshold = DOUBLE_TAP_DISTANCE_THRESHOLD;
if (timeSinceLastTap < DOUBLE_TAP_TIMEOUT && distance < distanceThreshold) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
evt.preventDefault();
isDoubleTap = true;
setTimeout(() => { isDoubleTap = false; }, DOUBLE_TAP_TIMEOUT);
}
// Store the current tap location
canvas.dataset.lastTapX = x;
canvas.dataset.lastTapY = y;
} else if (evt.type === 'dblclick') {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
lastTapTime = now;
}
function drawLine(x1, y1, x2, y2, colour) {
ctx.beginPath();
ctx.strokeStyle = colour;
ctx.lineWidth = LINE_WIDTH;
ctx.lineCap = "butt";
if (x1 === x2 && y1 === y2) {
// Draw a dot
ctx.arc(x1, y1, LINE_WIDTH / 2, 0, 2 * Math.PI);
ctx.fillStyle = colour;
ctx.fill();
} else {
// Draw a line
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
ctx.closePath();
}
function isValidCoordinate(coord) {
return typeof coord === 'number' && !isNaN(coord) && isFinite(coord);
}
function ongoingTouchIndexById(idToFind) {
for (let i = 0; i < ongoingTouches.length; i++) {
const id = ongoingTouches[i].id;
if (id === idToFind) {
return i;
}
}
return -1;
}
function cleanupTouches() {
// Remove any touches that might have been orphaned
for (let i = ongoingTouches.length - 1; i >= 0; i--) {
if (!ongoingTouches[i] || !ongoingTouches[i].id) {
ongoingTouches.splice(i, 1);
}
}
}
// Event listeners
function setupEventListeners() {
// Document-level events
document.addEventListener('touchstart', handleStart, false);
document.addEventListener('mousedown', handleStart, false);
// Canvas-specific events
canvas.addEventListener('touchmove', handleMove, false);
canvas.addEventListener('mousemove', handleMove, false);
canvas.addEventListener('touchend', handleEnd, false);
canvas.addEventListener('mouseup', handleEnd, false);
canvas.addEventListener('touchcancel', handleCancel, false);
canvas.addEventListener('dblclick', handleDoubleTap, false);
canvas.addEventListener('touchstart', handleDoubleTap, false);
// Button events - handle mode selection
document.getElementById('simpleButton').addEventListener('click', function(evt) {
if (splashScreenVisible) {
evt.preventDefault();
evt.stopPropagation();
hideSplashScreen();
initSimpleMode();
}
}, false);
document.getElementById('fancyButton').addEventListener('click', function(evt) {
if (splashScreenVisible) {
evt.preventDefault();
evt.stopPropagation();
hideSplashScreen();
initFancyModeUI();
}
}, false);
}
// Initialize event listeners
setupEventListeners();