简体   繁体   English

画布上的图像上的圆角

[英]Rounded corners on images in canvas

I have a canvas with an image inside it. 我有一个带有图像的画布。 I'm looking to put rounded corners on two corners of the image. 我想在图像的两个角上放置圆角。 I am thinking the way to do it would be use one of the global operators but I can't seem to figure out how I'd do that. 我认为这样做的方法是使用全球运营商之一,但我似乎无法弄清楚我是如何做到这一点的。

Any help would be appreciated. 任何帮助,将不胜感激。

Instead of using a global operator, figure out the space that you want the image to occupy (which should be a path that is a rectangle except for the rounded corners) 而不是使用全局运算符,找出您希望图像占用的空间(除了圆角之外应该是一个矩形的路径)

Then put this path on the context before you draw your image, call .clip(), and then draw the image. 然后在绘制图像之前将此路径放在上下文中,调用.clip(),然后绘制图像。

The image will then be drawn with rounded corners on the two corners of the image. 然后在图像的两个角上用圆角绘制图像。

So your only real task now is coming up with the path you need to do it. 所以你现在唯一真正的任务是提出你需要的道路。

In short: 简而言之:

ctx.save();
ctx.beginPath();
// use lineTo and BezierTo here to make the path you want, which is a rectangle the size of the image with two rounded corners.
ctx.closePath();
ctx.clip();

// draw the image
ctx.restore(); // so clipping path won't affect anything else drawn afterwards

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM