HTML5画布 - 用图像填充圆圈

tbl*_*ert 30 html5 canvas

如何在圆圈内绘制图像?如果我做:

context.beginPath();
context.arc((e.pageX),(e.pageY),161,0,Math.PI*2,true);
context.closePath();
Run Code Online (Sandbox Code Playgroud)

然后我如何使用fill()用我绘制的图像填充它?

jar*_*lli 54

前几天我做了一件我做的大事;

var thumbImg = document.createElement('img');

thumbImg.src = 'path_to_image';
thumbImg.onload = function() {
    tmpCtx.save();
    tmpCtx.beginPath();
    tmpCtx.arc(25, 25, 25, 0, Math.PI * 2, true);
    tmpCtx.closePath();
    tmpCtx.clip();

    tmpCtx.drawImage(thumbImg, 0, 0, 50, 50);

    tmpCtx.beginPath();
    tmpCtx.arc(0, 0, 25, 0, Math.PI * 2, true);
    tmpCtx.clip();
    tmpCtx.closePath();
    tmpCtx.restore();
};
Run Code Online (Sandbox Code Playgroud)

工作对我来说很完美.

这是我制作的更复杂版本的图像缓存,https://jsfiddle.net/jaredwilli/ex5n5/

  • 虽然这最终是我在画布中提出的解决方案,但在使用我工作的应用程序需要完成的各种内容对其进行高度扩充和修改之后,结果是在启动某些东西之前的 2 天出现了并且有 2 天的时间来做一个替代解决方案,结果证明它在桌面和移动设备上都更快、更跨浏览器。解决方案是@MatTheCat 建议“将 <img> 与 CSS 用于边框半径”的答案。刚刚使用 border-radius: 100% 使其成为圆形。迄今为止最好的解决方案。只是在说... (2认同)

mob*_*oby 11

不确定你是否还在寻找答案,但这里是如何:

var ctx = document.getElementById('your_canvas').getContext("2d");
//ctx.lineWidth = 13; 
//ctx.strokeStyle = 'rgba(0,0,0,1)'; 
//ctx.fillStyle="rgba(0,0,0,0)" // if using this, make sure alpha < 1

ctx.arc(100,100, 50, 0, Math.PI*2,true); // you can use any shape
ctx.clip();

var img = new Image();
img.addEventListener('load', function(e) {
    ctx.drawImage(this, 0, 0, 200, 300);
    //ctx.fill();
//ctx.stroke();
}, true);
img.src="/path/to/image.jpg";
Run Code Online (Sandbox Code Playgroud)

您也可以使用模式执行此操作,但您可以获得较少的图像放置灵活性

ctx.arc(100,100, 70, 0, Math.PI*2,true);
ctx.clip();

img = new Image()
img.addEventListener('load', function(e) {
    ctx.fillStyle = ctx.createPattern(this, 'no-repeat') 
    ctx.fill();
}, true);
img.src="/path/to/image.jpg"
Run Code Online (Sandbox Code Playgroud)


Chr*_*gan 5

考虑使用以下一些替代方法:

  • <img>与CSS搭配使用border-radiushttp : //jsfiddle.net/ChrisMorgan/BQGxA/

  • 而不是使用SVG <canvas>并将椭圆设置为图像的剪切路径。(更复杂的剪切路径也很容易)

不了解您的要求和情况,我不知道这些条件是否可以满足您的要求,但我认为值得考虑。<canvas>并非解决所有问题的方法-在许多情况下,普通HMTL和/或SVG中的CSS可能是更好的选择。


Jes*_*era 5

clip() 方法的问题在于 Chrome 会将边框呈现为非抗锯齿,如本问题所示。

一种解决方案是使用 globalCompositeOperation 如丹尼尔的回答所示:

//set-up - probably only needs to be done once
var scratchCanvas = document.createElement('canvas');
scratchCanvas.width = 100;
scratchCanvas.height = 100;
var scratchCtx = scratchCanvas.getContext('2d');


//drawing code
scratchCtx.clearRect(0, 0, scratchCanvas.width, scratchCanvas.height);

scratchCtx.globalCompositeOperation = 'source-over'; //default

//Do whatever drawing you want. In your case, draw your image.
scratchCtx.drawImage(imageToCrop, ...);


//As long as we can represent our clipping region as a single path, 
//we can perform our clipping by using a non-default composite operation.
//You can think of destination-in as "write alpha". It will not touch
//the color channel of the canvas, but will replace the alpha channel.
//(Actually, it will multiply the already drawn alpha with the alpha
//currently being drawn - meaning that things look good where two anti-
//aliased pixels overlap.)
//
//If you can't represent the clipping region as a single path, you can
//always draw your clip shape into yet another scratch canvas.

scratchCtx.fillStyle = '#fff'; //color doesn't matter, but we want full opacity
scratchCtx.globalCompositeOperation = 'destination-in';
scratchCtx.beginPath();
scratchCtx.arc(50, 50, 50, 0, 2 * Math.PI, true);
scratchCtx.closePath();
scratchCtx.fill();


//Now that we have a nice, cropped image, we can draw it in our
//actual canvas. We can even draw it over top existing pixels, and
//everything will look great!

ctx.drawImage(scratchCanves, ...);
Run Code Online (Sandbox Code Playgroud)