Skip to content

Instantly share code, notes, and snippets.

@berkaytumal
Created April 21, 2023 01:32
Show Gist options
  • Save berkaytumal/0e3dc0caaf74f101cbe92c5ab5974167 to your computer and use it in GitHub Desktop.
Save berkaytumal/0e3dc0caaf74f101cbe92c5ab5974167 to your computer and use it in GitHub Desktop.
Firefox Context2D RoundRect Polyfill
var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
if (isFirefox) {
CanvasRenderingContext2D.prototype.roundRect = function (
x,
y,
width,
height,
radius = 5,
fill = false,
stroke = true
) {
const ctx = this
if (typeof radius === 'number') {
radius = { tl: radius, tr: radius, br: radius, bl: radius };
} else {
radius = { ...{ tl: 0, tr: 0, br: 0, bl: 0 }, ...radius };
}
console.log(ctx)
ctx.beginPath();
ctx.moveTo(x + radius.tl, y);
ctx.lineTo(x + width - radius.tr, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius.tr);
ctx.lineTo(x + width, y + height - radius.br);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius.br, y + height);
ctx.lineTo(x + radius.bl, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius.bl);
ctx.lineTo(x, y + radius.tl);
ctx.quadraticCurveTo(x, y, x + radius.tl, y);
ctx.closePath();
if (fill) {
ctx.fill();
}
if (stroke) {
ctx.stroke();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment