Hey folks, I’m working on a little canvas pixel-art game and I’m trying to make camera + sprite motion feel smooth without the art turning into blurry mush on high refresh monitors, and the tradeoff is jittery snapping vs smeary filtering.
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
let camX = 0;
let last = performance.now();
function frame(t) {
const dt = (t - last) / 1000;
last = t;
camX += 60 * dt; // pixels per second
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
// draw at subpixel camera positions
ctx.setTransform(1, 0, 0, 1, -camX, 0);
ctx.drawImage(spriteSheet, 32, 0, 16, 16, 100, 50, 16, 16);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
Should I be rounding the camera/sprite positions to integers every frame (accepting tiny stutter), or is there a better pattern like rendering to an integer-scaled offscreen buffer and compositing, so motion stays smooth but pixels stay crisp?
Quelly