diff --git a/src/extras/PointerLens/PointerLens.js b/src/extras/PointerLens/PointerLens.js index b291587e07..1c71e6f6dd 100644 --- a/src/extras/PointerLens/PointerLens.js +++ b/src/extras/PointerLens/PointerLens.js @@ -53,13 +53,10 @@ export class PointerLens { this.scene = this.viewer.scene; this._lensCursorDiv = document.createElement('div'); - this.viewer.scene.canvas.canvas.parentNode.insertBefore(this._lensCursorDiv, this.viewer.scene.canvas.canvas); - this._lensCursorDiv.style.background = "pink"; - this._lensCursorDiv.style.border = "2px solid red"; - this._lensCursorDiv.style.borderRadius = "20px"; - this._lensCursorDiv.style.width = "10px"; - this._lensCursorDiv.style.height = "10px"; - this._lensCursorDiv.style.margin = "-200px -200px"; + this._lensParams = { canvasSize: 300, cursorBorder: 2, cursorSize: 10 }; + this._lensCursorDiv.style.borderRadius = "50%"; + this._lensCursorDiv.style.width = this._lensParams.cursorSize + "px"; + this._lensCursorDiv.style.height = this._lensParams.cursorSize + "px"; this._lensCursorDiv.style.zIndex = "100000"; this._lensCursorDiv.style.position = "absolute"; this._lensCursorDiv.style.pointerEvents = "none"; @@ -72,8 +69,8 @@ export class PointerLens { this._lensContainer.style.background = "white"; // this._lensContainer.style.opacity = "0"; this._lensContainer.style.borderRadius = "50%"; - this._lensContainer.style.width = "300px"; - this._lensContainer.style.height = "300px"; + this._lensContainer.style.width = this._lensParams.canvasSize + "px"; + this._lensContainer.style.height = this._lensParams.canvasSize + "px"; this._lensContainer.style.zIndex = "15000"; this._lensContainer.style.position = "absolute"; @@ -85,13 +82,14 @@ export class PointerLens { // this._lensCanvas.style.background = "darkblue"; this._lensCanvas.style.borderRadius = "50%"; - this._lensCanvas.style.width = "300px"; - this._lensCanvas.style.height = "300px"; + this._lensCanvas.style.width = this._lensParams.canvasSize + "px"; + this._lensCanvas.style.height = this._lensParams.canvasSize + "px"; this._lensCanvas.style.zIndex = "15000"; this._lensCanvas.style.pointerEvents = "none"; document.body.appendChild(this._lensContainer); this._lensContainer.appendChild(this._lensCanvas); + this._lensContainer.appendChild(this._lensCursorDiv); this._lensCanvasContext = this._lensCanvas.getContext('2d'); this._canvasElement = this.viewer.scene.canvas.canvas; @@ -109,7 +107,7 @@ export class PointerLens { this._active = (cfg.active !== false); this._visible = false; - this._snapped = false; + this.snapped = false; this._onViewerRendering = this.viewer.scene.on("rendering", () => { if (this._active && this._visible) { @@ -156,21 +154,13 @@ export class PointerLens { this._lensCanvas.height // destination height ); - const centerLensCanvas = [ - (lensRect.left + lensRect.right) / 2 - canvasRect.left, - (lensRect.top + lensRect.bottom) / 2 - canvasRect.top - ]; + const middle = this._lensParams.canvasSize / 2 - this._lensParams.cursorSize / 2 - this._lensParams.cursorBorder; - if (this._snappedCanvasPos) { - const deltaX = this._snappedCanvasPos[0] - this._canvasPos[0]; - const deltaY = this._snappedCanvasPos[1] - this._canvasPos[1]; + const deltaX = this._snappedCanvasPos ? (this._snappedCanvasPos[0] - this._canvasPos[0]) : 0; + const deltaY = this._snappedCanvasPos ? (this._snappedCanvasPos[1] - this._canvasPos[1]) : 0; - this._lensCursorDiv.style.marginLeft = `${centerLensCanvas[0] + deltaX * this._zoomLevel - 10}px`; - this._lensCursorDiv.style.marginTop = `${centerLensCanvas[1] + deltaY * this._zoomLevel - 10}px`; - } else { - this._lensCursorDiv.style.marginLeft = `${centerLensCanvas[0] - 10}px`; - this._lensCursorDiv.style.marginTop = `${centerLensCanvas[1] - 10}px`; - } + this._lensCursorDiv.style.left = `${middle + deltaX * this._zoomLevel}px`; + this._lensCursorDiv.style.top = `${middle + deltaY * this._zoomLevel}px`; } @@ -238,14 +228,10 @@ export class PointerLens { * @private */ set snapped(snapped) { - this._snapped = snapped; - if (snapped) { - this._lensCursorDiv.style.background = "greenyellow"; - this._lensCursorDiv.style.border = "2px solid green"; - } else { - this._lensCursorDiv.style.background = "pink"; - this._lensCursorDiv.style.border = "2px solid red"; - } + this._snapped = snapped; + const [ bg, border ] = snapped ? [ "greenyellow", "green" ] : [ "pink", "red" ]; + this._lensCursorDiv.style.background = bg; + this._lensCursorDiv.style.border = this._lensParams.cursorBorder + "px solid " + border; } /** @@ -257,19 +243,19 @@ export class PointerLens { get snapped() { return this._snapped; } - + + _updateActiveVisible() { + this._lensContainer.style.visibility = (this._active && this._visible) ? "visible" : "hidden"; + this.update(); + } + /** * Sets if this PointerLens is active. * @param active */ set active(active) { this._active = active; - this._lensContainer.style.visibility = (active && this._visible) ? "visible" : "hidden"; - if (!active || !this._visible ) { - this._lensCursorDiv.style.marginLeft = `-100px`; - this._lensCursorDiv.style.marginTop = `-100px`; - } - this.update(); + this._updateActiveVisible(); } /** @@ -288,12 +274,7 @@ export class PointerLens { */ set visible(visible) { this._visible = visible; - this._lensContainer.style.visibility = (visible && this._active) ? "visible" : "hidden"; - if (!visible || !this._active) { - this._lensCursorDiv.style.marginLeft = `-100px`; - this._lensCursorDiv.style.marginTop = `-100px`; - } - this.update(); + this._updateActiveVisible(); } /** diff --git a/src/plugins/AngleMeasurementsPlugin/AngleMeasurementsTouchControl.js b/src/plugins/AngleMeasurementsPlugin/AngleMeasurementsTouchControl.js index a7ed75da7a..99bcca23c5 100644 --- a/src/plugins/AngleMeasurementsPlugin/AngleMeasurementsTouchControl.js +++ b/src/plugins/AngleMeasurementsPlugin/AngleMeasurementsTouchControl.js @@ -1,7 +1,7 @@ import {math} from "../../viewer/scene/math/math.js"; import {PointerCircle} from "../../extras/PointerCircle/PointerCircle.js"; import {AngleMeasurementsControl} from "./AngleMeasurementsControl.js"; - +import {transformToNode} from "../lib/ui/index.js"; const WAITING_FOR_ORIGIN_TOUCH_START = 0; const WAITING_FOR_ORIGIN_QUICK_TOUCH_END = 1; @@ -17,6 +17,8 @@ const WAITING_FOR_TARGET_LONG_TOUCH_END = 8; const TOUCH_CANCELING = 7; +const tmpVec2 = math.vec2(); + /** * Creates {@link AngleMeasurement}s from touch input. * @@ -155,6 +157,19 @@ export class AngleMeasurementsTouchControl extends AngleMeasurementsControl { this._touchState = WAITING_FOR_ORIGIN_TOUCH_START; } + const copyCanvasPos = (event, dst) => { + dst[0] = event.clientX; + dst[1] = event.clientY; + transformToNode(canvas.ownerDocument.documentElement, canvas, dst); + return dst; + }; + + const toBodyPos = (pos, dst) => { + dst.set(pos); + transformToNode(canvas, canvas.ownerDocument.documentElement, dst); + return dst; + }; + canvas.addEventListener("touchstart", this._onCanvasTouchStart = (event) => { const currentNumTouches = event.touches.length; @@ -168,11 +183,8 @@ export class AngleMeasurementsTouchControl extends AngleMeasurementsControl { } const touch = event.touches[0]; - const touchX = touch.clientX; - const touchY = touch.clientY; - - touchStartCanvasPos.set([touchX, touchY]); - touchMoveCanvasPos.set([touchX, touchY]); + copyCanvasPos(touch, touchStartCanvasPos); + touchMoveCanvasPos.set(touchStartCanvasPos); switch (this._touchState) { @@ -188,7 +200,7 @@ export class AngleMeasurementsTouchControl extends AngleMeasurementsControl { }); if (snapPickResult && snapPickResult.snapped) { pointerWorldPos.set(snapPickResult.worldPos); - this.pointerCircle.start(snapPickResult.snappedCanvasPos); + this.pointerCircle.start(toBodyPos(snapPickResult.snappedCanvasPos, tmpVec2)); } else { const pickResult = scene.pick({ canvasPos: touchMoveCanvasPos, @@ -196,7 +208,7 @@ export class AngleMeasurementsTouchControl extends AngleMeasurementsControl { }) if (pickResult && pickResult.worldPos) { pointerWorldPos.set(pickResult.worldPos); - this.pointerCircle.start(pickResult.canvasPos); + this.pointerCircle.start(toBodyPos(pickResult.canvasPos, tmpVec2)); } else { return; } @@ -303,7 +315,7 @@ export class AngleMeasurementsTouchControl extends AngleMeasurementsControl { this.pointerLens.cursorPos = snapPickResult.snappedCanvasPos; this.pointerLens.snapped = true; } - this.pointerCircle.start(snapPickResult.snappedCanvasPos); + this.pointerCircle.start(toBodyPos(snapPickResult.snappedCanvasPos, tmpVec2)); pointerWorldPos.set(snapPickResult.worldPos); this._currentAngleMeasurement.corner.worldPos = snapPickResult.worldPos; this._currentAngleMeasurement.corner.entity = snapPickResult.entity; @@ -325,7 +337,7 @@ export class AngleMeasurementsTouchControl extends AngleMeasurementsControl { this.pointerLens.cursorPos = pickResult.canvasPos; this.pointerLens.snapped = false; } - this.pointerCircle.start(pickResult.canvasPos); + this.pointerCircle.start(toBodyPos(pickResult.canvasPos, tmpVec2)); pointerWorldPos.set(pickResult.worldPos); this._currentAngleMeasurement.corner.worldPos = pickResult.worldPos; this._currentAngleMeasurement.corner.entity = pickResult.entity; @@ -396,7 +408,7 @@ export class AngleMeasurementsTouchControl extends AngleMeasurementsControl { this.pointerLens.cursorPos = snapPickResult.snappedCanvasPos; this.pointerLens.snapped = true; } - this.pointerCircle.start(snapPickResult.snappedCanvasPos); + this.pointerCircle.start(toBodyPos(snapPickResult.snappedCanvasPos, tmpVec2)); pointerWorldPos.set(snapPickResult.worldPos); this._currentAngleMeasurement.target.worldPos = snapPickResult.worldPos; this._currentAngleMeasurement.target.entity = snapPickResult.entity; @@ -418,7 +430,7 @@ export class AngleMeasurementsTouchControl extends AngleMeasurementsControl { this.pointerLens.cursorPos = pickResult.canvasPos; this.pointerLens.snapped = false; } - this.pointerCircle.start(pickResult.canvasPos); + this.pointerCircle.start(toBodyPos(pickResult.canvasPos, tmpVec2)); pointerWorldPos.set(pickResult.worldPos); this._currentAngleMeasurement.target.worldPos = pickResult.worldPos; this._currentAngleMeasurement.target.entity = pickResult.entity; @@ -482,14 +494,11 @@ export class AngleMeasurementsTouchControl extends AngleMeasurementsControl { } const touch = event.touches[0]; - const touchX = touch.clientX; - const touchY = touch.clientY; - if (touch.identifier !== touchId) { return; } - touchMoveCanvasPos.set([touchX, touchY]); + copyCanvasPos(touch, touchMoveCanvasPos); let snapPickResult; let pickResult; @@ -662,9 +671,6 @@ export class AngleMeasurementsTouchControl extends AngleMeasurementsControl { } const touch = event.changedTouches[0]; - const touchX = touch.clientX; - const touchY = touch.clientY; - if (touch.identifier !== touchId) { return; } @@ -674,7 +680,10 @@ export class AngleMeasurementsTouchControl extends AngleMeasurementsControl { longTouchTimeout = null; } - touchEndCanvasPos.set([touchX, touchY]); + copyCanvasPos(touch, touchEndCanvasPos); + + const touchX = touchEndCanvasPos[0]; + const touchY = touchEndCanvasPos[1]; switch (this._touchState) { diff --git a/src/plugins/DistanceMeasurementsPlugin/DistanceMeasurementsTouchControl.js b/src/plugins/DistanceMeasurementsPlugin/DistanceMeasurementsTouchControl.js index 599aaac91c..72b7b4637e 100644 --- a/src/plugins/DistanceMeasurementsPlugin/DistanceMeasurementsTouchControl.js +++ b/src/plugins/DistanceMeasurementsPlugin/DistanceMeasurementsTouchControl.js @@ -1,7 +1,7 @@ import {math} from "../../viewer/scene/math/math.js"; import {PointerCircle} from "../../extras/PointerCircle/PointerCircle.js"; import {DistanceMeasurementsControl} from "./DistanceMeasurementsControl.js"; - +import {transformToNode} from "../lib/ui/index.js"; const WAITING_FOR_ORIGIN_TOUCH_START = 0; const WAITING_FOR_ORIGIN_QUICK_TOUCH_END = 1; @@ -13,6 +13,8 @@ const WAITING_FOR_TARGET_LONG_TOUCH_END = 5; const TOUCH_CANCELING = 7; +const tmpVec2 = math.vec2(); + /** * Creates {@link DistanceMeasurement}s from touch input. * @@ -159,6 +161,19 @@ export class DistanceMeasurementsTouchControl extends DistanceMeasurementsContro this._touchState = WAITING_FOR_ORIGIN_TOUCH_START; } + const copyCanvasPos = (event, dst) => { + dst[0] = event.clientX; + dst[1] = event.clientY; + transformToNode(canvas.ownerDocument.documentElement, canvas, dst); + return dst; + }; + + const toBodyPos = (pos, dst) => { + dst.set(pos); + transformToNode(canvas, canvas.ownerDocument.documentElement, dst); + return dst; + }; + canvas.addEventListener("touchstart", this._onCanvasTouchStart = (event) => { const currentNumTouches = event.touches.length; @@ -172,16 +187,8 @@ export class DistanceMeasurementsTouchControl extends DistanceMeasurementsContro } const touch = event.touches[0]; - - // Get the canvas's bounding rectangle - const rect = canvas.getBoundingClientRect(); - - // Calculate the touch position relative to the canvas - const touchX = touch.clientX - rect.left; - const touchY = touch.clientY - rect.top; - - touchStartCanvasPos.set([touchX, touchY]); - touchMoveCanvasPos.set([touchX, touchY]); + copyCanvasPos(touch, touchStartCanvasPos); + touchMoveCanvasPos.set(touchStartCanvasPos); switch (this._touchState) { @@ -197,7 +204,7 @@ export class DistanceMeasurementsTouchControl extends DistanceMeasurementsContro }); if (snapPickResult && snapPickResult.snapped) { pointerWorldPos.set(snapPickResult.worldPos); - this.pointerCircle.start(snapPickResult.snappedCanvasPos); + this.pointerCircle.start(toBodyPos(snapPickResult.snappedCanvasPos, tmpVec2)); } else { const pickResult = scene.pick({ canvasPos: touchMoveCanvasPos, @@ -205,7 +212,7 @@ export class DistanceMeasurementsTouchControl extends DistanceMeasurementsContro }) if (pickResult && pickResult.worldPos) { pointerWorldPos.set(pickResult.worldPos); - this.pointerCircle.start(pickResult.canvasPos); + this.pointerCircle.start(toBodyPos(pickResult.canvasPos, tmpVec2)); } else { return; } @@ -309,7 +316,7 @@ export class DistanceMeasurementsTouchControl extends DistanceMeasurementsContro this.pointerLens.cursorPos = snapPickResult.snappedCanvasPos; this.pointerLens.snapped = true; } - this.pointerCircle.start(snapPickResult.snappedCanvasPos); + this.pointerCircle.start(toBodyPos(snapPickResult.snappedCanvasPos, tmpVec2)); pointerWorldPos.set(snapPickResult.worldPos); this._currentDistanceMeasurement.target.worldPos = snapPickResult.worldPos; this._currentDistanceMeasurement.target.entity = snapPickResult.entity; @@ -327,7 +334,7 @@ export class DistanceMeasurementsTouchControl extends DistanceMeasurementsContro this.pointerLens.cursorPos = pickResult.canvasPos; this.pointerLens.snapped = false; } - this.pointerCircle.start(pickResult.canvasPos); + this.pointerCircle.start(toBodyPos(pickResult.canvasPos, tmpVec2)); pointerWorldPos.set(pickResult.worldPos); this._currentDistanceMeasurement.target.worldPos = pickResult.worldPos; this._currentDistanceMeasurement.target.entity = pickResult.entity; @@ -386,19 +393,11 @@ export class DistanceMeasurementsTouchControl extends DistanceMeasurementsContro } const touch = event.touches[0]; - - // Get the canvas's bounding rectangle - const rect = canvas.getBoundingClientRect(); - - // Calculate the touch position relative to the canvas - const touchX = touch.clientX - rect.left; - const touchY = touch.clientY - rect.top; - if (touch.identifier !== touchId) { return; } - touchMoveCanvasPos.set([touchX, touchY]); + copyCanvasPos(touch, touchMoveCanvasPos); let snapPickResult; let pickResult; @@ -563,14 +562,6 @@ export class DistanceMeasurementsTouchControl extends DistanceMeasurementsContro } const touch = event.changedTouches[0]; - - // Get the canvas's bounding rectangle - const rect = canvas.getBoundingClientRect(); - - // Calculate the touch position relative to the canvas - const touchX = touch.clientX - rect.left; - const touchY = touch.clientY - rect.top; - if (touch.identifier !== touchId) { return; } @@ -580,7 +571,10 @@ export class DistanceMeasurementsTouchControl extends DistanceMeasurementsContro longTouchTimeout = null; } - touchEndCanvasPos.set([touchX, touchY]); + copyCanvasPos(touch, touchEndCanvasPos); + + const touchX = touchEndCanvasPos[0]; + const touchY = touchEndCanvasPos[1]; switch (this._touchState) {