references/techniques.md · 24000
padAPI.buttonsCache.includes(button)) { newPress = false; } return newPress; },
Parameters:
- `button` -- the button name to listen for.
- `hold` -- if true, holding the button counts as continuous action; if false, only new presses register.
**Usage in a game loop:**
```javascript
if (gamepadAPI.turbo) {
if (gamepadAPI.buttonPressed("A", "hold")) {
this.turbo_fire();
}
if (gamepadAPI.buttonPressed("B")) {
this.managePause();
}
}
Analog stick input with threshold (prevent stick drift):
if (gamepadAPI.axesStatus[0].x > 0.5) {
this.player.angle += 3;
this.turret.angle += 3;
}
Getting all connected gamepads:
const gamepads = navigator.getGamepads();
// Returns an array where unavailable/disconnected slots contain null
// Example with one device at index 1: [null, [object Gamepad]]
Crisp Pixel Art Look
Source: MDN - Crisp Pixel Art Look
What It Is
A technique for rendering pixel art without blurriness on high-resolution displays by mapping individual image pixels to blocks of screen pixels without smoothing interpolation. Retro pixel art requires preserving hard edges during scaling, but modern browsers default to smoothing algorithms that blend colors and create blur.
How It Works
The CSS image-rendering property controls how browsers scale images. Setting it to pixelated enforces nearest-neighbor scaling, which preserves the crisp, blocky look of pixel art instead of applying bilinear or bicubic smoothing.
Key CSS values:
pixelated-- preserves crisp edges for pixel art.crisp-edges-- alternative supported on some browsers.
When to Use It
- Retro-style games with pixel art assets.
- Any game where you want a deliberately blocky, pixelated visual style.
- When scaling small sprite images to larger display sizes.
Technique 1: Scaling <img> Elements with CSS
<img
src="character.png"
alt="pixel art character, upscaled with CSS, appearing crisp" />
img {
width: 48px;
height: 136px;
image-rendering: pixelated;
}
Technique 2: Crisp Pixel Art in Canvas
Set the canvas width/height attributes to the original pixel art resolution, then use CSS width/height for scaling (e.g., 4x scale: 128 pixels to 512px CSS width).
<canvas id="game" width="128" height="128">A cat</canvas>
canvas {
width: 512px;
height: 512px;
image-rendering: pixelated;
}
const ctx = document.getElementById("game").getContext("2d");
const image = new Image();
image.onload = () => {
ctx.drawImage(image, 0, 0);
};
image.src = "cat.png";
Technique 3: Arbitrary Canvas Scaling with Correction
For non-integer scale factors, image pixels must align to canvas pixels at integer multiples:
const ctx = document.getElementById("game").getContext("2d");
ctx.scale(0.8, 0.8);
const image = new Image();
image.onload = () => {
// Correct formula: dWidth = sWidth / xScale * n (where n is an integer)
ctx.drawImage(image, 0, 0, 128, 128, 0, 0, 128 / 0.8, 128 / 0.8);
};
image.src = "cat.png";
When using drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight):
dWidthmust equalsWidth / xScale * ndHeightmust equalsHeight / yScale * m- Where
nandmare positive integers (1, 2, 3, etc.)
Known Limitations
devicePixelRatio misalignment: When devicePixelRatio is not an integer (e.g., at 110% browser zoom), pixels may render unevenly because CSS pixels cannot perfectly map to device pixels. This creates a non-uniform appearance without an easy solution.
Best Practices
- Use integer scale factors (2x, 3x, 4x) whenever possible.
- Preserve the aspect ratio -- scale width and height equally.
- Test across different browser zoom levels.
- Avoid fractional canvas scale factors or drawImage dimensions.
- Include descriptive
aria-labelattributes on canvas elements for accessibility.