This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc-speed-surface.js
184 lines (151 loc) · 4.44 KB
/
sc-speed-surface.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
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
import { html, css } from 'lit';
import ScElement from './ScElement.js';
function getTime() {
if (window.performance) {
return window.performance.now();
} else {
return Date.now();
}
}
class ScSpeedSurface extends ScElement {
static get properties() {
return {
width: {
type: Number,
},
height: {
type: Number,
},
}
}
static get styles() {
return css`
:host {
display: inline-block;
box-sizing: border-box;
};
`;
}
constructor() {
super();
this.width = 200;
this.height = 200;
this.pointerId = null;
this.lastPointer = null;
this.lastTime = null;
this.mouseMove = this.mouseMove.bind(this);
this.mouseUp = this.mouseUp.bind(this);
this.touchStart = this.touchStart.bind(this);
this.touchMove = this.touchMove.bind(this);
this.touchEnd = this.touchEnd.bind(this);
this.propagateValues = this.propagateValues.bind(this);
this.rafId = null;
}
render() {
return html`
<div
style="
width: ${this.width}px;
height: ${this.height}px;
"
@mousedown="${this.mouseDown}"
@touchstart="${{
handleEvent: this.touchStart,
passive: false,
}}"
@contextmenu="${this._preventContextMenu}"
></div>
`;
}
mouseDown(e) {
window.addEventListener('mousemove', this.mouseMove);
window.addEventListener('mouseup', this.mouseUp);
this._requestUserSelectNoneOnBody();
this.pointerId = 'mouse';
this.lastTime = getTime();
this.lastPointer = e;
}
mouseMove(e) {
this.requestPropagateValues(e);
}
mouseUp(e) {
window.removeEventListener('mousemove', this.mouseMove);
window.removeEventListener('mouseup', this.mouseUp);
this._cancelUserSelectNoneOnBody();
this.requestPropagateValues(e);
// we want to have { dx: 0, dy: 0 } on mouse up,
// with 20ms, we should be in the next requestAnimationFrame
setTimeout(() => {
this.pointerId = null;
this.requestPropagateValues(e);
}, 20);
}
// @eventOptions({ passive: false })
touchStart(e) {
e.preventDefault(); // prevent scrolling
if (this.pointerId === null) {
const touch = e.changedTouches[0];
this.pointerId = touch.identifier;
window.addEventListener('touchmove', this.touchMove, { passive: false });
window.addEventListener('touchend', this.touchEnd);
window.addEventListener('touchcancel', this.touchEnd);
this._requestUserSelectNoneOnBody();
this.lastTime = getTime();
this.lastPointer = touch;
}
}
touchMove(e) {
e.preventDefault(); // prevent scrolling
for (let touch of e.changedTouches) {
if (touch.identifier === this.pointerId) {
this.requestPropagateValues(touch);
}
}
}
touchEnd(e) {
for (let touch of e.changedTouches) {
if (touch.identifier === this.pointerId) {
window.removeEventListener('touchmove', this.touchMove);
window.removeEventListener('touchend', this.touchEnd);
window.removeEventListener('touchcancel', this.touchEnd);
this._cancelUserSelectNoneOnBody();
this.requestPropagateValues(touch);
// we want to have { dx: 0, dy: 0 } on mouse up,
// with 20ms, we should be in the next requestAnimationFrame
setTimeout(() => {
this.pointerId = null;
this.requestPropagateValues(touch);
}, 20);
}
}
}
requestPropagateValues(e) {
window.cancelAnimationFrame(this.rafId);
this.rafId = window.requestAnimationFrame(() => this.propagateValues(e));
}
// return speed in px / ms
propagateValues(e) {
const lastX = this.lastPointer.screenX;
const lastY = this.lastPointer.screenY;
const x = e.screenX;
const y = e.screenY;
const now = getTime();
const dt = (this.lastTime - now);
const dx = (x - lastX) / dt;
const dy = (y - lastY) / dt;
this.lastTime = now;
this.lastPointer = e;
// propagate outside the shadow dom boudaries
// cf. https://lit-element.polymer-project.org/guide/events#custom-events
const event = new CustomEvent('input', {
bubbles: true,
composed: true,
detail: { dx, dy, pointerId: this.pointerId },
});
this.dispatchEvent(event);
}
}
if (customElements.get('sc-speed-surface') === undefined) {
customElements.define('sc-speed-surface', ScSpeedSurface);
}
export default ScSpeedSurface;