Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用画布创建三个圆形遮罩_Javascript_Html_Canvas_Mask - Fatal编程技术网

Javascript 使用画布创建三个圆形遮罩

Javascript 使用画布创建三个圆形遮罩,javascript,html,canvas,mask,Javascript,Html,Canvas,Mask,我需要用画布创建一个圆形蒙版,我正在尝试,但我无法得到它 我知道用Flash可以做到这一点,但我宁愿不用这项技术: 有人能帮我吗?我对javascript了解不多 我需要的是在图片上创建三个不同大小的圆圈(背景色画布) 我知道你来这里不是为了做其他人的工作,但无论我做了多少努力,我都没有得到……你可以根据需要添加多少个圆,你只需指明位置和所需半径即可: JavaScript: var context = document.getElementById('canvas').getContext

我需要用画布创建一个圆形蒙版,我正在尝试,但我无法得到它

我知道用Flash可以做到这一点,但我宁愿不用这项技术:

有人能帮我吗?我对javascript了解不多

我需要的是在图片上创建三个不同大小的圆圈(背景色画布)


我知道你来这里不是为了做其他人的工作,但无论我做了多少努力,我都没有得到……

你可以根据需要添加多少个圆,你只需指明位置和所需半径即可:


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);
<canvas id="canvas" width="600" height="400" />
canvas{
    background: url("http://cdn2.epictimes.com/derrickblair/wp-content/uploads/sites/9/2015/01/happy-people.jpg") no-repeat;
}

HTML:

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);
<canvas id="canvas" width="600" height="400" />
canvas{
    background: url("http://cdn2.epictimes.com/derrickblair/wp-content/uploads/sites/9/2015/01/happy-people.jpg") no-repeat;
}

您可以在此处看到这一点:


相关链接:
  • 您应该阅读更多关于
    canvas
    的信息: