githubEdit

Canvas

Getting the context

var canvas = document.getElementById('c')
var c = canvas.getContext('2d')

Basic drawing

// x = 10, y = 20, width = 200, height = 100
c.fillStyle = '#ff0000'
c.strokeStyle = '#ff00ff'
c.lineWidth = 5
c.lineCap = 'round'
c.fillRect(10, 20, 200, 100)
c.stroke()
c.fill()

Saving and restoring

c.save()
c.restore()

Saves: strokeStyle fillStyle globalAlpha lineWidth lineCap lineJoin miterLimit shadowOffsetX shadowOffsetY shadowBlur shadowColor globalCompositeOperation, Transformations (translate rotate scale transform setTransform), Clipping path

Animation

onframe: function() {
  c.clearRect(0, 0, w, h)
}

Transformations

To rotate along origin:

To scale along origin:

See MDN: Transformationsarrow-up-right.

Image drawing

See MDN: Imagesarrow-up-right.

Colors, styles shadows

See MDN: Stylesarrow-up-right

Gradients

Drawing

More resources

Last updated