Code

Canvas

Colors & Gradients

Beginner Solid fills, linear gradients, radial gradients, conic gradients, drop shadows, and globalAlpha. MDN Reference

// Linear gradient
const linGrad = ctx.createLinearGradient(0, 0, 460, 0);
linGrad.addColorStop(0,   '#6366f1');
linGrad.addColorStop(0.5, '#ec4899');
linGrad.addColorStop(1,   '#f59e0b');
ctx.fillStyle = linGrad;
ctx.fillRect(10, 10, 440, 70);

// Radial gradient
const radGrad = ctx.createRadialGradient(
  175, 250, 10,   // inner circle
  175, 250, 80    // outer circle
);
radGrad.addColorStop(0,   '#fef08a');
radGrad.addColorStop(0.5, '#f97316');
radGrad.addColorStop(1,   'rgba(239,68,68,0)');
ctx.fillStyle = radGrad;
ctx.beginPath();
ctx.arc(175, 250, 80, 0, Math.PI * 2);
ctx.fill();

// Conic gradient
const conicGrad = ctx.createConicGradient(0, 350, 250);
conicGrad.addColorStop(0,    '#a855f7');
conicGrad.addColorStop(0.25, '#3b82f6');
conicGrad.addColorStop(0.5,  '#22c55e');
conicGrad.addColorStop(0.75, '#facc15');
conicGrad.addColorStop(1,    '#a855f7');
ctx.fillStyle = conicGrad;
ctx.beginPath();
ctx.arc(350, 250, 80, 0, Math.PI * 2);
ctx.fill();

// Drop shadow
ctx.shadowColor   = 'rgba(0,0,0,0.5)';
ctx.shadowBlur    = 15;
ctx.shadowOffsetX = 6;
ctx.shadowOffsetY = 6;
ctx.fillStyle = '#6366f1';
ctx.fillRect(10, 175, 120, 60);
ctx.shadowColor = 'transparent'; // always reset!

// Overlapping semi-transparent circles
ctx.globalAlpha = 0.6;
for (const [x, y, c] of circles) {
  ctx.beginPath();
  ctx.arc(x, y, 45, 0, Math.PI * 2);
  ctx.fillStyle = c;
  ctx.fill();
}
ctx.globalAlpha = 1;
Key APIs
  • ctx.createLinearGradient(x0,y0,x1,y1) — gradient along a line
  • ctx.createRadialGradient(x0,y0,r0,x1,y1,r1) — between two circles
  • ctx.createConicGradient(angle, cx, cy) — sweeps around a point
  • gradient.addColorStop(offset, color) — 0 = start, 1 = end
  • ctx.shadowColor / shadowBlur / shadowOffsetX / Y — drop shadow
  • ctx.globalAlpha — 0 (transparent) → 1 (opaque) for all drawing