Compare commits

..

No commits in common. "4602ec9e74865afb2a4574eac5d290aa3c3b3590" and "10c9e576e3334a310aca1bea415b165752217159" have entirely different histories.

3 changed files with 36 additions and 148 deletions

View File

@ -18,7 +18,6 @@ const autoFitOptions = {
}
let viewportWidth = document.documentElement.clientWidth
let autoFitResetTimer: ReturnType<typeof setTimeout> | undefined
const initAutoFit = () => {
autofit.init(autoFitOptions, false)
@ -30,36 +29,29 @@ const resetAutoFit = () => {
viewportWidth = document.documentElement.clientWidth
}
const scheduleAutoFitReset = () => {
clearTimeout(autoFitResetTimer)
// resize autofit
autoFitResetTimer = setTimeout(resetAutoFit, 50)
}
const handleResize = () => {
const nextWidth = document.documentElement.clientWidth
//
if (Math.abs(nextWidth - viewportWidth) <= 2) return
scheduleAutoFitReset()
resetAutoFit()
}
onMounted(() => {
initAutoFit()
window.addEventListener('resize', handleResize)
window.addEventListener('orientationchange', scheduleAutoFitReset)
document.addEventListener('fullscreenchange', scheduleAutoFitReset)
document.addEventListener('webkitfullscreenchange', scheduleAutoFitReset)
window.addEventListener('orientationchange', resetAutoFit)
document.addEventListener('fullscreenchange', resetAutoFit)
document.addEventListener('webkitfullscreenchange', resetAutoFit)
})
onUnmounted(() => {
autofit.off('#app')
clearTimeout(autoFitResetTimer)
window.removeEventListener('resize', handleResize)
window.removeEventListener('orientationchange', scheduleAutoFitReset)
document.removeEventListener('fullscreenchange', scheduleAutoFitReset)
document.removeEventListener('webkitfullscreenchange', scheduleAutoFitReset)
window.removeEventListener('orientationchange', resetAutoFit)
document.removeEventListener('fullscreenchange', resetAutoFit)
document.removeEventListener('webkitfullscreenchange', resetAutoFit)
})
</script>

View File

@ -25,8 +25,6 @@ import { onBeforeUnmount, onMounted, ref } from 'vue'
const isPortraitTablet = ref(false)
let portraitMediaQuery
let orientationFrame
const orientationTimers = new Set()
const isTablet = () => {
const userAgent = navigator.userAgent
@ -42,32 +40,11 @@ const isTablet = () => {
}
const updateOrientation = () => {
const viewport = window.visualViewport
const viewportWidth = viewport?.width || window.innerWidth
const viewportHeight = viewport?.height || window.innerHeight
const isPortrait = viewportHeight > viewportWidth
const isPortrait = portraitMediaQuery?.matches ?? window.innerHeight > window.innerWidth
isPortraitTablet.value = isTablet() && isPortrait
document.documentElement.classList.toggle('is-portrait-tablet', isPortraitTablet.value)
}
const scheduleOrientationUpdate = () => {
updateOrientation()
cancelAnimationFrame(orientationFrame)
orientationFrame = requestAnimationFrame(updateOrientation)
orientationTimers.forEach(clearTimeout)
orientationTimers.clear()
// iPad
for (const delay of [80, 180, 350]) {
const timer = setTimeout(() => {
orientationTimers.delete(timer)
updateOrientation()
}, delay)
orientationTimers.add(timer)
}
}
const tryLockLandscape = async () => {
if (!isTablet() || !screen.orientation?.lock) return
@ -83,22 +60,17 @@ onMounted(() => {
updateOrientation()
tryLockLandscape()
portraitMediaQuery.addEventListener?.('change', scheduleOrientationUpdate)
window.addEventListener('resize', scheduleOrientationUpdate)
window.addEventListener('orientationchange', scheduleOrientationUpdate)
window.visualViewport?.addEventListener('resize', scheduleOrientationUpdate)
portraitMediaQuery.addEventListener?.('change', updateOrientation)
window.addEventListener('resize', updateOrientation)
window.addEventListener('orientationchange', updateOrientation)
document.addEventListener('fullscreenchange', tryLockLandscape)
})
onBeforeUnmount(() => {
document.documentElement.classList.remove('is-portrait-tablet')
cancelAnimationFrame(orientationFrame)
orientationTimers.forEach(clearTimeout)
orientationTimers.clear()
portraitMediaQuery?.removeEventListener?.('change', scheduleOrientationUpdate)
window.removeEventListener('resize', scheduleOrientationUpdate)
window.removeEventListener('orientationchange', scheduleOrientationUpdate)
window.visualViewport?.removeEventListener('resize', scheduleOrientationUpdate)
portraitMediaQuery?.removeEventListener?.('change', updateOrientation)
window.removeEventListener('resize', updateOrientation)
window.removeEventListener('orientationchange', updateOrientation)
document.removeEventListener('fullscreenchange', tryLockLandscape)
})
</script>

View File

@ -94,9 +94,6 @@ const containerRef = ref(null)
const mapCanvasRef = ref(null)
let resizeObserver
let eventsBound = false
let orientationObserver
let animationFrameId = null
let isRendering = false
const init = () => {
mapCanvas = mapCanvasRef.value;
@ -109,14 +106,11 @@ const init = () => {
*/
function resizeCanvasToContainer() {
if (!mapCanvas || !containerRef.value) return;
// clientWidth/clientHeight autofit.js CSS transform
// getBoundingClientRect() Canvas
const containerWidth = containerRef.value.clientWidth;
const containerHeight = containerRef.value.clientHeight;
if (!containerWidth || !containerHeight) return;
const containerRect = containerRef.value.getBoundingClientRect(); //
if (!containerRect.width || !containerRect.height) return;
applicationState.devicePixelRatio = window.devicePixelRatio || 1; //
applicationState.canvasWidthPx = containerWidth; //
applicationState.canvasHeightPx = containerHeight; // = -
applicationState.canvasWidthPx = containerRect.width; //
applicationState.canvasHeightPx = containerRect.height; // = -
mapCanvas.width = applicationState.canvasWidthPx * applicationState.devicePixelRatio; // Canvas
mapCanvas.height = applicationState.canvasHeightPx * applicationState.devicePixelRatio; // Canvas
mapCanvas.style.width = applicationState.canvasWidthPx + 'px'; // CSS
@ -217,8 +211,6 @@ async function loadMapIntoApplication(parsedMap) {
* @param {number} timestamp - requestAnimationFrame 提供的时间戳毫秒
*/
function renderFrame(timestamp) {
if (!isRendering || !canvasCtx) return;
//
applicationState.animationTimeSeconds = (timestamp || 0) / 1000;
@ -228,7 +220,7 @@ function renderFrame(timestamp) {
//
if (!applicationState.isMapLoaded) {
animationFrameId = requestAnimationFrame(renderFrame); //
requestAnimationFrame(renderFrame); //
return;
}
@ -278,36 +270,7 @@ function renderFrame(timestamp) {
drawMapBoundary(canvasCtx, map, applicationState);
//
animationFrameId = requestAnimationFrame(renderFrame);
}
const stopRendering = () => {
isRendering = false
if (animationFrameId !== null) {
cancelAnimationFrame(animationFrameId)
animationFrameId = null
}
}
const startRendering = () => {
if (isRendering || !applicationState.isMapLoaded || document.documentElement.classList.contains('is-portrait-tablet')) return
isRendering = true
animationFrameId = requestAnimationFrame(renderFrame)
}
const handleOrientationVisibility = () => {
if (document.documentElement.classList.contains('is-portrait-tablet')) {
stopRendering()
return
}
// v-show Vue
nextTick(() => {
requestAnimationFrame(() => {
resizeCanvasToContainer()
startRendering()
})
})
requestAnimationFrame(renderFrame);
}
const drawAllRobots = (ctx) => {
@ -329,25 +292,12 @@ const bindEvent = () => {
let activePointerId = null
let lastPointerX = 0
let lastPointerY = 0
let pointerStartX = 0
let pointerStartY = 0
let hasDragged = false
const getCanvasPointerPosition = (pointerEvent) => {
const canvasRect = mapCanvas.getBoundingClientRect()
const scaleX = applicationState.canvasWidthPx / canvasRect.width
const scaleY = applicationState.canvasHeightPx / canvasRect.height
return {
x: (pointerEvent.clientX - canvasRect.left) * scaleX,
y: (pointerEvent.clientY - canvasRect.top) * scaleY
}
}
// Pointer Events Pad
mapCanvas.addEventListener('pointermove', (pointerEvent) => {
const pointerPosition = getCanvasPointerPosition(pointerEvent)
applicationState.mouseState.screenX = pointerPosition.x;
applicationState.mouseState.screenY = pointerPosition.y;
const canvasRect = mapCanvas.getBoundingClientRect(); // Canvas
applicationState.mouseState.screenX = pointerEvent.clientX - canvasRect.left;
applicationState.mouseState.screenY = pointerEvent.clientY - canvasRect.top;
//
const worldPos = screenToWorld(applicationState.mouseState.screenX, applicationState.mouseState.screenY, applicationState);
applicationState.mouseState.worldX = worldPos.x;
@ -355,15 +305,12 @@ const bindEvent = () => {
if (applicationState.mouseState.isDragging && pointerEvent.pointerId === activePointerId) {
pointerEvent.preventDefault()
const movementX = pointerPosition.x - lastPointerX
const movementY = pointerPosition.y - lastPointerY
if (Math.hypot(pointerPosition.x - pointerStartX, pointerPosition.y - pointerStartY) > 6) {
hasDragged = true
}
const movementX = pointerEvent.clientX - lastPointerX
const movementY = pointerEvent.clientY - lastPointerY
applicationState.camera.centerX -= movementX / applicationState.camera.pixelsPerMeter;
applicationState.camera.centerY += movementY / applicationState.camera.pixelsPerMeter;
lastPointerX = pointerPosition.x
lastPointerY = pointerPosition.y
lastPointerX = pointerEvent.clientX
lastPointerY = pointerEvent.clientY
}
if (pointerEvent.pointerType === 'mouse') {
@ -372,7 +319,9 @@ const bindEvent = () => {
});
mapCanvas.addEventListener('click', (mouseEvent) => {
const { x: screenX, y: screenY } = getCanvasPointerPosition(mouseEvent)
const canvasRect = mapCanvas.getBoundingClientRect(); // Canvas
const screenX = mouseEvent.clientX - canvasRect.left; // Canvas X
const screenY = mouseEvent.clientY - canvasRect.top; // Canvas Y
const worldPos = screenToWorld(screenX, screenY, applicationState);
})
@ -382,33 +331,15 @@ const bindEvent = () => {
return
}
pointerEvent.preventDefault()
const pointerPosition = getCanvasPointerPosition(pointerEvent)
activePointerId = pointerEvent.pointerId
lastPointerX = pointerPosition.x
lastPointerY = pointerPosition.y
pointerStartX = pointerPosition.x
pointerStartY = pointerPosition.y
hasDragged = false
lastPointerX = pointerEvent.clientX
lastPointerY = pointerEvent.clientY
applicationState.mouseState.isDragging = true;
mapCanvas.setPointerCapture(pointerEvent.pointerId)
});
const endPointerDrag = (pointerEvent, cancelled = false) => {
const endPointerDrag = (pointerEvent) => {
if (pointerEvent.pointerId !== activePointerId) return
// Pad/
if (!cancelled && !hasDragged && pointerEvent.pointerType !== 'mouse') {
const pointerPosition = getCanvasPointerPosition(pointerEvent)
applicationState.mouseState.screenX = pointerPosition.x
applicationState.mouseState.screenY = pointerPosition.y
const worldPos = screenToWorld(
applicationState.mouseState.screenX,
applicationState.mouseState.screenY,
applicationState
)
updateTooltipDisplay(hitTestRobots(worldPos.x, worldPos.y, applicationState))
}
applicationState.mouseState.isDragging = false;
if (mapCanvas.hasPointerCapture(pointerEvent.pointerId)) {
mapCanvas.releasePointerCapture(pointerEvent.pointerId)
@ -416,8 +347,8 @@ const bindEvent = () => {
activePointerId = null
}
mapCanvas.addEventListener('pointerup', (pointerEvent) => endPointerDrag(pointerEvent));
mapCanvas.addEventListener('pointercancel', (pointerEvent) => endPointerDrag(pointerEvent, true));
mapCanvas.addEventListener('pointerup', endPointerDrag);
mapCanvas.addEventListener('pointercancel', endPointerDrag);
//
mapCanvas.addEventListener('wheel', (wheelEvent) => {
@ -529,11 +460,6 @@ onMounted(() => {
resizeObserver = new ResizeObserver(resizeCanvasToContainer)
resizeObserver.observe(containerRef.value)
window.addEventListener('resize', resizeCanvasToContainer)
orientationObserver = new MutationObserver(handleOrientationVisibility)
orientationObserver.observe(document.documentElement, {
attributes: true,
attributeFilter: ['class']
})
})
watch(() => props.mapName,
@ -541,7 +467,7 @@ watch(() => props.mapName,
if (newVal) {
await loadSourceMap(newVal)
addRobot(robotStore.ip, robotStore.ip, robotStore.position.x, robotStore.position.y, robotStore.position.theta, getThemeColor('--color-canvas-robot'))
startRendering()
requestAnimationFrame(renderFrame);
}
}, {
immediate: true
@ -554,10 +480,8 @@ defineExpose({
})
onUnmounted(() => {
stopRendering()
window.removeEventListener('resize', resizeCanvasToContainer)
resizeObserver?.disconnect()
orientationObserver?.disconnect()
canvasCtx = null
})
</script>