Skip to content

Instantly share code, notes, and snippets.

@cmargroff
Last active April 16, 2019 16:16
Show Gist options
  • Save cmargroff/8195de2692026bd1b810e47af9414156 to your computer and use it in GitHub Desktop.
Save cmargroff/8195de2692026bd1b810e47af9414156 to your computer and use it in GitHub Desktop.
CanvasRenderingContext2D.prototype.roundedRect = function(x: number, y: number, width: number, height: number, radius: number) {
const points = [
[x + radius, y + radius],
[x + width - radius, y + radius],
[x + width - radius, y + height - radius],
[x + radius, y + height - radius]
]
let startAngle = 1 * Math.PI
points.forEach((set, i) => {
this.arc(set[0], set[1], radius, startAngle, startAngle + 0.5 * Math.PI)
startAngle += 0.5* Math.PI
if(startAngle >= 2 * Math.PI){
startAngle = 0
}
})
}
CanvasRenderingContext2D.prototype.fillRoundedRect = function (x: number, y: number, width: number, height: number, radius: number) {
this.beginPath()
this.roundedRect(x, y, width, height, radius)
this.closePath()
this.fill()
}
CanvasRenderingContext2D.prototype.strokeRoundedRect = function (x: number, y: number, width: number, height: number, radius: number) {
this.beginPath()
this.roundedRect(x, y, width, height, radius)
this.closePath()
this.stroke()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment