-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
102 lines (90 loc) · 3.25 KB
/
test.html
File metadata and controls
102 lines (90 loc) · 3.25 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Drawing App with Apple Pencil Support</title>
<style>
canvas {
border: 1px solid #000;
touch-action: none;
}
</style>
</head>
<body>
<h1>Drawing App with Apple Pencil Support</h1>
<canvas id="drawingCanvas" width="500" height="500"></canvas>
<br>
<button id="clearButton">Clear Canvas</button>
<script>
const canvas = document.getElementById('drawingCanvas');
const ctx = canvas.getContext('2d');
const clearButton = document.getElementById('clearButton');
let isDrawing = false;
let lastX = 0;
let lastY = 0;
function startDrawing(e) {
isDrawing = true;
[lastX, lastY] = getCoordinates(e);
}
function draw(e) {
if (!isDrawing) return;
const [x, y] = getCoordinates(e);
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(x, y);
ctx.stroke();
[lastX, lastY] = [x, y];
}
function stopDrawing() {
isDrawing = false;
}
function getCoordinates(e) {
let x, y;
if (e.type.includes('mouse')) {
x = e.offsetX;
y = e.offsetY;
} else {
const rect = canvas.getBoundingClientRect();
x = e.touches ? e.touches[0].clientX - rect.left : e.clientX - rect.left;
y = e.touches ? e.touches[0].clientY - rect.top : e.clientY - rect.top;
}
return [x, y];
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
// Mouse events
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);
// Touch events (for Apple Pencil and touch devices)
canvas.addEventListener('touchstart', startDrawing);
canvas.addEventListener('touchmove', draw);
canvas.addEventListener('touchend', stopDrawing);
// Pointer events (for better precision with stylus)
canvas.addEventListener('pointerdown', startDrawing);
canvas.addEventListener('pointermove', draw);
canvas.addEventListener('pointerup', stopDrawing);
canvas.addEventListener('pointerout', stopDrawing);
clearButton.addEventListener('click', clearCanvas);
// Prevent scrolling when touching the canvas
document.body.addEventListener('touchstart', function (e) {
if (e.target == canvas) {
e.preventDefault();
}
}, { passive: false });
document.body.addEventListener('touchend', function (e) {
if (e.target == canvas) {
e.preventDefault();
}
}, { passive: false });
document.body.addEventListener('touchmove', function (e) {
if (e.target == canvas) {
e.preventDefault();
}
}, { passive: false });
</script>
</body>
</html>