Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create three circle mask with canvas

I need to create a circle mask with canvas, I'm trying to do that, but I can't get it.

I knew to do that with Flash, but I prefef to do without that technology: enter image description here

May anybody help me with this? I don't know too much about javascript

What I need is create three circles with different sizes on a picture (Canvas with background color).

I know that you are not here to do the job of others but however much I tried I hace not gotten...

like image 671
avacode Avatar asked Sep 28 '22 20:09

avacode


1 Answers

You can add as many circles as you need, you must only indicate the position and the desired radius:


JavaScript:

var context = document.getElementById('canvas').getContext('2d');

// Color of the "mask"
context.fillStyle = '#000';

// Rectangle with the proportions of the image (600x400)
context.fillRect(0,0,600,400);

/**
* @param x Specifies the x-coordinate
* @param y Specifies the y-coordinate
* @param radius Specifies radius of the circle
*/
var clearCircle = function(x, y, radius){
    context.save();
    context.globalCompositeOperation = 'destination-out';
    context.beginPath();
    context.arc(x, y, radius, 0, 2 * Math.PI, false);
    context.fill();
    context.restore();
};

// First circle
clearCircle(155, 190, 50);

// Second circle
clearCircle(300, 190, 70);

// Third circle
clearCircle(440, 200, 60);

HTML:

<canvas id="canvas" width="600" height="400" />

CSS:

canvas{
    background: url("http://cdn2.epictimes.com/derrickblair/wp-content/uploads/sites/9/2015/01/happy-people.jpg") no-repeat;
}


You can see this in action here: http://jsfiddle.net/tomloprod/szau09x6/


Related links:

  • You should read more about canvas here: http://www.w3schools.com/html/html5_canvas.asp
like image 131
tomloprod Avatar answered Nov 02 '22 13:11

tomloprod