Drawing Pixel Art
Drawing Pixel Art
Creates 8-bit style pixel art using only vanilla JavaScript and the Canvas API. No libraries, no build tools, no dependencies — just a <canvas> element and code.
JavaScript// Minimal setup: a scalable pixel grid renderer const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); ctx.imageSmoothingEnabled = false; // crisp pixels, no blur const PIXEL_SIZE = 10; // each "pixel" is rendered as a 10x10 block const GRID = 16; // 16x16 sprite grid canvas.width = GRID * PIXEL_SIZE; canvas.height = GRID * PIXEL_SIZE; function drawPixel(x, y, color) { ctx.fillStyle = color; ctx.fillRect(x * PIXEL_SIZE, y * PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE); } // Draw a simple heart sprite using a 2D array (0 = transparent, 1 = color) const heart = [ [0,1,1,0,1,1,0], [1,1,1,1,1,1,1], [1,1,1,1,1,1,1], [0,1,1,1,1,1,0], [0,0,1,1,1,0,0], [0,0,0,1,0,0,0], ]; heart.forEach((row, y) => { row.forEach((val, x) => { if (val) drawPixel(x, y, '#e63946'); }); });
Progress:
- Step 1: Set up canvas with disabled image smoothing
- Step 2: Define a pixel grid (2D array or sprite map)
- Step 3: Choose a limited color palette (true 8-bit uses 4-16 colors)
- Step 4: Write a
drawPixel(x, y, color)helper that scales logical pixels to canvas blocks - Step 5: Render the sprite by iterating the grid
- Step 6: (Optional) Add animation via
requestAnimationFrameswapping frames/grids - Step 7: (Optional) Export as PNG via
canvas.toDataURL()
1. Canvas setup (always do this first)
JavaScriptctx.imageSmoothingEnabled = false; ctx.webkitImageSmoothingEnabled = false; // legacy Safari
This is the single most important line — without it, scaled pixel art looks blurry instead of crisp.
2. Represent sprites as data, not draw calls
Use arrays of indices mapped to a palette. This separates art data from rendering logic and makes sprites easy to edit, mirror, or animate.
JavaScriptconst palette = { 0: null, // transparent 1: '#000000', // outline 2: '#ffcc99', // skin 3: '#5555ff', // clothes }; const sprite = [ [0,1,1,1,1,0], [1,2,2,2,2,1], [1,2,1,2,1,1], [0,1,3,3,1,0], [0,1,3,3,1,0], [0,0,1,1,0,0], ]; function drawSprite(grid, paletteMap, offsetX = 0, offsetY = 0) { grid.forEach((row, y) => { row.forEach((idx, x) => { const color = paletteMap[idx]; if (color) drawPixel(x + offsetX, y + offsetY, color); }); }); }
3. Animate with frame arrays
JavaScriptconst frames = [frame1Grid, frame2Grid, frame3Grid]; let frameIndex = 0; function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawSprite(frames[frameIndex], palette); frameIndex = (frameIndex + 1) % frames.length; setTimeout(() => requestAnimationFrame(animate), 200); // ~5 fps, classic 8-bit feel } animate();
4. Mirror sprites (save half the data)
JavaScriptfunction mirrorGrid(grid) { return grid.map(row => [...row].reverse()); }
Example 1: Input: "Draw an 8-bit style coin icon" Output:
JavaScriptconst coin = [ [0,0,1,1,1,1,0,0], [0,1,2,2,2,2,1,0], [1,2,2,3,2,2,2,1], [1,2,3,3,3,2,2,1], [1,2,2,3,2,2,2,1], [1,2,2,2,2,2,2,1], [0,1,2,2,2,2,1,0], [0,0,1,1,1,1,0,0], ]; const palette = { 0: null, 1: '#7a5c00', 2: '#ffd700', 3: '#fff3b0' }; drawSprite(coin, palette);
Example 2:
Input: "Make a walk-cycle animation for a tiny character"
Output: Define 2-4 grids with leg/arm positions shifted, store in frames[], call the animate() loop pattern shown above at 150-250ms per frame.
Example 3: Input: "Export the pixel art as a downloadable image" Output:
JavaScriptconst link = document.createElement('a'); link.download = 'sprite.png'; link.href = canvas.toDataURL('image/png'); link.click();
- Disable smoothing always — this is non-negotiable for the 8-bit look.
- Keep palettes small (4-16 colors) for authenticity; store as a lookup object, not raw hex scattered in code.
- Use power-of-two grid sizes (8x8, 16x16, 32x32) matching classic sprite conventions.
- Separate data from rendering — grids/palettes are pure data;
drawPixel/drawSpriteare the only functions touching canvas. - Scale via integer
PIXEL_SIZEonly — non-integer scaling reintroduces blur/artifacts. - For transparency, use
null/0in the grid and skipfillRectrather than drawing white. - For sprite sheets, pack multiple grids into one canvas using offsetX/offsetY rather than multiple canvases.
- Forgetting
imageSmoothingEnabled = false— results in blurry, non-pixelated output defeating the entire aesthetic. - Using CSS
width/heightto scale canvas without also setting matching canvaswidth/heightattributes — causes blurry stretching. Scale viaPIXEL_SIZEin drawing logic instead, or if scaling via CSS, also setimage-rendering: pixelatedin CSS. - Hardcoding colors directly in draw calls instead of a palette object — makes recoloring/theming painful.
- Redrawing the entire canvas unnecessarily every frame when only part of the sprite changes — fine for small sprites, but inefficient for larger scenes.
- Non-square pixels from mismatched width/height scale factors — always use one
PIXEL_SIZEfor both axes unless intentionally distorting.