Last active
December 27, 2015 00:49
-
-
Save nicolsc/7240777 to your computer and use it in GitHub Desktop.
This file contains 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
function mycircle(r,g,b,a,width,height, targetID,canvasContainerID){ | |
/* Default values */ | |
r = r || 0; | |
g = g ||0; | |
b = b || 0; | |
width = width || 100; | |
height = height || 100; | |
targetID = targetID || 'canvas'; | |
/* Reset container, if asked to */ | |
if (canvasContainerID){ | |
//reset it | |
document.getElementById(canvasContainerID).innerHTML = ('<canvas id="'+targetID+'"></canvas>'); | |
} | |
/* Get target canvas, and check if it exists & is a canvas */ | |
var target = document.getElementById(targetID); | |
if (!target){ | |
throw new Error('Target '+targetID+' not found'); | |
} | |
if (target.tagName.toLowerCase()!='canvas'){ | |
throw new Error('Target '+targetID+' must be a canvas, but is a '+target.tagName); | |
} | |
/* Set canvas size */ | |
target.width=width; | |
target.height = height; | |
/* draw the canvas */ | |
var ctx = target.getContext('2d'); | |
ctx.beginPath(); | |
ctx.arc(width/2, height/2, width/2, 0, 2 * Math.PI, false); | |
ctx.fillStyle = 'rgba('+r+', '+g+','+b+','+a+')'; | |
ctx.fill(); | |
/* output the png base64 of the rectangle in the console*/ | |
console.log(document.getElementById('canvas').toDataURL('image/png')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment