Skip to content

Instantly share code, notes, and snippets.

@mauriciomassaia
Created June 15, 2018 04:26
Show Gist options
  • Save mauriciomassaia/38a1e177fe41ee28ea7ae7971028cd9f to your computer and use it in GitHub Desktop.
Save mauriciomassaia/38a1e177fe41ee28ea7ae7971028cd9f to your computer and use it in GitHub Desktop.
Canvas 2d Mask
const size = 512
const size2 = size / 2
const c = document.createElement('canvas')
c.width = size
c.height = size
document.body.appendChild(c)
const ctx = c.getContext('2d')
ctx.fillStyle = '#ff0000'
ctx.fillRect(0, 0, size, size)
// applying the circle mask
ctx.globalCompositeOperation = 'destination-in'
ctx.fillStyle = '#000000'
ctx.arc(size2, size2, size2, 0, 2*Math.PI)
ctx.fill()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment