-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14-point-event.ts
82 lines (68 loc) · 2.19 KB
/
14-point-event.ts
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
/**
* @author 素燕(我有个公众号:素燕)
* @description touch 事件
* https://javascript.info/pointer-events
*/
let lastPoint = {
x: 0,
y: 0
}
function handleStart(evt: PointerEvent) {
// evt.preventDefault();
let el = document.getElementById("canvas") as HTMLCanvasElement;
let ctx = el.getContext("2d") as CanvasRenderingContext2D;
console.log('point start', evt.offsetY);
lastPoint = {
x: evt.offsetX,
y: evt.offsetY
};
ctx.beginPath();
ctx.arc(evt.offsetX, evt.offsetY, 4, 0, 2 * Math.PI, false); // a circle at the start
ctx.fillStyle = '#222';
ctx.fill();
}
function handleMove(evt: PointerEvent) {
console.log('point move', evt.offsetY);
// evt.preventDefault();
let el = document.getElementById("canvas") as HTMLCanvasElement;
let ctx = el.getContext("2d");
ctx.beginPath();
ctx.moveTo(lastPoint.x, lastPoint.y);
ctx.lineTo(evt.offsetX, evt.offsetY);
ctx.lineWidth = 4;
ctx.strokeStyle = '#222';
ctx.stroke();
lastPoint = {
x: evt.offsetX,
y: evt.offsetY
};
}
function handleEnd(evt: PointerEvent) {
// evt.preventDefault();
console.log('point stop', evt.offsetY);
let el = document.getElementById("canvas") as HTMLCanvasElement;
let ctx = el.getContext("2d");
ctx.lineWidth = 4;
ctx.fillStyle = '#222';
ctx.beginPath();
ctx.moveTo(lastPoint.x, lastPoint.y);
ctx.lineTo(evt.offsetX, evt.offsetY);
ctx.fillRect(evt.offsetX - 4, evt.offsetY - 4, 8, 8); // and a square at the end
}
function startup() {
const width = 320;
const height = 1600;
let el = document.getElementById("canvas") as HTMLCanvasElement;
const dpr = window.devicePixelRatio;
el.width = width * dpr;
el.height = height * dpr;
el.style.width = width + 'px';
el.style.height = height + 'px';
let ctx = el.getContext("2d") as CanvasRenderingContext2D;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
el.addEventListener('pointerdown', handleStart, false);
el.addEventListener('pointerup', handleEnd, false);
el.addEventListener('pointermove', handleMove, false);
console.log('canvas pageY', el.offsetTop)
}
startup();