Skip to content

Instantly share code, notes, and snippets.

@Joopmicroop
Last active May 25, 2016 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Joopmicroop/992145509f67b4b9aa9b967ddfe5a5f2 to your computer and use it in GitHub Desktop.
Save Joopmicroop/992145509f67b4b9aa9b967ddfe5a5f2 to your computer and use it in GitHub Desktop.
canvas helper functions
var gradientCircle = function(x,y,r,innerColor,outerColor){
this.beginPath();
this.arc(x,y,r,0*Math.PI,2*Math.PI);
var grd = this.createRadialGradient(x, y, 0, x, y, r);
grd.addColorStop(0,innerColor);
grd.addColorStop(1,outerColor);
this.fillStyle = grd;
this.fill();
this.closePath();
}.bind(this.ctx);
// create a rounded box / rect in canvas
var roundedRect = function(x,y,w,h,round){
if(!round){ this.rect(x,y,w,h); return; }
this.beginPath();
this.moveTo(x+round, y);
this.lineTo(x+w-round, y);
this.bezierCurveTo(x+w, y,x+w, y, x+w, y+round);
this.lineTo(x+w, y+h-round);
this.bezierCurveTo(x+w, y+h, x+w, y+h, x+w-round, y+h);
this.lineTo(x+round, y+h);
this.bezierCurveTo(x,y+h, x, y+h, x, y+h-round);
this.lineTo(x, y+round);
this.bezierCurveTo(x, y, x, y, x+round, y);
this.closePath();
}.bind(this.ctx);
var fillRoundedRect = function(x,y,w,h,round){
roundedRect(x,y,w,h,round);
this.fill()
}.bind(this.ctx);
var strokeRoundedRect = function(x,y,w,h,round){
roundedRect(x,y,w,h,round);
this.stroke()
}.bind(this.ctx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment