front/src/views/site/SiteDotGrid.vue
zzh 4ff22fed57
Some checks failed
Build and Deploy Front / deploy (push) Failing after 12s
feat: Nana 升级为 Edi,H5 仓操接入装袋打板并补分析页
助手改为浮窗 Edi、运营/客户分流;官网跑道虹膜后重播;H5 FAB 增加装袋/打板原生页与 3D 入口图标;并带上财务/营销/代理分析相关改动。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-11 14:33:27 +08:00

300 lines
8.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div ref="rootRef" class="ai-dot-grid" aria-hidden="true">
<canvas ref="canvasRef" class="ai-dot-grid__canvas" />
</div>
</template>
<script lang="ts" setup>
/**
* 交互点阵:鼠标附近点被推开形变(React Bits Dot Field 一类)
* - 静止时停帧;离开时影响力衰减回弹,避免瞬移
* - 单次遍历 + fillRect,避免双遍 arc
*/
import { onMounted, onUnmounted, ref, watch } from 'vue';
import { isPortalIrisActive } from '@/utils/page-transition-flag';
defineOptions({ name: 'SiteDotGrid' });
const props = withDefaults(
defineProps<{
paused?: boolean;
}>(),
{ paused: false },
);
const rootRef = ref<HTMLDivElement | null>(null);
const canvasRef = ref<HTMLCanvasElement | null>(null);
let teardown: (() => void) | null = null;
let setPaused: ((v: boolean) => void) | null = null;
onMounted(() => {
const root = rootRef.value;
const canvas = canvasRef.value;
if (!root || !canvas) return;
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const isNarrow = window.matchMedia('(max-width: 860px)').matches;
const allowPointer =
!reduceMotion && !isNarrow && window.matchMedia('(pointer: fine)').matches;
const ctx = canvas.getContext('2d', { alpha: false });
if (!ctx) return;
const gap = isNarrow ? 20 : 18;
const dot = isNarrow ? 1.8 : 2;
const influence = 120;
const strength = 18;
const dprCap = 1.25;
/** 上下各约 18% 渐隐,中间才有点阵 */
const edgePad = 0.18;
let width = 0;
let height = 0;
let dpr = 1;
let cols = 0;
let rows = 0;
let bases: Float32Array | null = null;
let rootRect = { left: 0, top: 0 };
const mouse = { x: 0, y: 0, tx: 0, ty: 0, active: false };
let influenceAmt = 0; // 0..1,离开后衰减
let raf = 0;
let isVisible = true;
let isPageVisible = !document.hidden;
let isPaused = props.paused;
let pointerLast = 0;
let moving = false;
const syncRect = () => {
const rect = root.getBoundingClientRect();
rootRect = { left: rect.left, top: rect.top };
};
const rebuild = () => {
const rect = root.getBoundingClientRect();
rootRect = { left: rect.left, top: rect.top };
width = Math.max(1, Math.floor(rect.width));
height = Math.max(1, Math.floor(rect.height));
dpr = Math.min(window.devicePixelRatio || 1, dprCap);
canvas.width = Math.floor(width * dpr);
canvas.height = Math.floor(height * dpr);
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
cols = Math.ceil(width / gap) + 1;
rows = Math.ceil(height / gap) + 1;
bases = new Float32Array(cols * rows * 2);
const ox = (width - (cols - 1) * gap) / 2;
const oy = (height - (rows - 1) * gap) / 2;
let i = 0;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
bases[i++] = ox + c * gap;
bases[i++] = oy + r * gap;
}
}
};
const paint = () => {
if (!bases) return;
mouse.x += (mouse.tx - mouse.x) * 0.18;
mouse.y += (mouse.ty - mouse.y) * 0.18;
const targetInf = mouse.active ? 1 : 0;
influenceAmt += (targetInf - influenceAmt) * 0.12;
ctx.fillStyle = '#eef1f4';
ctx.fillRect(0, 0, width, height);
const mx = mouse.x;
const my = mouse.y;
const inf = influence * Math.max(influenceAmt, 0.001);
const inf2 = inf * inf;
const pushK = strength * influenceAmt;
const live = influenceAmt > 0.02;
const total = cols * rows;
const topBand = height * edgePad;
const botBand = height * (1 - edgePad);
for (let i = 0; i < total; i++) {
const ix = i * 2;
const by = bases[ix + 1];
// 上下留白:边缘外不画,边缘内渐隐
let edge = 1;
if (by < topBand) edge = by / topBand;
else if (by > botBand) edge = (height - by) / (height - botBand);
if (edge < 0.06) continue;
edge = edge * edge; // 更利落地收掉上下
let x = bases[ix];
let y = by;
let s = dot;
let hot = false;
if (live) {
const dx = x - mx;
const dy = y - my;
const d2 = dx * dx + dy * dy;
if (d2 < inf2 && d2 > 0.01) {
const d = Math.sqrt(d2);
const t = 1 - d / inf;
const fall = t * t;
const push = pushK * fall;
x += (dx / d) * push;
y += (dy / d) * push;
s = dot * (1 + fall * 0.55);
hot = fall > 0.4;
}
}
const a = (hot ? 0.22 : 0.12) * edge;
ctx.fillStyle = hot
? `rgba(2, 62, 138, ${a})`
: `rgba(15, 23, 42, ${a})`;
ctx.fillRect(x - s / 2, y - s / 2, s, s);
}
};
const tryStop = () => {
if (raf !== 0) {
cancelAnimationFrame(raf);
raf = 0;
}
};
const stillSettling = () =>
Math.abs(mouse.tx - mouse.x) > 0.15 ||
Math.abs(mouse.ty - mouse.y) > 0.15 ||
Math.abs((mouse.active ? 1 : 0) - influenceAmt) > 0.01;
const loop = () => {
raf = 0;
// 门户虹膜过渡中挂起;结束后继续,避免点阵永久停在首帧
if (isPortalIrisActive()) {
raf = requestAnimationFrame(loop);
return;
}
paint();
if (!isPaused && isVisible && isPageVisible && (moving || stillSettling())) {
moving = false;
raf = requestAnimationFrame(loop);
}
};
const tryStart = () => {
if (isPaused || !isVisible || !isPageVisible) {
paint();
return;
}
if (reduceMotion || !allowPointer) {
paint();
return;
}
if (raf === 0) raf = requestAnimationFrame(loop);
};
setPaused = (next: boolean) => {
isPaused = next;
if (next) {
tryStop();
mouse.active = false;
paint();
} else {
tryStart();
}
};
const onPointerMove = (e: PointerEvent) => {
if (!allowPointer) return;
const now = performance.now();
if (now - pointerLast < 14) return;
pointerLast = now;
mouse.tx = e.clientX - rootRect.left;
mouse.ty = e.clientY - rootRect.top;
mouse.active = true;
moving = true;
tryStart();
};
const onPointerLeave = () => {
mouse.active = false;
moving = true;
tryStart();
};
if (allowPointer) {
window.addEventListener('pointermove', onPointerMove, { passive: true });
window.addEventListener('blur', onPointerLeave);
// 滚轮/resize 后校准坐标
window.addEventListener('scroll', syncRect, { passive: true, capture: true });
}
const ro = new ResizeObserver(() => {
rebuild();
paint();
});
ro.observe(root);
rebuild();
paint();
const io = new IntersectionObserver(
([entry]) => {
isVisible = entry.isIntersecting;
if (isVisible) tryStart();
else tryStop();
},
{ threshold: 0 },
);
io.observe(root);
const onVisibility = () => {
isPageVisible = !document.hidden;
if (isPageVisible) tryStart();
else tryStop();
};
document.addEventListener('visibilitychange', onVisibility);
teardown = () => {
tryStop();
setPaused = null;
ro.disconnect();
io.disconnect();
document.removeEventListener('visibilitychange', onVisibility);
window.removeEventListener('pointermove', onPointerMove);
window.removeEventListener('blur', onPointerLeave);
window.removeEventListener('scroll', syncRect, true);
};
});
watch(
() => props.paused,
(v) => setPaused?.(!!v),
);
onUnmounted(() => {
teardown?.();
teardown = null;
});
</script>
<style scoped>
.ai-dot-grid {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
pointer-events: none;
background: #eef1f4;
}
.ai-dot-grid__canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
</style>