|
| 1 | +import { Layout, LayoutAxis } from "plotly.js"; |
| 2 | + |
| 3 | +type PlotlyEl = Plotly.PlotlyHTMLElement & { |
| 4 | + data: (Plotly.PlotData & { entity: string })[]; |
| 5 | + layout: Plotly.Layout; |
| 6 | +}; |
| 7 | +const zoomedRange = (axis: Partial<LayoutAxis>, zoom: number) => { |
| 8 | + if (!axis || !axis.range) return undefined; |
| 9 | + const center = (+axis.range[1] + +axis.range[0]) / 2; |
| 10 | + if (isNaN(center)) return undefined; // probably a categorical axis. Don't zoom |
| 11 | + const radius = (+axis.range[1] - +axis.range[0]) / zoom / 2; |
| 12 | + return [center - radius, center + radius]; |
| 13 | +}; |
| 14 | +const ONE_FINGER_DOUBLE_TAP_ZOOM_MS_THRESHOLD = 250; |
| 15 | +export class TouchController { |
| 16 | + isEnabled = true; |
| 17 | + lastTouches?: TouchList; |
| 18 | + lastSingleTouchTimestamp = 0; |
| 19 | + elRect?: DOMRect; |
| 20 | + el: PlotlyEl; |
| 21 | + onZoomStart: Function; |
| 22 | + onZoom: Function; |
| 23 | + onZoomEnd: Function; |
| 24 | + state: "one finger" | "two fingers" | "idle" = "idle"; |
| 25 | + constructor(param: { |
| 26 | + el: PlotlyEl; |
| 27 | + onZoomStart: Function; |
| 28 | + onZoom: (layout: Partial<Layout>) => any; |
| 29 | + onZoomEnd: Function; |
| 30 | + }) { |
| 31 | + this.el = param.el; |
| 32 | + this.onZoom = param.onZoom; |
| 33 | + this.onZoomStart = param.onZoomStart; |
| 34 | + this.onZoomEnd = param.onZoomEnd; |
| 35 | + } |
| 36 | + disconnect() { |
| 37 | + this.el.removeEventListener("touchmove", this.onTouchMove); |
| 38 | + this.el.removeEventListener("touchstart", this.onTouchStart); |
| 39 | + this.el.removeEventListener("touchend", this.onTouchEnd); |
| 40 | + } |
| 41 | + connect() { |
| 42 | + this.el.addEventListener("touchmove", this.onTouchMove, { |
| 43 | + capture: true, |
| 44 | + }); |
| 45 | + this.el.addEventListener("touchstart", this.onTouchStart, { |
| 46 | + capture: true, |
| 47 | + }); |
| 48 | + this.el.addEventListener("touchend", this.onTouchEnd, { |
| 49 | + capture: true, |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + onTouchStart = async (e: TouchEvent) => { |
| 54 | + if (!this.isEnabled) return; |
| 55 | + const stateWas = this.state; |
| 56 | + this.state = "idle"; |
| 57 | + if (e.touches.length == 1) { |
| 58 | + const now = Date.now(); |
| 59 | + if ( |
| 60 | + now - this.lastSingleTouchTimestamp < |
| 61 | + ONE_FINGER_DOUBLE_TAP_ZOOM_MS_THRESHOLD |
| 62 | + ) { |
| 63 | + e.stopPropagation(); |
| 64 | + e.stopImmediatePropagation(); |
| 65 | + this.state = "one finger"; |
| 66 | + this.lastTouches = e.touches; |
| 67 | + this.elRect = this.el.getBoundingClientRect(); |
| 68 | + } else { |
| 69 | + this.lastSingleTouchTimestamp = now; |
| 70 | + } |
| 71 | + } else if (e.touches.length == 2) { |
| 72 | + this.state = "two fingers"; |
| 73 | + this.lastTouches = e.touches; |
| 74 | + } |
| 75 | + if (stateWas === "idle" && stateWas !== this.state) { |
| 76 | + this.onZoomStart(); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + onTouchMove = async (e: TouchEvent) => { |
| 81 | + if (!this.isEnabled) return; |
| 82 | + |
| 83 | + if (e.touches.length === 1 && this.state === "one finger") |
| 84 | + this.handleSingleFingerZoom(e); |
| 85 | + if (e.touches.length === 2 && this.state === "two fingers") |
| 86 | + this.handleTwoFingersZoom(e); |
| 87 | + }; |
| 88 | + async handleSingleFingerZoom(e: TouchEvent) { |
| 89 | + e.preventDefault(); |
| 90 | + e.stopPropagation(); |
| 91 | + e.stopImmediatePropagation(); |
| 92 | + const ts_old = this.lastTouches!; |
| 93 | + this.lastTouches = e.touches; |
| 94 | + const ts_new = e.touches; |
| 95 | + const height = this.elRect?.height || 500; |
| 96 | + const dist = (ts_new[0].clientY - ts_old[0].clientY) / height; |
| 97 | + let zoom = 1; |
| 98 | + if (dist > 0) zoom = 1 + dist * 4; |
| 99 | + if (dist < 0) zoom = 1 / (1 - dist * 4); |
| 100 | + await this.handleZoom(zoom); |
| 101 | + } |
| 102 | + async handleTwoFingersZoom(e: TouchEvent) { |
| 103 | + e.preventDefault(); |
| 104 | + e.stopPropagation(); |
| 105 | + e.stopImmediatePropagation(); |
| 106 | + const ts_old = this.lastTouches!; |
| 107 | + this.lastTouches = e.touches; |
| 108 | + const ts_new = e.touches; |
| 109 | + const spread_old = Math.sqrt( |
| 110 | + (ts_old[0].clientX - ts_old[1].clientX) ** 2 + |
| 111 | + (ts_old[0].clientY - ts_old[1].clientY) ** 2 |
| 112 | + ); |
| 113 | + const spread_new = Math.sqrt( |
| 114 | + (ts_new[0].clientX - ts_new[1].clientX) ** 2 + |
| 115 | + (ts_new[0].clientY - ts_new[1].clientY) ** 2 |
| 116 | + ); |
| 117 | + await this.handleZoom(spread_new / spread_old); |
| 118 | + } |
| 119 | + async handleZoom(zoom: number) { |
| 120 | + const oldLayout = this.el.layout; |
| 121 | + const layout = {}; |
| 122 | + |
| 123 | + layout["xaxis.range"] = zoomedRange(oldLayout.xaxis, zoom); |
| 124 | + layout["yaxis.range"] = zoomedRange(oldLayout.yaxis, zoom); |
| 125 | + for (let i = 2; i < 31; i++) { |
| 126 | + layout[`xaxis${i}.range`] = zoomedRange(oldLayout[`xaxis${i}`], zoom); |
| 127 | + layout[`yaxis${i}.range`] = zoomedRange(oldLayout[`yaxis${i}`], zoom); |
| 128 | + } |
| 129 | + this.onZoom(layout); |
| 130 | + } |
| 131 | + |
| 132 | + onTouchEnd = () => { |
| 133 | + if (!this.isEnabled) return; |
| 134 | + |
| 135 | + if (this.state !== "idle") { |
| 136 | + this.onZoomEnd(); |
| 137 | + this.state = "idle"; |
| 138 | + } |
| 139 | + }; |
| 140 | +} |
0 commit comments