diff --git a/src/content/Backgrounds/Hyperspeed/Hyperspeed.jsx b/src/content/Backgrounds/Hyperspeed/Hyperspeed.jsx index 7377c54d..34ae53c4 100644 --- a/src/content/Backgrounds/Hyperspeed/Hyperspeed.jsx +++ b/src/content/Backgrounds/Hyperspeed/Hyperspeed.jsx @@ -413,6 +413,10 @@ const Hyperspeed = ({ effectOptions = { this.setSize = this.setSize.bind(this); this.onMouseDown = this.onMouseDown.bind(this); this.onMouseUp = this.onMouseUp.bind(this); + + this.onTouchStart = this.onTouchStart.bind(this); + this.onTouchEnd = this.onTouchEnd.bind(this); + this.onContextMenu = this.onContextMenu.bind(this); window.addEventListener("resize", this.onWindowResize.bind(this)); } @@ -500,6 +504,12 @@ const Hyperspeed = ({ effectOptions = { this.container.addEventListener("mousedown", this.onMouseDown); this.container.addEventListener("mouseup", this.onMouseUp); this.container.addEventListener("mouseout", this.onMouseUp); + + this.container.addEventListener("touchstart", this.onTouchStart, { passive: true }); + this.container.addEventListener("touchend", this.onTouchEnd, { passive: true }); + this.container.addEventListener("touchcancel", this.onTouchEnd, { passive: true }); + + this.container.addEventListener("contextmenu", this.onContextMenu); this.tick(); } @@ -516,6 +526,22 @@ const Hyperspeed = ({ effectOptions = { this.speedUpTarget = 0; } + onTouchStart(ev) { + if (this.options.onSpeedUp) this.options.onSpeedUp(ev); + this.fovTarget = this.options.fovSpeedUp; + this.speedUpTarget = this.options.speedUp; + } + + onTouchEnd(ev) { + if (this.options.onSlowDown) this.options.onSlowDown(ev); + this.fovTarget = this.options.fov; + this.speedUpTarget = 0; + } + + onContextMenu(ev) { + ev.preventDefault(); + } + update(delta) { let lerpPercentage = Math.exp(-(-60 * Math.log2(1 - 0.1)) * delta); this.speedUp += lerp( @@ -584,6 +610,11 @@ const Hyperspeed = ({ effectOptions = { this.container.removeEventListener("mousedown", this.onMouseDown); this.container.removeEventListener("mouseup", this.onMouseUp); this.container.removeEventListener("mouseout", this.onMouseUp); + + this.container.removeEventListener("touchstart", this.onTouchStart); + this.container.removeEventListener("touchend", this.onTouchEnd); + this.container.removeEventListener("touchcancel", this.onTouchEnd); + this.container.removeEventListener("contextmenu", this.onContextMenu); } } diff --git a/src/tailwind/Backgrounds/Hyperspeed/Hyperspeed.jsx b/src/tailwind/Backgrounds/Hyperspeed/Hyperspeed.jsx index 5e32d7dc..202c50a7 100644 --- a/src/tailwind/Backgrounds/Hyperspeed/Hyperspeed.jsx +++ b/src/tailwind/Backgrounds/Hyperspeed/Hyperspeed.jsx @@ -412,6 +412,10 @@ const Hyperspeed = ({ effectOptions = { this.setSize = this.setSize.bind(this); this.onMouseDown = this.onMouseDown.bind(this); this.onMouseUp = this.onMouseUp.bind(this); + + this.onTouchStart = this.onTouchStart.bind(this); + this.onTouchEnd = this.onTouchEnd.bind(this); + this.onContextMenu = this.onContextMenu.bind(this); window.addEventListener("resize", this.onWindowResize.bind(this)); } @@ -500,6 +504,11 @@ const Hyperspeed = ({ effectOptions = { this.container.addEventListener("mouseup", this.onMouseUp); this.container.addEventListener("mouseout", this.onMouseUp); + this.container.addEventListener("touchstart", this.onTouchStart, { passive: true }); + this.container.addEventListener("touchend", this.onTouchEnd, { passive: true }); + this.container.addEventListener("touchcancel", this.onTouchEnd, { passive: true }); + this.container.addEventListener("contextmenu", this.onContextMenu); + this.tick(); } @@ -514,6 +523,22 @@ const Hyperspeed = ({ effectOptions = { this.fovTarget = this.options.fov; this.speedUpTarget = 0; } + + onTouchStart(ev) { + if (this.options.onSpeedUp) this.options.onSpeedUp(ev); + this.fovTarget = this.options.fovSpeedUp; + this.speedUpTarget = this.options.speedUp; + } + + onTouchEnd(ev) { + if (this.options.onSlowDown) this.options.onSlowDown(ev); + this.fovTarget = this.options.fov; + this.speedUpTarget = 0; + } + + onContextMenu(ev) { + ev.preventDefault(); + } update(delta) { let lerpPercentage = Math.exp(-(-60 * Math.log2(1 - 0.1)) * delta); @@ -582,6 +607,11 @@ const Hyperspeed = ({ effectOptions = { this.container.removeEventListener("mousedown", this.onMouseDown); this.container.removeEventListener("mouseup", this.onMouseUp); this.container.removeEventListener("mouseout", this.onMouseUp); + + this.container.removeEventListener("touchstart", this.onTouchStart); + this.container.removeEventListener("touchend", this.onTouchEnd); + this.container.removeEventListener("touchcancel", this.onTouchEnd); + this.container.removeEventListener("contextmenu", this.onContextMenu); } } diff --git a/src/ts-default/Backgrounds/Hyperspeed/Hyperspeed.tsx b/src/ts-default/Backgrounds/Hyperspeed/Hyperspeed.tsx index e3b406a0..2e5d5d2c 100644 --- a/src/ts-default/Backgrounds/Hyperspeed/Hyperspeed.tsx +++ b/src/ts-default/Backgrounds/Hyperspeed/Hyperspeed.tsx @@ -33,8 +33,8 @@ interface Colors { } interface HyperspeedOptions { - onSpeedUp?: (ev: MouseEvent) => void; - onSlowDown?: (ev: MouseEvent) => void; + onSpeedUp?: (ev: MouseEvent | TouchEvent) => void; + onSlowDown?: (ev: MouseEvent | TouchEvent) => void; distortion?: string | Distortion; length: number; roadWidth: number; @@ -1074,6 +1074,10 @@ class App { this.onMouseDown = this.onMouseDown.bind(this); this.onMouseUp = this.onMouseUp.bind(this); + this.onTouchStart = this.onTouchStart.bind(this); + this.onTouchEnd = this.onTouchEnd.bind(this); + this.onContextMenu = this.onContextMenu.bind(this); + window.addEventListener("resize", this.onWindowResize.bind(this)); } @@ -1162,6 +1166,11 @@ class App { this.container.addEventListener("mousedown", this.onMouseDown); this.container.addEventListener("mouseup", this.onMouseUp); this.container.addEventListener("mouseout", this.onMouseUp); + + this.container.addEventListener("touchstart", this.onTouchStart, { passive: true }); + this.container.addEventListener("touchend", this.onTouchEnd, { passive: true }); + this.container.addEventListener("touchcancel", this.onTouchEnd, { passive: true }); + this.container.addEventListener("contextmenu", this.onContextMenu); this.tick(); } @@ -1177,6 +1186,22 @@ class App { this.fovTarget = this.options.fov; this.speedUpTarget = 0; } + + onTouchStart(ev: TouchEvent) { + if (this.options.onSpeedUp) this.options.onSpeedUp(ev); + this.fovTarget = this.options.fovSpeedUp; + this.speedUpTarget = this.options.speedUp; + } + + onTouchEnd(ev: TouchEvent) { + if (this.options.onSlowDown) this.options.onSlowDown(ev); + this.fovTarget = this.options.fov; + this.speedUpTarget = 0; + } + + onContextMenu(ev: MouseEvent) { + ev.preventDefault(); + } update(delta: number) { const lerpPercentage = Math.exp(-(-60 * Math.log2(1 - 0.1)) * delta); @@ -1243,6 +1268,11 @@ class App { this.container.removeEventListener("mousedown", this.onMouseDown); this.container.removeEventListener("mouseup", this.onMouseUp); this.container.removeEventListener("mouseout", this.onMouseUp); + + this.container.removeEventListener("touchstart", this.onTouchStart); + this.container.removeEventListener("touchend", this.onTouchEnd); + this.container.removeEventListener("touchcancel", this.onTouchEnd); + this.container.removeEventListener("contextmenu", this.onContextMenu); } } diff --git a/src/ts-tailwind/Backgrounds/Hyperspeed/Hyperspeed.tsx b/src/ts-tailwind/Backgrounds/Hyperspeed/Hyperspeed.tsx index f5e351df..d9a292c8 100644 --- a/src/ts-tailwind/Backgrounds/Hyperspeed/Hyperspeed.tsx +++ b/src/ts-tailwind/Backgrounds/Hyperspeed/Hyperspeed.tsx @@ -31,8 +31,8 @@ interface Colors { } interface HyperspeedOptions { - onSpeedUp?: (ev: MouseEvent) => void; - onSlowDown?: (ev: MouseEvent) => void; + onSpeedUp?: (ev: MouseEvent | TouchEvent) => void; + onSlowDown?: (ev: MouseEvent | TouchEvent) => void; distortion?: string | Distortion; length: number; roadWidth: number; @@ -1075,7 +1075,11 @@ class App { this.setSize = this.setSize.bind(this); this.onMouseDown = this.onMouseDown.bind(this); this.onMouseUp = this.onMouseUp.bind(this); - + + this.onTouchStart = this.onTouchStart.bind(this); + this.onTouchEnd = this.onTouchEnd.bind(this); + this.onContextMenu = this.onContextMenu.bind(this); + window.addEventListener("resize", this.onWindowResize.bind(this)); } @@ -1165,6 +1169,17 @@ class App { this.container.addEventListener("mouseup", this.onMouseUp); this.container.addEventListener("mouseout", this.onMouseUp); + this.container.addEventListener("touchstart", this.onTouchStart, { + passive: true, + }); + this.container.addEventListener("touchend", this.onTouchEnd, { + passive: true, + }); + this.container.addEventListener("touchcancel", this.onTouchEnd, { + passive: true, + }); + this.container.addEventListener("contextmenu", this.onContextMenu); + this.tick(); } @@ -1180,6 +1195,22 @@ class App { this.speedUpTarget = 0; } + onTouchStart(ev: TouchEvent) { + if (this.options.onSpeedUp) this.options.onSpeedUp(ev); + this.fovTarget = this.options.fovSpeedUp; + this.speedUpTarget = this.options.speedUp; + } + + onTouchEnd(ev: TouchEvent) { + if (this.options.onSlowDown) this.options.onSlowDown(ev); + this.fovTarget = this.options.fov; + this.speedUpTarget = 0; + } + + onContextMenu(ev: MouseEvent) { + ev.preventDefault(); + } + update(delta: number) { const lerpPercentage = Math.exp(-(-60 * Math.log2(1 - 0.1)) * delta); this.speedUp += lerp( @@ -1239,12 +1270,17 @@ class App { if (this.scene) { this.scene.clear(); } - + window.removeEventListener("resize", this.onWindowResize.bind(this)); if (this.container) { this.container.removeEventListener("mousedown", this.onMouseDown); this.container.removeEventListener("mouseup", this.onMouseUp); this.container.removeEventListener("mouseout", this.onMouseUp); + + this.container.removeEventListener("touchstart", this.onTouchStart); + this.container.removeEventListener("touchend", this.onTouchEnd); + this.container.removeEventListener("touchcancel", this.onTouchEnd); + this.container.removeEventListener("contextmenu", this.onContextMenu); } }