Last active
April 16, 2019 16:16
-
-
Save cmargroff/8195de2692026bd1b810e47af9414156 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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