如何使用 HTML Canvas 将半圆形区域划分为彩色片段

发布于 2024-10-12 03:27:12 字数 1026 浏览 3 评论 0原文

尝试在 HTML Canvas 中将半圆形区域划分为彩色片段。 这是我尝试过的,

            ctx.save();
            ctx.translate(c.width / 2, (c.height / 2)-1);
            ctx.strokeStyle = "red"
            ctx.lineWidth = 3;
            ctx.lineCap = "round";
            var x=400; // number of times lineTo strokes. Greater the value the better is the smoothness
            var factor=1;   //with =1, the entire semicirular region is filled.
            for (var i = 0; i < x; i++) { 
                 //ctx.rotate(Math.PI);
                ctx.beginPath();

                ctx.strokeStyle = "rgba(255,0,0,1)";
                //ctx.rotate(-Math.PI/2);

                ctx.rotate((-Math.PI * factor) / x); 
                //1st color segment, factor=1 helps to paint 100% of semicircular region

                ctx.moveTo(122, 0);
                ctx.lineTo(70, 0);
                ctx.stroke();
                //ctx.rotate(Math.PI); //2nd color segment

替代方法,可能是使用同心 arc() 段。我现在正在尝试这样做。但任何能够提供一些线索的人都会提供很大的帮助。

trying to divide a semicircular region into colored segments in HTML Canvas.
Here's what I tried,

            ctx.save();
            ctx.translate(c.width / 2, (c.height / 2)-1);
            ctx.strokeStyle = "red"
            ctx.lineWidth = 3;
            ctx.lineCap = "round";
            var x=400; // number of times lineTo strokes. Greater the value the better is the smoothness
            var factor=1;   //with =1, the entire semicirular region is filled.
            for (var i = 0; i < x; i++) { 
                 //ctx.rotate(Math.PI);
                ctx.beginPath();

                ctx.strokeStyle = "rgba(255,0,0,1)";
                //ctx.rotate(-Math.PI/2);

                ctx.rotate((-Math.PI * factor) / x); 
                //1st color segment, factor=1 helps to paint 100% of semicircular region

                ctx.moveTo(122, 0);
                ctx.lineTo(70, 0);
                ctx.stroke();
                //ctx.rotate(Math.PI); //2nd color segment

Alternate way, might be to use concentric arc() segments. I'm trying that now. But any one who can throw some light would be a great help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟若柳尘 2024-10-19 03:27:12

示例位于 http://www.phpied.com/ wp-content/uploads/2008/02/canvas-pie.html 是我正在寻找的。
正如我预期的那样,使用同心 arc() 。

the sample at http://www.phpied.com/wp-content/uploads/2008/02/canvas-pie.html was the one I was looking for.
Uses concentric arc() as I anticipated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文