forked from shuding/apple-pencil-safari-api-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (134 loc) · 4.15 KB
/
index.js
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
const $force = document.querySelectorAll('#force')[0]
const $touches = document.querySelectorAll('#touches')[0]
const canvas = document.querySelectorAll('canvas')[0]
const context = canvas.getContext('2d')
let lineWidth = 0
let isMousedown = false
let points = []
canvas.width = window.innerWidth * 2
canvas.height = window.innerHeight * 2
const strokeHistory = []
const requestIdleCallback = window.requestIdleCallback || function (fn) { setTimeout(fn, 1) };
/**
* This function takes in an array of points and draws them onto the canvas.
* @param {array} stroke array of points to draw on the canvas
* @return {void}
*/
function drawOnCanvas (stroke) {
context.strokeStyle = 'black'
context.lineCap = 'round'
context.lineJoin = 'round'
const l = stroke.length - 1
if (stroke.length >= 3) {
const xc = (stroke[l].x + stroke[l - 1].x) / 2
const yc = (stroke[l].y + stroke[l - 1].y) / 2
context.lineWidth = stroke[l - 1].lineWidth
context.quadraticCurveTo(stroke[l - 1].x, stroke[l - 1].y, xc, yc)
context.stroke()
context.beginPath()
context.moveTo(xc, yc)
} else {
const point = stroke[l];
context.lineWidth = point.lineWidth
context.strokeStyle = point.color
context.beginPath()
context.moveTo(point.x, point.y)
context.stroke()
}
}
/**
* Remove the previous stroke from history and repaint the entire canvas based on history
* @return {void}
*/
function undoDraw () {
strokeHistory.pop()
context.clearRect(0, 0, canvas.width, canvas.height)
strokeHistory.map(function (stroke) {
if (strokeHistory.length === 0) return
context.beginPath()
let strokePath = [];
stroke.map(function (point) {
strokePath.push(point)
drawOnCanvas(strokePath)
})
})
}
for (const ev of ["touchstart", "mousedown"]) {
canvas.addEventListener(ev, function (e) {
let pressure = 0.1;
let x, y;
if (e.touches && e.touches[0] && typeof e.touches[0]["force"] !== "undefined") {
if (e.touches[0]["force"] > 0) {
pressure = e.touches[0]["force"]
}
x = e.touches[0].pageX * 2
y = e.touches[0].pageY * 2
} else {
pressure = 1.0
x = e.pageX * 2
y = e.pageY * 2
}
isMousedown = true
lineWidth = Math.log(pressure + 1) * 40
context.lineWidth = lineWidth// pressure * 50;
points.push({ x, y, lineWidth })
drawOnCanvas(points)
})
}
for (const ev of ['touchmove', 'mousemove']) {
canvas.addEventListener(ev, function (e) {
if (!isMousedown) return
e.preventDefault()
let pressure = 0.1
let x, y
if (e.touches && e.touches[0] && typeof e.touches[0]["force"] !== "undefined") {
if (e.touches[0]["force"] > 0) {
pressure = e.touches[0]["force"]
}
x = e.touches[0].pageX * 2
y = e.touches[0].pageY * 2
} else {
pressure = 1.0
x = e.pageX * 2
y = e.pageY * 2
}
// smoothen line width
lineWidth = (Math.log(pressure + 1) * 40 * 0.2 + lineWidth * 0.8)
points.push({ x, y, lineWidth })
drawOnCanvas(points);
requestIdleCallback(() => {
$force.textContent = 'force = ' + pressure
const touch = e.touches ? e.touches[0] : null
if (touch) {
$touches.innerHTML = `
touchType = ${touch.touchType} ${touch.touchType === 'direct' ? '👆' : '✍️'} <br/>
radiusX = ${touch.radiusX} <br/>
radiusY = ${touch.radiusY} <br/>
rotationAngle = ${touch.rotationAngle} <br/>
altitudeAngle = ${touch.altitudeAngle} <br/>
azimuthAngle = ${touch.azimuthAngle} <br/>
`
}
})
})
}
for (const ev of ['touchend', 'touchleave', 'mouseup']) {
canvas.addEventListener(ev, function (e) {
let pressure = 0.1;
let x, y;
if (e.touches && e.touches[0] && typeof e.touches[0]["force"] !== "undefined") {
if (e.touches[0]["force"] > 0) {
pressure = e.touches[0]["force"]
}
x = e.touches[0].pageX * 2
y = e.touches[0].pageY * 2
} else {
pressure = 1.0
x = e.pageX * 2
y = e.pageY * 2
}
isMousedown = false
requestIdleCallback(function () { strokeHistory.push([...points]); points = []})
lineWidth = 0
})
};