简体   繁体   English

在路径中绘制不同颜色的形状(HTML5 Canvas / Javascript)

[英]Drawing different colored shapes in a path (HTML5 Canvas / Javascript)

I'm trying to draw multiple circle arcs filled with different colors 我正在尝试绘制多个填充不同颜色的圆弧

        //-------------- draw
        ctx.beginPath();
        ctx.fillStyle = "black";
        ctx.arc(30, 30, 20, 0, Math.PI*2, true);
        ctx.fill();
        ctx.fillStyle = "red";
        ctx.arc(100, 100, 40, 0, Math.PI*2, true);
        ctx.fill();
        ctx.closePath();

This produces both arcs filled in with red, and I can tell that there is a faint black outline around the smaller one. 这会产生两个用红色填充的弧线,我可以看出在较小的弧线周围有一个微弱的黑色轮廓。

在此输入图像描述

Can anyone shed some light on how I can accomplish this? 任何人都可以阐明如何实现这一目标吗? what I'm doing wrong? 我做错了什么?

Close the path and then reopen it. 关闭路径,然后重新打开它。

ctx.closePath();
ctx.beginPath();

jsFiddle . jsFiddle

...between the arc drawing code. ......弧形图代码之间。

界

A path starts with beginPath and ends with endPath. 路径以beginPath开头,以endPath结尾。 Every thing in between is the same path. 介于两者之间的每件事都是相同的道路。 You can even draw paths with holes in them by using winging rules. 您甚至可以使用侧翼规则绘制带孔的路径。 Draw something in one direction and something else the opposite direction but inside the first thing. 在一个方向上绘制一些东西,在相反的方向上绘制其他东西,但在第一个方面 Let's draw a rectangle with a hole in the middle using lines. 让我们用线画一个中间有一个洞的矩形。 beginPath(); beginPath方法(); moveTo (10,10); moveTo(10,10); lineTo(100,10); 了lineTo(100,10); lineTo((100,60); lineTo(10,60); lineTo(10,10); closePath(); moveTo(20,20); lineTo(20,50); lineTo(90,50); lineTo(90,20); lineTo(20,20); closePath(); endPath(); fill(); lineTo((100,60); lineTo(10,60); lineTo(10,10); closePath(); moveTo(20,20); lineTo(20,50); lineTo(90,50); lineTo(90 ,20); lineTo(20,20); closePath(); endPath(); fill();

You could do this with any shape. 你可以用任何形状做到这一点。 Try an arc in one direction then another in the opposite direction using a smaller radius 使用较小的半径在一个方向上尝试弧形,然后在相反方向上尝试另一个弧形

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

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