From 7765fb066a31159033240009ff8ea9fbc4e01e2c Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sat, 15 Oct 2022 16:27:11 +0200 Subject: [PATCH] canvas: attempt to poll touch azimuth, altitude and force on iOS that otherwise don't trigger onTouch as expected --- src/client/events/utils.js | 18 ++++++++++++- src/client/widgets/pads/canvas.js | 43 ++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/client/events/utils.js b/src/client/events/utils.js index d8cb5eba..098eb62d 100644 --- a/src/client/events/utils.js +++ b/src/client/events/utils.js @@ -1,4 +1,6 @@ const Touch = window.Touch || class Touch {} +const iOS = require('../ui/ios') + // const cssTransformCoords = require('./transform-coords') @@ -6,7 +8,7 @@ module.exports = { fix: function(e) { - return { + var ev = { target: e.target, firstTarget: null, @@ -38,6 +40,20 @@ module.exports = { azimuthAngle: e.azimuthAngle, touchType: e.touchType } + + if (iOS) { + // iOS-specific event properties + ev.touchType = e.touchType + // iOS-pollable event properties + for (var p of ['altitudeAngle', 'azimuthAngle', 'force']) { + Object.defineProperty(ev, p, { + enumerable: true, + value: ()=>e[p] + }) + } + } + + return ev }, normalizeDragEvent: function(event, previousEvent) { diff --git a/src/client/widgets/pads/canvas.js b/src/client/widgets/pads/canvas.js index d90e9ae6..f9dec6ff 100644 --- a/src/client/widgets/pads/canvas.js +++ b/src/client/widgets/pads/canvas.js @@ -1,7 +1,8 @@ var Canvas = require('../common/canvas'), Script = require('../scripts/script'), widgetManager = require('../../managers/widgets'), - html = require('nanohtml') + html = require('nanohtml'), + iOS = require('../../ui/ios') class CanvasWidget extends Canvas { @@ -114,16 +115,49 @@ class CanvasWidget extends Canvas { this.batchDraw() } + if (iOS) { + this.iosTouchCache = {} + this.iosTouchPolls = {} + } + this.on('draginit',(e)=>{ touchCb(e, 'start') + + if (iOS) { + // start polling extra touch inforamtions + this.iosTouchPolls[e.pointerId] = setInterval(()=>{ + for (var p of ['altitudeAngle', 'azimuthAngle', 'force']) { + // if polled value has changed, retrig last move event + if (e[p] !== this.iosTouchCache[e.pointerId][p]) { + // reset movement data since this is only relevant when + // the pointer doesn't move and but force does + this.iosTouchCache[e.pointerId].movementX = 0 + this.iosTouchCache[e.pointerId].movementY = 0 + // don't manually update polled data, its accessed + // with a getter that pulls it from the original event + this.trigger('drag', e) + } + } + }, 1000 / CANVAS_FRAMERATE) + } }, {element: this.container, multitouch: true}) this.on('drag',(e)=>{ touchCb(e, 'move') + if (iOS) { + // cache touch inforamtions + this.iosTouchCache[e.pointerId] === e + } }, {element: this.container, multitouch: true}) this.on('dragend',(e)=>{ touchCb(e, 'stop') + if (iOS) { + // stop polling extra touch inforamtions + clearInterval(this.iosTouchPolls[e.pointerId]) + delete this.iosTouchPolls[e.ointerId] + delete this.iosTouchCache[e.ointerId] + } }, {element: this.container, multitouch: true}) } @@ -192,6 +226,13 @@ class CanvasWidget extends Canvas { onRemove() { clearInterval(this.drawInterval) + + if (iOS) { + for (var k in this.iosTouchPolls) { + clearInterval(this.iosTouchPolls[k]) + } + } + super.onRemove() }