【问题标题】:How to dividing a semicircular region into colored segments using HTML Canvas如何使用 HTML Canvas 将半圆形区域划分为彩色段
【发布时间】:2011-01-14 00:26:02
【问题描述】:

试图在 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() 段。我现在正在尝试。但是任何能提供一些启发的人都会有很大的帮助。

【问题讨论】:

  • 你基本上是在要求一个饼图,对吧?

标签: html canvas


【解决方案1】:

http://www.phpied.com/wp-content/uploads/2008/02/canvas-pie.html 的样本是我正在寻找的样本。 正如我预期的那样使用同心圆弧()。

【讨论】:

    猜你喜欢
    • 2018-05-08
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 2010-11-18
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多