HTML5 - 画布形状描边
我创建了两种形状,圆形和矩形,一个放在另一个的上面,就像一把钥匙锁。然后我尝试应用描边,但它抚摸了两个形状。我想要它做的只是抚摸合并的图案,而不是任何交叉点。
context.beginPath();
context.fillStyle = "#ccc";
context.arc(115, 550, 12, 0, 2 * Math.PI, false);
context.moveTo(105, 555);
context.fillStyle = "#999";
context.rect(105, 555, 20, 30);
context.fill();
context.stroke();
context.closePath();
如果我尝试先绘制矩形,那么当您描边时,顶部的圆弧会有额外的线条路径,就像我必须关闭路径然后再次绘制它一样。
I have created 2 shapes, circle and rectangle, one on top of the other to resemble a key lock. I then try to apply a stroke but its stroking both shapes. What I want it to do is just stroke the merged pattern and not any of the intersections.
context.beginPath();
context.fillStyle = "#ccc";
context.arc(115, 550, 12, 0, 2 * Math.PI, false);
context.moveTo(105, 555);
context.fillStyle = "#999";
context.rect(105, 555, 20, 30);
context.fill();
context.stroke();
context.closePath();
If I try to draw the rect first, then the arc on top there are extra line paths when you stroke, its like I have to close Path and then draw it again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望路径没有相交部分,则不能使用矩形和整个圆。
相反,您必须仅绘制圆的一部分和矩形的一部分。这应该适合你:
You can't use a rect and a whole circle if you want the path to do without the intersecting part.
Instead you have to draw only part of the circle and only part of the rectangle. This should do it for you:
在研究我自己的不规则形状答案时,我发现了克劳德教授的一个实验室项目解决了我的问题。
此页面 SVG-to-Canvas 将 SVG 图形解析为 Canvas 代码。因此,如果您有像 Illustrator 这样的应用程序,您可以使用它绘制图形并将图形保存为 SVG,那么您可以解析可用的画布代码并将它们插入。
While working on my own irregular shape answer, I discovered a lab project by Professor Cloud that solved my problem.
This page, SVG-to-Canvas, parses an SVG graphic to Canvas code. So if you have an application like Illustrator with which you can draw and save the graphic as SVG, then you can parse the usable canvas codes and just plug them in.
您可以使用合成和临时画布。像这样的东西:
还有一个jsfiddle: https://jsfiddle.net/EvaF/8to68dtd/2/< /a>
You can use compositing and temporary canvas. Something like that:
And a jsfiddle: https://jsfiddle.net/EvaF/8to68dtd/2/