如何在html和css中创建多色圆圈

时间:2013-05-04 19:56:33

标签: javascript html css css3

我想创建一个基于某些值划分为6个扇区的圆,扇区的角度取决于某个参数。参数值越大,圆的弧度越大。

我理解它的方式可以通过制作一个具有这6个不同部分的圆圈,然后在顶部放置另一个div来构建,这会产生这种白色环效果。我知道如何创建圆,但无法理解如何将其动态划分为不同颜色的扇区。

这对CSS来说是否可行,是否存在使用Javascript的解决方案。任何帮助都将深表感谢。

circle

3 个答案:

答案 0 :(得分:2)

HTML5 Canvas是要走的路。以下是一些要学习的链接:

W3C Specification

Kinetic.js

答案 1 :(得分:1)

尝试这样的事情: http://html5.litten.com/graphing-data-in-the-html5-canvas-element-part-iv-simple-pie-charts/

应该注意的是,我发现曾经使用谷歌找到这个问题/答案:HTML5 Canvas pie chart

答案 2 :(得分:1)

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<canvas id="first" width="300" height="300" style="border:1px solid #000;">Your browser       does not support the HTML5 canvas tag
</canvas>

 <p>
  <select name="shapes" id="shapes">
     <option value="Square">Square</option>
     <option value="Circle">Circle</option>
  </select>
</p>
<p>
  <select name="bkcolour" id="bkcolour">
    <option>Red</option>
    <option>Black</option>
  </select>
</p>

<button onclick="drawshapes()">Try it</button>

<script>
    function drawTenSquare()
    {
   for(var i=0; i<10; i++)
   {
        var x=45;
    var y=25;
    var canvas =document.getElementById("first");
            var context=canvas.getContext("2d");
            context.fillStyle="#FF0000";
            context.fillRect(x+(i*15),y+(i*5),10,10);
   }
   }

   function drawTenCircle()
   {
    for(var i=0; i<10; i++)
    {
       var canvas=document.getElementById("first");
           var context=canvas.getContext("2d");
           context.beginPath();
           context.arc(95+(i*5),50+(i*2),40,0,2*Math.PI);
           context.stroke();
   }
  }

  function drawshapes()
  {
   var shape = document.getElementById("shapes")
   var index = shape.selectedIndex;
   var valindex = shape[index].value;

   if(valindex == "Square")
   {
    drawTenSquare();
   }
   else if(valindex == "Circle")
   {
    drawTenCircle();
   }
}
</script>

</body>
</html>