Added throttling with 16ms

This commit is contained in:
Stephan Egli 2025-01-10 12:14:31 +01:00
parent 86b7e79eb0
commit 834e19d9b8

View File

@ -9,6 +9,18 @@ let offsetY = 0;
const canvas = document.getElementById("shapesCanvas") const canvas = document.getElementById("shapesCanvas")
// Add throttle function at the top
function throttle(func, limit) {
let inThrottle;
return function(...args) {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
}
}
// 1. Init the repo & document handle // 1. Init the repo & document handle
const handle = initRepo() const handle = initRepo()
await handle.whenReady() await handle.whenReady()
@ -56,6 +68,11 @@ canvas.addEventListener("mousedown", (e) => {
} }
}); });
// Throttled version of moveShape
const throttledMoveShape = throttle((shapeId, x, y) => {
moveShape(shapeId, x, y);
}, 16); // 50ms throttle time (20 updates per second)
// Mousemove: Shape verschieben, sofern dragging aktiv ist // Mousemove: Shape verschieben, sofern dragging aktiv ist
canvas.addEventListener("mousemove", (e) => { canvas.addEventListener("mousemove", (e) => {
if (!isDragging || selectedShapeId === null) return; if (!isDragging || selectedShapeId === null) return;
@ -66,8 +83,8 @@ canvas.addEventListener("mousemove", (e) => {
const newX = mouseX - offsetX; const newX = mouseX - offsetX;
const newY = mouseY - offsetY; const newY = mouseY - offsetY;
// Achtung: Hier könnte man Throttling/RAF implementieren // Use throttled version instead
moveShape(selectedShapeId, newX, newY); throttledMoveShape(selectedShapeId, newX, newY);
}); });
// Mouseup: Dragging beenden // Mouseup: Dragging beenden