Skip to content

Instantly share code, notes, and snippets.

@Sorroko
Created May 22, 2012 18:11
Show Gist options
  • Select an option

  • Save Sorroko/2770657 to your computer and use it in GitHub Desktop.

Select an option

Save Sorroko/2770657 to your computer and use it in GitHub Desktop.
Extends the Canvas Rendering context in javascript
CanvasRenderingContext2D.prototype.roundRect = function (x, y, w, h, r) {
if (w < 2 * r) r = w / 2;
if (h < 2 * r) r = h / 2;
this.beginPath();
this.moveTo(x+r, y);
this.arcTo(x+w, y, x+w, y+h, r);
this.arcTo(x+w, y+h, x, y+h, r);
this.arcTo(x, y+h, x, y, r);
this.arcTo(x, y, x+w, y, r);
this.closePath();
return this; //return the context
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment