›Canvas art

Canvas art tricks

Game feel is mostly art direction, and canvas gives you a lot of it for free — if you dodge the five rookie mistakes. This guide is a kit of small techniques: each block stands alone, and each one raises a game from "demo" to "designed". All snippets run in Studio's preview.

Cudic Cookbook · pairs with Build a platformer

1. Crisp on retina: the device-pixel-ratio fix

A canvas sized in CSS pixels but drawn in raw pixels renders blurry on hi-DPI screens — text smears, edges fuzz. Size the backing store up, draw in CSS coordinates:

function fitCanvas(c) {
  var dpr = Math.min(devicePixelRatio || 1, 2);   // cap at 2 — beyond it, cost beats quality
  var w = c.clientWidth, h = c.clientHeight;
  c.width = w * dpr; c.height = h * dpr;
  c.getContext('2d').setTransform(dpr, 0, 0, dpr, 0, 0);
  return { w: w, h: h };
}
var size = fitCanvas(canvas);
addEventListener('resize', function () { size = fitCanvas(canvas); });

The cap at 2 matters: 3x/4x phones burn fill-rate for pixels nobody can see. Two is the sharpness sweet spot for 2D games.

2. Gradients that earn their keep

Backgrounds are where gradients go wrong — rainbow washes everywhere. Use them to model light instead: one radial gradient behind the play area, dark at the edges, and the whole scene reads as lit.

function vignette(x, ctx) {            // once per frame, or cache it offscreen
  var g = ctx.createRadialGradient(x.width / 2, x.height / 2, x.height * 0.3,
                                   x.width / 2, x.height / 2, x.height * 0.85);
  g.addColorStop(0, 'rgba(0,0,0,0)'); g.addColorStop(1, 'rgba(6,10,18,0.55)');
  ctx.fillStyle = g; ctx.fillRect(0, 0, x.width, x.height);
}

Creating gradients per-frame is real work. If a gradient never changes, draw it once into an offscreen canvas and then blur the draw:

var bg = document.createElement('canvas');
bg.width = x.width; bg.height = x.height;
vignette(bg, bg.getContext('2d'));
// per frame: ctx.drawImage(bg, 0, 0);  — one blit, no gradient math

3. Glow without WebGL

shadowBlur is the cheap neon sign. It's also slow when abused, so keep it on a few things — the ball, pickups, danger — and shadow-color matches fill-color for the sign look:

ctx.shadowColor = '#ff8c1a';
ctx.shadowBlur = 18;
ctx.fillStyle = '#ffb566';
ctx.fillRect(ball.x - 8, ball.y - 8, 16, 16);
ctx.shadowBlur = 0;                 // off again — or everything glows

Rule of thumb: at most 2–3 glowing things on screen. If four things glow, none do — the eye needs hierarchy, not a light show.

4. Particles from nothing

Explosions, sparks, dust, trails, rain — all the same 30-line system: a pool of points with position, velocity, life. Reuse a fixed array (no allocation in the loop) and respawn dead ones at the emitter:

var P = [], PN = 120;
for (var i = 0; i < PN; i++) P.push({ x: 0, y: 0, vx: 0, vy: 0, life: 0 });

function burst(px, py) {              // respawn all at once — an explosion
  for (var i = 0; i < PN; i++) {
    var a = Math.random() * Math.PI * 2, sp = 60 + Math.random() * 240;
    P[i].x = px; P[i].y = py;
    P[i].vx = Math.cos(a) * sp; P[i].vy = Math.sin(a) * sp;
    P[i].life = 0.4 + Math.random() * 0.5;
  }
}

function particles(ctx, dt) {
  ctx.fillStyle = '#ffd166';
  for (var i = 0; i < PN; i++) {
    var p = P[i];
    if (p.life <= 0) continue;
    p.life -= dt; p.x += p.vx * dt; p.y += p.vy * dt; p.vy += 300 * dt;
    ctx.globalAlpha = Math.max(p.life / 0.9, 0);      // fade out
    ctx.fillRect(p.x - 2, p.y - 2, 4, 4);
  }
  ctx.globalAlpha = 1;
}

globalAlpha per particle is the free anti-artifact: it ties particle vividness directly to remaining life. Rounds it out by keeping the same pool for trails: burst only 3–5 particles per frame at an emitter instead of 120 at once.

5. Motion that stays readable

  • Ease, never snap. A UI number that eases (shown += (target - shown) * 0.15 per frame) feels machine-polished; a number that jumps feels like a spreadsheet.
  • One shake, capped. Screen shake sells impact, but clamp to ~6px and 200ms — longer shakes read as a crash, not a hit.
  • Squash the landing. Scale the player sprite to 1.25×/0.8× for 80ms on jump/land. Two transforms and the character feels alive.
  • 60fps is a floor, not a ceiling. Budget ~7ms per frame for drawing; profile long loops first, since draw rate doesn't pay rent.

Built a look you like? Save, then Publish and watch it live in the gallery — the card art comes straight from the running preview, so scene design is your thumbnail.