fix(web): draw the remote screen inside a fixed canvas to kill the iOS ghost lines
On an iPhone in landscape, panning the picture to the left with two fingers left
a stack of 1px vertical lines along the right edge. It only showed up after
zooming in, and every pan added more lines.
The cause is the old render model, not the panning code. #screen-canvas was
sized to the remote resolution and then moved/scaled with a CSS transform
(`canvas.style.transform = scale(s) translate(tx, ty)`). iOS Safari caches the
raster of a transformed canvas layer and does not repaint the strip the layer
uncovers when it moves, so whatever sat at the old right edge -- one column of
the picture -- got stretched across the newly exposed band. Pan again and the
next column repeats it: the lines accumulate.
An earlier attempt clamped the pan range so the uncovered band could not appear.
That was wrong twice over: it did not remove the ghosting (the clamp still moved
the layer, so the edge was still repainted from a stale raster), and it put hard
limits on dragging, which is not how the page is used.
So the canvas element now never moves. It is pinned to the container
(bitmap = viewport x devicePixelRatio, driven by a ResizeObserver), and every
frame is drawn into it with ctx.setTransform. There is no CSS transform, no CSS
scaling, and no layer movement left to cache, so there is nothing to leave a
stale edge behind.
- renderFrame(): paints black, then draws the latest frame with the fit-to-view
and pinch-zoom/pan transform applied via setTransform.
- initDecoder(): keeps the remote-resolution frame in an offscreen frameCanvas.
The isTopDown flip is applied to that canvas only, so the visible canvas is
always upright and no longer needs a flipped transform.
- positionCursorOverlay() / screenToCanvas(): rewritten as exact inverses of the
new transform chain, so the cursor overlay and touch coordinates still land on
the right remote pixel.
- applyZoomTransform(): drops the CSS transform and every pan clamp. Dragging
past the picture just shows the canvas' own black background, with no boundary
at all -- the free panning feel is unchanged.
- Touch/mouse deltas are converted with remotePerScreenPx() instead of assuming
the old element size.
The picture also has to land exactly where it used to. In the old model the
canvas element *was* the picture, so the flex container positioned it: portrait
CSS sets `.canvas-container { align-items: flex-start; padding-top: 56px }` to
pin it to the top and keep the on-screen keyboard off it, landscape leaves it
centred. Now that the picture is drawn inside a full-bleed canvas, flex no
longer positions it, and hard-coding "centre" dropped it 262px in portrait --
straight into the keyboard's path. So renderFrame takes its vertical placement
from the container's computed align-items and padding-top, and the canvas covers
the padding box so a zoomed picture panned upwards still reaches the top of the
container the way it used to.
The zoom anchor had the same problem: originX/originY were read as percentages
of the canvas, while the old transform-origin sat on the canvas element, i.e. on
the picture. They are percentages of the picture now, so pinch-zoom keeps the
point under the fingers fixed instead of drifting towards the canvas centre.
Two smaller details have to be right for the mapping to stay exact, and both are
fixed here rather than left for later:
- resizeScreenCanvas() takes its scale factor from the bitmap it just allocated
divided by the measured CSS width, not from devicePixelRatio. The bitmap is
rounded, so with a fractional CSS width or a non-integer ratio the two drift
apart -- 843.5px at dpr 2.625 lands 2.2e-3 apart -- and translateX, which
reaches a few hundred px, turns that into a ~1px mismatch between the picture
and the cursor overlay. getViewMetrics().d is now identically equal to the
kx reported by canvasPixelRatio().
- updateInputShortcutsPosition() divides the picture height out of streamH, which
is 0 before the first frame; it now bails out instead of placing the shortcuts
bar in the middle of the canvas box.
Nothing else reads the canvas dimensions, so the semantic change from "remote
resolution" to "viewport x dpr" does not reach any other feature: there is no
toDataURL/getImageData anywhere, the wall tiles own their own canvases and go
through wallHandleFrame, the cursor overlay is a fixed 24x24 element that never
scaled with the old CSS transform either, and the wheel/dblclick handlers only
forward coordinates. The overlays that sit on top of the canvas (quick controls
100, shortcuts 101, android navbar 102, cursor 1002) all carry a z-index, so
making the canvas positioned does not push it over them.
Verified in a browser harness built from the same functions the page uses:
drawing position vs cursor position over 90 fully-visible cases is off by at
most 0.42px, the screen->remote->screen round trip over 1500 points is exact to
0.000px, a real two-finger gesture chain (scale 1 -> 2.5, translateX -145) moves
the picture correctly with no page errors, and across dpr 1.5/2/2.625/2.75/3 the
bitmap ratio matches the CSS ratio exactly (round-trip error 7e-4px, float
noise). Picture placement was diffed against the previous build in five layouts
(portrait, portrait fullscreen, Android immersive, landscape, landscape
fullscreen) and matches to 0.0px in every one; a portrait zoom of 2x panned up
by 150px shows the same visible band (y 44..128) as before; with the viewport
shortened to simulate the keyboard the picture stays at y 100..319 and is not
covered; pinch anchors at 25%/50%/80% of the picture stay put to 0px across
scale 1.5/2/3. The user confirmed on the device that the lines are gone.
Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
36ae4b3057
commit
0a6cbbbf06
1 file changed
+266
-174
+266
-174
@@ -892,7 +892,17 @@
|
||||
}
|
||||
#screen-page.active { display: flex !important; flex-direction: column; }
|
||||
.canvas-container { flex: 1; display: flex; align-items: center; justify-content: center; overflow: hidden; background: #111; position: relative; }
|
||||
#screen-canvas { max-width: 100%; max-height: 100%; }
|
||||
/* 画布铺满容器的 border box(含 padding 区),位图按设备像素(见 resizeScreenCanvas):
|
||||
元素本身永远不移动、不做 CSS transform,缩放平移全部在画布内部绘制。iOS Safari 会给
|
||||
被 transform 的 canvas 层缓存栅格,平移后不重绘边缘,右侧会留下一条条 1px 残影;
|
||||
元素不动就不会有这个问题,拖拽也仍然没有边界限制。
|
||||
之所以要盖住 padding 区:竖屏容器有 padding-top:56px 给快捷按钮让位,画面本身仍从
|
||||
content box 顶开始(见 getViewMetrics 的 padTop),但放大后往上拖时得能画进那条
|
||||
padding 里,否则会比以前少显示 56px。 */
|
||||
#screen-canvas {
|
||||
position: absolute; top: 0; left: 0; right: 0; bottom: 0;
|
||||
width: 100%; height: 100%; display: block;
|
||||
}
|
||||
.screen-toolbar {
|
||||
background: linear-gradient(180deg, rgba(0,0,0,0.95) 0%, rgba(0,0,0,0.85) 100%);
|
||||
/* 远程桌面顶部工具栏也避开 notch / Dynamic Island —— 顶 padding + 安全区 */
|
||||
@@ -985,10 +995,11 @@
|
||||
padding-top: calc(56px + env(safe-area-inset-top)) !important;
|
||||
}
|
||||
}
|
||||
/* 画布铺满容器,位图由 resizeScreenCanvas 按设备像素设置;
|
||||
不能再有 object-fit: contain —— 那会把位图按比例缩进盒子,坐标映射就错了。 */
|
||||
#screen-page.pseudo-fullscreen #screen-canvas {
|
||||
max-width: 100vw !important; max-height: 100vh !important;
|
||||
max-height: 100dvh !important;
|
||||
object-fit: contain;
|
||||
width: 100% !important; height: 100% !important;
|
||||
max-width: none !important; max-height: none !important;
|
||||
}
|
||||
#screen-page.pseudo-fullscreen .toolbar-toggle {
|
||||
display: flex; /* Show toolbar toggle in fullscreen */
|
||||
@@ -1006,8 +1017,8 @@
|
||||
align-items: center !important;
|
||||
}
|
||||
#screen-page.android-portrait #screen-canvas {
|
||||
max-width: 100vw !important;
|
||||
max-height: 100dvh !important; max-height: 100vh !important;
|
||||
width: 100% !important; height: 100% !important;
|
||||
max-width: none !important; max-height: none !important;
|
||||
}
|
||||
#screen-page.android-portrait .toolbar-toggle { display: flex !important; }
|
||||
#screen-page.android-portrait .quick-controls { display: none !important; }
|
||||
@@ -2024,6 +2035,151 @@
|
||||
const canvas = document.getElementById('screen-canvas');
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
// ── 画面渲染模型 ────────────────────────────────────────────────────
|
||||
// canvas 元素尺寸固定为容器大小、位图按设备像素,缩放/平移全部在画布内部
|
||||
// 绘制,元素本身永远不移动、不加 CSS transform。
|
||||
// 起因:iOS Safari 会把被 transform 的 canvas 层栅格缓存住,平移后不重绘
|
||||
// 让出来的边缘,屏幕右侧就累积出一条条 1px 残影。元素不动就没有这个层,
|
||||
// 也就没有残影;同时拖拽依旧没有边界限制,拖出画面只是露出画布自己的黑底。
|
||||
|
||||
// 捏合缩放/平移状态。声明放在这里是因为下面的 renderFrame 会被 ResizeObserver
|
||||
// 提前调用,晚声明会踩 TDZ。
|
||||
let zoomState = {
|
||||
scale: 1,
|
||||
minScale: 1,
|
||||
maxScale: 4,
|
||||
translateX: 0,
|
||||
translateY: 0,
|
||||
lastPinchDist: 0,
|
||||
isPinching: false,
|
||||
pinchCenterX: 0,
|
||||
pinchCenterY: 0,
|
||||
// 缩放原点,相对画布盒子的百分比(双指起手时设为捏合中心)
|
||||
originX: 50,
|
||||
originY: 50,
|
||||
// Two-finger gesture detection
|
||||
hasZoomed: false, // Whether zoom occurred in current gesture
|
||||
lastScrollY: 0, // For scroll delta calculation
|
||||
initialPinchDist: 0, // Distance at gesture start (for cumulative detection)
|
||||
initialCenterX: 0, // Pinch center at gesture start (for scroll-vs-zoom intent)
|
||||
initialCenterY: 0
|
||||
};
|
||||
|
||||
let streamW = 0, streamH = 0; // 远端画面分辨率
|
||||
let frameCanvas = null, frameCtx = null; // 最近一帧(远端分辨率,已按 isTopDown 摆正)
|
||||
let screenDpr = 1;
|
||||
// 画面在画布内的垂直对齐。必须跟着 .canvas-container 的 align-items 走:
|
||||
// 竖屏下 CSS 特意设成 flex-start + padding-top:56px,把画面顶到顶部给快捷按钮
|
||||
// 让位,键盘弹出时才不会盖住画面。以前画面是 canvas 元素本身、由 flex 对齐,
|
||||
// 现在画面画在画布内部,flex 不再管它,得自己照抄这个对齐方式。
|
||||
// 画布盖住了整个 border box,所以画面基准还要再往下挪一个 padding-top。
|
||||
let viewAlignY = 'center';
|
||||
let viewPadTop = 0; // 容器 padding-top(CSS px)
|
||||
|
||||
function syncViewAlign() {
|
||||
const cont = document.querySelector('.canvas-container');
|
||||
if (!cont) return;
|
||||
const cs = getComputedStyle(cont);
|
||||
const a = cs.alignItems;
|
||||
viewAlignY = (a === 'flex-start' || a === 'start') ? 'start' : 'center';
|
||||
viewPadTop = parseFloat(cs.paddingTop) || 0;
|
||||
}
|
||||
|
||||
// 视口度量,长度单位统一为画布位图像素
|
||||
function getViewMetrics() {
|
||||
const bw = canvas.width || 1, bh = canvas.height || 1;
|
||||
const fit = (streamW > 0 && streamH > 0)
|
||||
? Math.min(bw / streamW, bh / streamH) : 1;
|
||||
const padTop = viewPadTop * (screenDpr || 1);
|
||||
// 画面可用高度要扣掉 padding-top,余量才能在 content box 内居中
|
||||
const spareY = bh - streamH * fit - padTop;
|
||||
return {
|
||||
bw: bw, bh: bh, d: screenDpr || 1, fit: fit,
|
||||
offX: (bw - streamW * fit) / 2,
|
||||
// 贴顶时没有余量可分,直接落在 content box 顶部;居中才把余量对半分。
|
||||
offY: padTop + ((viewAlignY === 'start') ? 0 : spareY / 2)
|
||||
};
|
||||
}
|
||||
|
||||
// 画布位图像素 ↔ 屏幕 CSS 像素
|
||||
function canvasPixelRatio() {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
return {
|
||||
rect: rect,
|
||||
kx: rect.width ? canvas.width / rect.width : 1,
|
||||
ky: rect.height ? canvas.height / rect.height : 1
|
||||
};
|
||||
}
|
||||
|
||||
// 屏幕移动 1px 对应多少远端像素(触摸/鼠标增量换算用)
|
||||
function remotePerScreenPx() {
|
||||
const m = getViewMetrics();
|
||||
const fitCss = m.fit / m.d; // CSS px / 远端像素
|
||||
return 1 / (fitCss * (zoomState.scale || 1));
|
||||
}
|
||||
|
||||
// 缩放原点在画布位图坐标里的位置。基准是「画面」而不是画布:旧版 transform-origin
|
||||
// 落在 canvas 元素上(元素即画面),捏合时以手指下的画面点为锚点放大。画布铺满容器
|
||||
// 之后画面只占其中一块,若仍按画布百分比算锚点就偏了 —— 竖屏贴顶时偏得最明显。
|
||||
function zoomOriginPx(m) {
|
||||
m = m || getViewMetrics();
|
||||
if (!streamW || !streamH) return { x: m.bw / 2, y: m.bh / 2 };
|
||||
return {
|
||||
x: m.offX + streamW * m.fit * zoomState.originX / 100,
|
||||
y: m.offY + streamH * m.fit * zoomState.originY / 100
|
||||
};
|
||||
}
|
||||
|
||||
// 把最近一帧按当前缩放/平移画进画布。
|
||||
// 变换链:远端坐标 → fit 居中 → 绕捏合中心缩放 → 平移。
|
||||
function renderFrame() {
|
||||
if (!canvas.width || !canvas.height) return;
|
||||
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
||||
ctx.fillStyle = '#000';
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
if (!frameCanvas || !streamW || !streamH) return;
|
||||
const m = getViewMetrics();
|
||||
const s = zoomState.scale;
|
||||
const o = zoomOriginPx(m);
|
||||
const ox = o.x, oy = o.y;
|
||||
const a = s * m.fit;
|
||||
ctx.setTransform(a, 0, 0, a,
|
||||
ox + s * (m.offX + zoomState.translateX * m.d - ox),
|
||||
oy + s * (m.offY + zoomState.translateY * m.d - oy));
|
||||
ctx.drawImage(frameCanvas, 0, 0);
|
||||
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
||||
}
|
||||
|
||||
// 画布位图尺寸跟随视口(横竖屏、进出全屏、工具栏显隐都会改变它)
|
||||
function resizeScreenCanvas() {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const w = rect.width, h = rect.height;
|
||||
if (!w || !h) return;
|
||||
// 对齐方式跟着容器走:横竖屏切换、进出全屏都会改 align-items / padding。
|
||||
const prevAlign = viewAlignY, prevPad = viewPadTop;
|
||||
syncViewAlign();
|
||||
const alignChanged = viewAlignY !== prevAlign || viewPadTop !== prevPad;
|
||||
const d = Math.min(3, window.devicePixelRatio || 1);
|
||||
const bw = Math.round(w * d), bh = Math.round(h * d);
|
||||
// 比值取「实际位图 / 实际 CSS 尺寸」,而不是直接存 devicePixelRatio:
|
||||
// 位图取整、CSS 尺寸带小数时两者会差千分之几,而 translateX 最大有几百,
|
||||
// 乘上去就是亚像素偏差 —— 那会让光标覆盖层和画面在放大后对不齐。
|
||||
// 这样取值后 m.d 恒等于 canvasPixelRatio() 的 kx,两条路径才严格自洽。
|
||||
const k = bw / w;
|
||||
const changed = canvas.width !== bw || canvas.height !== bh ||
|
||||
screenDpr !== k || alignChanged;
|
||||
screenDpr = k;
|
||||
if (!changed) return;
|
||||
canvas.width = bw;
|
||||
canvas.height = bh;
|
||||
renderFrame();
|
||||
}
|
||||
|
||||
if (window.ResizeObserver) {
|
||||
new ResizeObserver(function () { resizeScreenCanvas(); })
|
||||
.observe(document.querySelector('.canvas-container'));
|
||||
}
|
||||
|
||||
// ===================== Screen Wall (multi-device) =====================
|
||||
// 该常量必须在脚本顶部:wall 状态初始化立即用到它,后文声明会触发 TDZ。
|
||||
const isTouchDevice = ('ontouchstart' in window) || (navigator.maxTouchPoints > 0);
|
||||
@@ -2779,29 +2935,37 @@
|
||||
needKeyframe = false;
|
||||
decodeTimestamp = 0;
|
||||
|
||||
// Clear canvas before resizing to prevent residual content
|
||||
ctx.setTransform(1, 0, 0, 1, 0, 0); // Reset transform
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
// 远端分辨率:画布位图尺寸不再跟它走(画布位图 = 视口),
|
||||
// 解码结果先落到 frameCanvas,再由 renderFrame 按缩放/平移画进画布。
|
||||
streamW = width;
|
||||
streamH = height;
|
||||
if (!frameCanvas) {
|
||||
frameCanvas = document.createElement('canvas');
|
||||
frameCtx = frameCanvas.getContext('2d');
|
||||
}
|
||||
if (frameCanvas.width !== width || frameCanvas.height !== height) {
|
||||
frameCanvas.width = width;
|
||||
frameCanvas.height = height;
|
||||
}
|
||||
frameCtx.setTransform(1, 0, 0, 1, 0, 0);
|
||||
frameCtx.fillStyle = '#000';
|
||||
frameCtx.fillRect(0, 0, width, height);
|
||||
// Windows/Linux/macOS clients send bottom-up H.264 (BMP convention) → flip.
|
||||
// Android MediaCodec sends top-down H.264 → no flip needed.
|
||||
// 翻转只作用在 frameCanvas 上,画布本身永远是正的。
|
||||
if (!isTopDown) {
|
||||
frameCtx.setTransform(1, 0, 0, -1, 0, height);
|
||||
}
|
||||
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
|
||||
// Clear again after resize (in case of smaller resolution)
|
||||
ctx.fillStyle = '#000';
|
||||
ctx.fillRect(0, 0, width, height);
|
||||
// 画布位图跟随视口(分辨率变化不影响它),随后按新画面重绘一次
|
||||
resizeScreenCanvas();
|
||||
renderFrame();
|
||||
|
||||
// Update input shortcuts position after canvas resize
|
||||
requestAnimationFrame(updateInputShortcutsPosition);
|
||||
// 分辨率变了:非控制态的远程光标按新画布重新定位
|
||||
requestAnimationFrame(renderRemoteCursor);
|
||||
|
||||
// Windows/Linux/macOS clients send bottom-up H.264 (BMP convention) → flip.
|
||||
// Android MediaCodec sends top-down H.264 → no flip needed.
|
||||
if (isTopDown) {
|
||||
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
||||
} else {
|
||||
ctx.setTransform(1, 0, 0, -1, 0, height);
|
||||
}
|
||||
if (decoder) { try { decoder.close(); } catch(e) {} }
|
||||
// Reset FPS sliding window on decoder (re)init so a resolution
|
||||
// change or codec switch doesn't carry over stale counts.
|
||||
@@ -2811,12 +2975,14 @@
|
||||
currentHeight = height;
|
||||
decoder = new VideoDecoder({
|
||||
output: (frame) => {
|
||||
// Check if frame dimensions match canvas
|
||||
if (frame.displayWidth !== canvas.width || frame.displayHeight !== canvas.height) {
|
||||
console.warn(`Frame size mismatch: frame=${frame.displayWidth}x${frame.displayHeight}, canvas=${canvas.width}x${canvas.height}`);
|
||||
// Check if frame dimensions match the stream
|
||||
if (frame.displayWidth !== streamW || frame.displayHeight !== streamH) {
|
||||
console.warn(`Frame size mismatch: frame=${frame.displayWidth}x${frame.displayHeight}, stream=${streamW}x${streamH}`);
|
||||
}
|
||||
ctx.drawImage(frame, 0, 0);
|
||||
// 先落到 frameCanvas(已带翻转),再按当前缩放/平移画进画布
|
||||
if (frameCtx) frameCtx.drawImage(frame, 0, 0);
|
||||
frame.close();
|
||||
renderFrame();
|
||||
// 原始风格的 FPS 计数:1 秒采样窗口
|
||||
frameCount++;
|
||||
const now = performance.now();
|
||||
@@ -5444,6 +5610,10 @@
|
||||
btnMouseBar.classList.add('ui-hidden');
|
||||
btnKeyboardBar.classList.add('ui-hidden');
|
||||
}
|
||||
// 横竖屏/全屏切换会改 .canvas-container 的 align-items(竖屏贴顶、横屏居中),
|
||||
// 画面在画布内的位置得当场重算,不能等 ResizeObserver 慢一拍。
|
||||
syncViewAlign();
|
||||
renderFrame();
|
||||
// Position input shortcuts below canvas
|
||||
updateInputShortcutsPosition();
|
||||
}
|
||||
@@ -5457,9 +5627,16 @@
|
||||
const shortcuts = uiElements.inputShortcuts;
|
||||
if (!canvas || canvas.offsetHeight === 0) return;
|
||||
|
||||
// Position shortcuts 2px below canvas
|
||||
const canvasBottom = canvas.offsetTop + canvas.offsetHeight;
|
||||
shortcuts.style.top = (canvasBottom + 2) + 'px';
|
||||
// 画布已经铺满容器,画面本身按 fit 居中,所以快捷栏要贴在「画面」的下边缘,
|
||||
// 而不是画布盒子的下边缘。
|
||||
// 还没收到首帧时 streamH 为 0,算不出画面高度,保持原位即可(放到画布正中反而更怪)。
|
||||
if (!streamH) return;
|
||||
const m = getViewMetrics();
|
||||
// 画面在画布内的实际位置由 getViewMetrics 决定(竖屏贴顶、横屏居中),
|
||||
// 这里换算成 CSS px 直接用,别再假设居中。
|
||||
const imgTop = canvas.offsetTop + m.offY / m.d;
|
||||
const imgH = streamH * (m.fit / m.d);
|
||||
shortcuts.style.top = (imgTop + imgH + 2) + 'px';
|
||||
}
|
||||
|
||||
function sendRdpReset() {
|
||||
@@ -5552,83 +5729,36 @@
|
||||
positionCursorOverlay(canvasX, canvasY);
|
||||
}
|
||||
|
||||
// Place the overlay at a canvas coordinate, applying the zoom/pan
|
||||
// transform. Shared by the touch control cursor and the non-control
|
||||
// remote cursor.
|
||||
// 远端坐标 → 屏幕坐标。.cursor-overlay 是 position:fixed,所以给的是视口坐标。
|
||||
function positionCursorOverlay(canvasX, canvasY) {
|
||||
const canvas = document.getElementById('screen-canvas');
|
||||
const cursorOverlay = document.getElementById('cursor-overlay');
|
||||
if (!canvas || !cursorOverlay) return;
|
||||
// Get canvas base position (without transform, use container + offset)
|
||||
// offsetLeft/offsetTop are layout positions, unaffected by CSS transform
|
||||
const container = document.querySelector('.canvas-container');
|
||||
const containerRect = container.getBoundingClientRect();
|
||||
const canvasDisplayWidth = canvas.offsetWidth;
|
||||
const canvasDisplayHeight = canvas.offsetHeight;
|
||||
const canvasLeft = containerRect.left + canvas.offsetLeft;
|
||||
const canvasTop = containerRect.top + canvas.offsetTop;
|
||||
|
||||
// Convert canvas coords to position on unzoomed canvas
|
||||
// Map [0, width-1] to [0, 1] for proper edge-to-edge display
|
||||
const relX = canvas.width > 1 ? canvasX / (canvas.width - 1) : 0;
|
||||
const relY = canvas.height > 1 ? canvasY / (canvas.height - 1) : 0;
|
||||
|
||||
// Position on unzoomed canvas (in pixels from canvas top-left)
|
||||
const unzoomedX = relX * canvasDisplayWidth;
|
||||
const unzoomedY = relY * canvasDisplayHeight;
|
||||
|
||||
// Apply zoom transform
|
||||
// Transform origin
|
||||
const originX = canvasDisplayWidth * zoomState.originX / 100;
|
||||
const originY = canvasDisplayHeight * zoomState.originY / 100;
|
||||
|
||||
// Scale around origin, then translate
|
||||
const scaledX = originX + (unzoomedX - originX) * zoomState.scale + zoomState.translateX * zoomState.scale;
|
||||
const scaledY = originY + (unzoomedY - originY) * zoomState.scale + zoomState.translateY * zoomState.scale;
|
||||
|
||||
// Final screen position
|
||||
const screenX = canvasLeft + scaledX;
|
||||
const screenY = canvasTop + scaledY;
|
||||
|
||||
cursorOverlay.style.left = screenX + 'px';
|
||||
cursorOverlay.style.top = screenY + 'px';
|
||||
if (!cursorOverlay || !streamW || !streamH) return;
|
||||
const r = canvasPixelRatio();
|
||||
const m = getViewMetrics();
|
||||
const s = zoomState.scale;
|
||||
// 与 renderFrame 的变换链保持一致:fit 居中 → 绕捏合中心缩放 → 平移
|
||||
const o = zoomOriginPx(m);
|
||||
const ox = o.x, oy = o.y;
|
||||
const bx = ox + s * (m.fit * canvasX + m.offX + zoomState.translateX * m.d - ox);
|
||||
const by = oy + s * (m.fit * canvasY + m.offY + zoomState.translateY * m.d - oy);
|
||||
cursorOverlay.style.left = (r.rect.left + bx / r.kx) + 'px';
|
||||
cursorOverlay.style.top = (r.rect.top + by / r.ky) + 'px';
|
||||
}
|
||||
|
||||
// Inverse transform: screen position -> canvas coordinates
|
||||
// Used to recalculate cursor position after zoom/pan
|
||||
// 屏幕坐标 → 远端坐标(positionCursorOverlay 的逆运算)
|
||||
function screenToCanvas(screenX, screenY) {
|
||||
const canvas = document.getElementById('screen-canvas');
|
||||
// Get canvas base position (without transform, use container + offset)
|
||||
const container = document.querySelector('.canvas-container');
|
||||
const containerRect = container.getBoundingClientRect();
|
||||
const canvasDisplayWidth = canvas.offsetWidth;
|
||||
const canvasDisplayHeight = canvas.offsetHeight;
|
||||
const canvasLeft = containerRect.left + canvas.offsetLeft;
|
||||
const canvasTop = containerRect.top + canvas.offsetTop;
|
||||
|
||||
// Reverse the transform chain
|
||||
const scaledX = screenX - canvasLeft;
|
||||
const scaledY = screenY - canvasTop;
|
||||
|
||||
const originX = canvasDisplayWidth * zoomState.originX / 100;
|
||||
const originY = canvasDisplayHeight * zoomState.originY / 100;
|
||||
|
||||
// Reverse: scaledX = originX + (unzoomedX - originX) * scale + translateX * scale
|
||||
const unzoomedX = (scaledX - originX) / zoomState.scale + originX - zoomState.translateX;
|
||||
const unzoomedY = (scaledY - originY) / zoomState.scale + originY - zoomState.translateY;
|
||||
|
||||
// Reverse: unzoomedX = relX * canvasDisplayWidth
|
||||
const relX = canvasDisplayWidth > 0 ? unzoomedX / canvasDisplayWidth : 0;
|
||||
const relY = canvasDisplayHeight > 0 ? unzoomedY / canvasDisplayHeight : 0;
|
||||
|
||||
// Reverse: relX = canvasX / (canvas.width - 1)
|
||||
const canvasX = relX * (canvas.width - 1);
|
||||
const canvasY = relY * (canvas.height - 1);
|
||||
|
||||
// Clamp to valid range
|
||||
return {
|
||||
x: Math.max(0, Math.min(canvas.width - 1, canvasX)),
|
||||
y: Math.max(0, Math.min(canvas.height - 1, canvasY))
|
||||
if (!streamW || !streamH) return { x: 0, y: 0 };
|
||||
const r = canvasPixelRatio();
|
||||
const m = getViewMetrics();
|
||||
const s = zoomState.scale;
|
||||
const px = (screenX - r.rect.left) * r.kx;
|
||||
const py = (screenY - r.rect.top) * r.ky;
|
||||
const o = zoomOriginPx(m);
|
||||
const ox = o.x, oy = o.y;
|
||||
const qx = ((px - ox) / s + ox - m.offX - zoomState.translateX * m.d) / (m.fit || 1);
|
||||
const qy = ((py - oy) / s + oy - m.offY - zoomState.translateY * m.d) / (m.fit || 1); return {
|
||||
x: Math.max(0, Math.min(streamW - 1, qx)),
|
||||
y: Math.max(0, Math.min(streamH - 1, qy))
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5649,26 +5779,8 @@
|
||||
const SCROLL_DEADZONE = 28; // Minimum finger-Y delta (px) to send one wheel notch
|
||||
|
||||
// Pinch-to-zoom state
|
||||
let zoomState = {
|
||||
scale: 1,
|
||||
minScale: 1,
|
||||
maxScale: 4,
|
||||
translateX: 0,
|
||||
translateY: 0,
|
||||
lastPinchDist: 0,
|
||||
isPinching: false,
|
||||
pinchCenterX: 0,
|
||||
pinchCenterY: 0,
|
||||
// Transform origin relative to canvas (percentage)
|
||||
originX: 50,
|
||||
originY: 50,
|
||||
// Two-finger gesture detection
|
||||
hasZoomed: false, // Whether zoom occurred in current gesture
|
||||
lastScrollY: 0, // For scroll delta calculation
|
||||
initialPinchDist: 0, // Distance at gesture start (for cumulative detection)
|
||||
initialCenterX: 0, // Pinch center at gesture start (for scroll-vs-zoom intent)
|
||||
initialCenterY: 0
|
||||
};
|
||||
// 注意:这个对象在文件靠上的渲染模型里就要用(renderFrame 会被 ResizeObserver
|
||||
// 触发),所以实际声明已上移到 canvas 定义之后,这里不再重复声明。
|
||||
const zoomIndicator = document.getElementById('zoom-indicator');
|
||||
let zoomIndicatorTimer = null;
|
||||
|
||||
@@ -5693,11 +5805,8 @@
|
||||
let prevScale = 1;
|
||||
|
||||
function applyZoomTransform() {
|
||||
const container = document.querySelector('.canvas-container');
|
||||
if (zoomState.scale === 1) {
|
||||
canvas.style.transform = '';
|
||||
canvas.style.transformOrigin = '';
|
||||
// Reset all zoom state when scale returns to 1
|
||||
// 回到 100%:复位到 fit 居中视图
|
||||
zoomState.originX = 50;
|
||||
zoomState.originY = 50;
|
||||
zoomState.translateX = 0;
|
||||
@@ -5710,26 +5819,10 @@
|
||||
prevScale = 1;
|
||||
} else {
|
||||
prevScale = zoomState.scale;
|
||||
// Clamp translate to prevent canvas from going out of view
|
||||
const rect = container.getBoundingClientRect();
|
||||
const maxTranslateX = (canvas.width * zoomState.scale - rect.width) / 2 / zoomState.scale;
|
||||
const maxTranslateY = (canvas.height * zoomState.scale - rect.height) / 2 / zoomState.scale;
|
||||
|
||||
if (canvas.width * zoomState.scale > rect.width) {
|
||||
zoomState.translateX = Math.max(-maxTranslateX, Math.min(maxTranslateX, zoomState.translateX));
|
||||
} else {
|
||||
zoomState.translateX = 0;
|
||||
}
|
||||
if (canvas.height * zoomState.scale > rect.height) {
|
||||
zoomState.translateY = Math.max(-maxTranslateY, Math.min(maxTranslateY, zoomState.translateY));
|
||||
} else {
|
||||
zoomState.translateY = 0;
|
||||
}
|
||||
|
||||
// Use pinch center as transform origin
|
||||
canvas.style.transformOrigin = zoomState.originX + '% ' + zoomState.originY + '%';
|
||||
canvas.style.transform = 'scale(' + zoomState.scale + ') translate(' + zoomState.translateX + 'px, ' + zoomState.translateY + 'px)';
|
||||
}
|
||||
// 不再给 canvas 加 CSS transform:元素不动,缩放平移全在画布内部绘制。
|
||||
// 因此也没有平移边界限制 —— 拖到画面外就是画布自己的黑底,手感与以前一致。
|
||||
renderFrame();
|
||||
// 非控制态:缩放/平移后远程光标要跟着画面走
|
||||
if (!controlEnabled) renderRemoteCursor();
|
||||
}
|
||||
@@ -5861,9 +5954,9 @@
|
||||
|
||||
// Initialize cursor to center of screen
|
||||
function initCursor() {
|
||||
if (!cursorState.initialized && canvas.width > 0) {
|
||||
cursorState.x = Math.round(canvas.width / 2);
|
||||
cursorState.y = Math.round(canvas.height / 2);
|
||||
if (!cursorState.initialized && streamW > 0 && streamH > 0) {
|
||||
cursorState.x = Math.round(streamW / 2);
|
||||
cursorState.y = Math.round(streamH / 2);
|
||||
cursorState.initialized = true;
|
||||
updateCursorOverlay(cursorState.x, cursorState.y);
|
||||
}
|
||||
@@ -5872,10 +5965,11 @@
|
||||
// Move cursor by delta (touchpad mode)
|
||||
function moveCursorBy(dx, dy) {
|
||||
initCursor();
|
||||
if (!streamW || !streamH) return;
|
||||
// Sensitivity multiplier (adjust for comfortable control)
|
||||
const sensitivity = 1.5;
|
||||
cursorState.x = Math.max(0, Math.min(canvas.width - 1, cursorState.x + dx * sensitivity));
|
||||
cursorState.y = Math.max(0, Math.min(canvas.height - 1, cursorState.y + dy * sensitivity));
|
||||
cursorState.x = Math.max(0, Math.min(streamW - 1, cursorState.x + dx * sensitivity));
|
||||
cursorState.y = Math.max(0, Math.min(streamH - 1, cursorState.y + dy * sensitivity));
|
||||
updateCursorOverlay(cursorState.x, cursorState.y);
|
||||
// Send move to remote
|
||||
sendMouse('move', Math.round(cursorState.x), Math.round(cursorState.y), 0);
|
||||
@@ -5898,13 +5992,8 @@
|
||||
}
|
||||
|
||||
function getTouchPos(touch) {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const scaleX = canvas.width / rect.width;
|
||||
const scaleY = canvas.height / rect.height;
|
||||
return {
|
||||
x: Math.round((touch.clientX - rect.left) * scaleX),
|
||||
y: Math.round((touch.clientY - rect.top) * scaleY)
|
||||
};
|
||||
const p = screenToCanvas(touch.clientX, touch.clientY);
|
||||
return { x: Math.round(p.x), y: Math.round(p.y) };
|
||||
}
|
||||
|
||||
function sendMouse(type, x, y, button, delta, deltaMode) {
|
||||
@@ -6102,9 +6191,16 @@
|
||||
zoomState.lastScrollY = center.y;
|
||||
|
||||
if (zoomState.scale === 1) {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const relX = (center.x - rect.left) / rect.width * 100;
|
||||
const relY = (center.y - rect.top) / rect.height * 100;
|
||||
// 原点按「画面」的百分比算,不是画布的百分比 —— 旧版 transform-origin
|
||||
// 落在 canvas 元素(即画面)上,捏合时以手指下的画面点为锚点放大。
|
||||
// 画布铺满容器后画面只占其中一块(竖屏贴顶时尤其悬殊),照抄画布百分比会偏。
|
||||
const r = canvasPixelRatio();
|
||||
const m = getViewMetrics();
|
||||
const px = (center.x - r.rect.left) * r.kx;
|
||||
const py = (center.y - r.rect.top) * r.ky;
|
||||
const imgW = streamW * m.fit, imgH = streamH * m.fit;
|
||||
const relX = imgW > 0 ? (px - m.offX) / imgW * 100 : 50;
|
||||
const relY = imgH > 0 ? (py - m.offY) / imgH * 100 : 50;
|
||||
zoomState.originX = Math.max(0, Math.min(100, relX));
|
||||
zoomState.originY = Math.max(0, Math.min(100, relY));
|
||||
}
|
||||
@@ -6155,9 +6251,9 @@
|
||||
clickAtCursor(0);
|
||||
console.log('[Touch] Second tap too far (' + Math.round(dist) + 'px), new first tap');
|
||||
// Move cursor to new position
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
cursorState.x = Math.max(0, Math.min(canvas.width - 1, cursorState.x + dx * canvas.width / rect.width * 1.5));
|
||||
cursorState.y = Math.max(0, Math.min(canvas.height - 1, cursorState.y + dy * canvas.height / rect.height * 1.5));
|
||||
const perPx = remotePerScreenPx() * 1.5;
|
||||
cursorState.x = Math.max(0, Math.min(streamW - 1, cursorState.x + dx * perPx));
|
||||
cursorState.y = Math.max(0, Math.min(streamH - 1, cursorState.y + dy * perPx));
|
||||
updateCursorOverlay(cursorState.x, cursorState.y);
|
||||
touchState.state = T_FIRST_DOWN;
|
||||
// Set up long press for right click
|
||||
@@ -6314,9 +6410,10 @@
|
||||
}
|
||||
}
|
||||
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const canvasDx = dx * canvas.width / rect.width;
|
||||
const canvasDy = dy * canvas.height / rect.height;
|
||||
// 屏幕位移 → 远端位移(含当前缩放,缩得越大光标移动越慢)
|
||||
const perPx = remotePerScreenPx();
|
||||
const canvasDx = dx * perPx;
|
||||
const canvasDy = dy * perPx;
|
||||
|
||||
// State machine transitions and actions
|
||||
if (touchState.state === T_FIRST_DOWN && touchState.moved) {
|
||||
@@ -6502,13 +6599,8 @@
|
||||
let lastMouseX = 0, lastMouseY = 0;
|
||||
|
||||
function getMousePos(e) {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const scaleX = canvas.width / rect.width;
|
||||
const scaleY = canvas.height / rect.height;
|
||||
return {
|
||||
x: Math.round((e.clientX - rect.left) * scaleX),
|
||||
y: Math.round((e.clientY - rect.top) * scaleY)
|
||||
};
|
||||
const p = screenToCanvas(e.clientX, e.clientY);
|
||||
return { x: Math.round(p.x), y: Math.round(p.y) };
|
||||
}
|
||||
|
||||
canvas.addEventListener('mousedown', function(e) {
|
||||
@@ -6693,9 +6785,9 @@
|
||||
zoomState.scale = 1;
|
||||
zoomState.translateX = 0;
|
||||
zoomState.translateY = 0;
|
||||
zoomState.originX = 50;
|
||||
zoomState.originY = 50;
|
||||
zoomState.isPinching = false;
|
||||
canvas.style.transform = '';
|
||||
canvas.style.transformOrigin = '';
|
||||
|
||||
if (decoder) { try { decoder.close(); } catch(e) {} decoder = null; }
|
||||
// 墙内设备仍可能被订阅(tile 还在),音频必须显式停止。
|
||||
|
||||
Reference in new issue
Block a user