|
7 | 7 | */
|
8 | 8 | 'use strict';
|
9 | 9 |
|
10 |
| -/* global Draggable */ |
11 |
| - |
12 | 10 | /*
|
13 | 11 | * The painting interface uses a square array of elevations. As you
|
14 | 12 | * drag the mouse it will paint filled circles into the elevation map,
|
@@ -198,42 +196,78 @@ for (let control of controls) {
|
198 | 196 | displayCurrentTool();
|
199 | 197 |
|
200 | 198 |
|
201 |
| -const output = document.getElementById('mapgen4'); |
202 |
| -new Draggable({ |
203 |
| - // TODO: replace with pointer events, now that they're widely supported |
204 |
| - el: output, |
205 |
| - start(event) { |
206 |
| - this.timestamp = Date.now(); |
| 199 | +function setUpPaintEventHandling() { |
| 200 | + const el = document.getElementById('mapgen4'); |
| 201 | + let dragging = false; |
| 202 | + let timestamp = 0; |
| 203 | + |
| 204 | + /** |
| 205 | + * @param {PointerEvent} event |
| 206 | + */ |
| 207 | + function start(event) { |
| 208 | + if (event.button !== 0) return; // left button only |
| 209 | + el.setPointerCapture(event.pointerId); |
| 210 | + |
| 211 | + dragging = true; |
| 212 | + timestamp = Date.now(); |
207 | 213 | currentStroke.time.fill(0);
|
208 | 214 | currentStroke.strength.fill(0);
|
209 | 215 | currentStroke.previousElevation.set(heightMap.elevation);
|
210 |
| - this.drag(event); |
211 |
| - }, |
212 |
| - drag(event) { |
| 216 | + move(event); |
| 217 | + } |
| 218 | + |
| 219 | + function end(_event) { |
| 220 | + dragging = false; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * @param {PointerEvent} event |
| 225 | + */ |
| 226 | + function move(event) { |
| 227 | + if (!dragging) return; |
| 228 | + |
213 | 229 | const nowMs = Date.now();
|
214 |
| - let coords = [event.x / output.clientWidth, |
215 |
| - event.y / output.clientHeight]; |
| 230 | + const bounds = el.getBoundingClientRect(); |
| 231 | + let coords = [ |
| 232 | + (event.x - bounds.left) / bounds.width, |
| 233 | + (event.y - bounds.top) / bounds.height, |
| 234 | + ]; |
216 | 235 | coords = exported.screenToWorldCoords(coords);
|
217 | 236 | let brushSize = SIZES[currentSize];
|
218 |
| - if (event.touch && event.touch.force > 0) { |
219 |
| - // Apple Stylus |
220 |
| - let radius = Math.sqrt(event.touch.force); |
| 237 | + if (event.pointerType === 'pen' && event.pressure !== 0.5) { |
| 238 | + // Pointer Event spec says 0.5 sent when pen does not |
| 239 | + // support pressure; I primarily added this for Apple |
| 240 | + // Pencil but haven't tested on others. I want pressure |
| 241 | + // 0.25 to correspond to "regular" pressure for the given |
| 242 | + // brush size, so radius should be 1.0. I am *not* |
| 243 | + // currently supporting Macbook pressure-sensitive |
| 244 | + // touchpads, which don't show up under Pointer Events. |
| 245 | + // https://developer.mozilla.org/en-US/docs/Web/API/Force_Touch_events |
| 246 | + let radius = 2 * Math.sqrt(event.pressure); |
221 | 247 | brushSize = {
|
222 | 248 | key: brushSize.key,
|
223 | 249 | innerRadius: Math.max(1, brushSize.innerRadius * radius),
|
224 | 250 | outerRadius: Math.max(2, brushSize.outerRadius * radius),
|
225 | 251 | rate: brushSize.rate,
|
226 | 252 | };
|
227 | 253 | }
|
228 |
| - if (event.raw && event.raw.shiftKey) { |
| 254 | + if (event.shiftKey) { |
229 | 255 | // Hold down shift to paint slowly
|
230 | 256 | brushSize = {...brushSize, rate: brushSize.rate/4};
|
231 | 257 | }
|
232 |
| - heightMap.paintAt(TOOLS[currentTool], coords[0], coords[1], brushSize, nowMs - this.timestamp); |
233 |
| - this.timestamp = nowMs; |
| 258 | + heightMap.paintAt(TOOLS[currentTool], coords[0], coords[1], |
| 259 | + brushSize, nowMs - timestamp); |
| 260 | + timestamp = nowMs; |
234 | 261 | exported.onUpdate();
|
235 |
| - }, |
236 |
| -}); |
| 262 | + } |
| 263 | + |
| 264 | + el.addEventListener('pointerdown', start); |
| 265 | + el.addEventListener('pointerup', end); |
| 266 | + el.addEventListener('pointercancel', end); |
| 267 | + el.addEventListener('pointermove', move) |
| 268 | + el.addEventListener('touchstart', (e) => e.preventDefault()); // prevent scroll |
| 269 | +} |
| 270 | +setUpPaintEventHandling(); |
237 | 271 |
|
238 | 272 |
|
239 | 273 |
|
|
0 commit comments