edgeScrolling via rAF, fix bug when distanceToEdge == 0

This commit is contained in:
Tomáš Mládek 2021-01-11 22:35:16 +01:00
parent 32de482742
commit f3eb39e034

View file

@ -154,46 +154,6 @@ export default defineComponent({
});
}
// Edge scrolling
const MOVE_EDGE = 75;
const MAX_SPEED = 20;
let edgeScrollInterval: number | undefined;
window.addEventListener("mousemove", (ev) => {
clearTimeout(edgeScrollInterval);
edgeScrollInterval = undefined;
if (!document.fullscreenElement) {
return;
}
let horizontalShift: number;
let verticalShift: number;
const transform = pz.getTransform();
if (ev.clientX < MOVE_EDGE || ev.clientX > window.innerWidth - MOVE_EDGE) {
const horizontalEdgeDistance =
(ev.clientX < window.innerWidth / 2) ? ev.clientX : (ev.clientX - window.innerWidth);
const horizontalRatio = (MOVE_EDGE - Math.abs(horizontalEdgeDistance)) / MOVE_EDGE;
horizontalShift = horizontalRatio * Math.sign(horizontalEdgeDistance) * MAX_SPEED;
} else {
horizontalShift = 0;
}
if (ev.clientY < MOVE_EDGE || ev.clientY > window.innerHeight - MOVE_EDGE) {
const verticalEdgeDistance =
(ev.clientY < window.innerHeight / 2) ? ev.clientY : (ev.clientY - window.innerHeight);
const verticalRatio = (MOVE_EDGE - Math.abs(verticalEdgeDistance)) / MOVE_EDGE;
verticalShift = verticalRatio * Math.sign(verticalEdgeDistance) * MAX_SPEED;
} else {
verticalShift = 0;
}
if (horizontalShift || verticalShift) {
edgeScrollInterval = setInterval(() => {
pz.moveTo(transform!.x + horizontalShift, transform!.y + verticalShift);
}, 1000 / 60);
}
});
// Anchors
anchors.value = processAnchors(svg);
panToAnchor.value = (anchor: SVGRectElement) => {
@ -205,20 +165,73 @@ export default defineComponent({
// Videoscrolls
scrolls.value = await processScrolls(svg);
});
if (process.env.VUE_APP_DEMO) {
const stats = new Stats();
document.body.appendChild(stats.dom);
function noop() {
stats.begin();
stats.end();
requestAnimationFrame(noop);
// Debug Stats
let stats: Stats | undefined;
if (process.env.VUE_APP_DEMO) {
stats = new Stats();
document.body.appendChild(stats.dom);
} else {
Array.from(document.body.getElementsByClassName("dev")).forEach((el) => {
(el as HTMLElement).style.display = "none";
});
}
requestAnimationFrame(noop);
}
// Animations: FPS Counter, Edge scrolling
let mouse: MouseEvent | undefined;
window.addEventListener("mousemove", (ev) => {
mouse = ev;
});
function animate() {
if (stats) {
stats.begin();
}
// Edge scrolling
const MOVE_EDGE = 75;
const MAX_SPEED = 20;
if (document.fullscreenElement && mouse) {
let horizontalShift: number;
let verticalShift: number;
const transform = pz.getTransform();
if (mouse.clientX < MOVE_EDGE || mouse.clientX > window.innerWidth - MOVE_EDGE) {
const horizontalEdgeDistance =
(mouse.clientX < window.innerWidth / 2) ? mouse.clientX : (mouse.clientX - window.innerWidth);
const horizontalRatio = (MOVE_EDGE - Math.abs(horizontalEdgeDistance)) / MOVE_EDGE;
const direction = mouse.clientX < MOVE_EDGE ? 1 : -1;
horizontalShift = horizontalRatio * direction * MAX_SPEED;
} else {
horizontalShift = 0;
}
if (mouse.clientY < MOVE_EDGE || mouse.clientY > window.innerHeight - MOVE_EDGE) {
const verticalEdgeDistance =
(mouse.clientY < window.innerHeight / 2) ? mouse.clientY : (mouse.clientY - window.innerHeight);
const verticalRatio = (MOVE_EDGE - Math.abs(verticalEdgeDistance)) / MOVE_EDGE;
const direction = mouse.clientY < MOVE_EDGE ? 1 : -1;
verticalShift = verticalRatio * direction * MAX_SPEED;
} else {
verticalShift = 0;
}
if (horizontalShift || verticalShift) {
pz.moveTo(transform!.x + horizontalShift, transform!.y + verticalShift);
}
}
if (stats) {
stats.end();
}
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
});
return {
root,