简体   繁体   English

画布上的彩色的弧

[英]Colored arcs on canvas

I want to draw sectors with different colors and radiuses. 我想绘制具有不同颜色和半径的扇区。 So I write some code using canvas.arc and fill methods 所以我使用canvas.arc和fill方法编写了一些代码

// constants & vars
var data=[0.2, 0.5, .5, 1.0, 0.8, .9, .7, .8];
var colors=["red", "blue", "green", "gray", "orange", 
            "rgb(255,165,0)", "rgb(100,100,100)", "black"];
var WIDTH = 200; 
var HEIGHT = 200;
var centralPoint = {x:WIDTH/2, y:HEIGHT/2};
var radius=HEIGHT/2;
var sectorsCount = data.length;
var epsilon=0.0001;
var myCanvas = document.getElementById("graphic"),
    ctx     = myCanvas.getContext('2d');
    myCanvas.height=HEIGHT;
    myCanvas.width=WIDTH;

//drawing
    for (var i=0; i<sectorsCount-1;i++) {
        ctx.moveTo(centralPoint.x, centralPoint.y);
        ctx.fillStyle=colors[i];
        ctx.arc(centralPoint.x,centralPoint.y, 
            radius*data[i], 
            i*2*Math.PI/sectorsCount+epsilon , 
            (i+1)*2*Math.PI/sectorsCount-epsilon,
             false);
        ctx.closePath();
        ctx.fill();
        ctx.stroke();
    }
    ctx.fillStyle="black";
    ctx.arc(centralPoint.x,centralPoint.y, 10, 0, Math.PI*2, true);
    ctx.fill();

The problem is when next sector is drawing, previous one change color too. 问题是下一个扇区在绘制时,上一个也要更改颜色。 So at the end I have sectors with different radius but with the same (last) color. 所以最后我得到了半径不同但颜色相同(最后)的扇区。

Possibly there is more simple way to do the same stuff? 可能有更简单的方法来做同样的事情?

Just add beginPath() before drawing the new arc: 只需在绘制新弧之前添加beginPath()

for (var i=0; i<sectorsCount-1;i++) {
    ctx.beginPath();     // <--- needs beginPath() here
    ctx.moveTo(centralPoint.x, centralPoint.y);

What happens if not is that path objects are accumulated. 否则,将发生路径对象累积的情况。 The next time a fill/stroke is used, all paths objects are filled/stroked. 下次使用填充/描边时,将填充/描边所有路径对象。 This happens independent on clearing the canvas, so the path needs to be cleared using the beginPath() . 这与清除画布无关,因此需要使用beginPath()清除路径。

Hope this helps - see update below: 希望对您有所帮助-请参见下面的更新:

 // constants & vars var data=[0.2, 0.5, .5, 1.0, 0.8, .9, .7, .8]; var colors=["red", "blue", "green", "gray", "orange", "rgb(255,165,0)", "rgb(100,100,100)", "black"]; var WIDTH = 200; var HEIGHT = 200; var centralPoint = {x:WIDTH/2, y:HEIGHT/2}; var radius=HEIGHT/2; var sectorsCount = data.length; var epsilon=0.0001; var myCanvas = document.getElementById("graphic"), ctx = myCanvas.getContext('2d'); myCanvas.height=HEIGHT; myCanvas.width=WIDTH; //drawing for (var i=0; i<sectorsCount-1;i++) { ctx.beginPath(); ctx.moveTo(centralPoint.x, centralPoint.y); ctx.fillStyle=colors[i]; ctx.arc(centralPoint.x,centralPoint.y, radius*data[i], i*2*Math.PI/sectorsCount+epsilon , (i+1)*2*Math.PI/sectorsCount-epsilon, false); ctx.closePath(); ctx.fill(); ctx.stroke(); } ctx.fillStyle="black"; ctx.arc(centralPoint.x,centralPoint.y, 10, 0, Math.PI*2, true); ctx.fill(); 
 <canvas id="graphic" /> 

Path drawing commands begin with context.beginPath and remain in effect until the next context.beginPath . 路径绘制命令以context.beginPath开头,一直有效,直到下一个context.beginPath为止。 So without a new beginPath all of your previous wedges will be redrawn with the last wedge. 因此,如果没有新的beginPath,则所有先前的楔形都将与最后一个楔形一起重绘。

So the simple fix is to make sure each of your wedges begin with beginPath : 因此,简单的解决方法是确保每个楔形都以beginPath

 var data=[0.2, 0.5, .5, 1.0, 0.8, .9, .7, .8]; var colors=["red", "blue", "green", "gray", "orange", "rgb(255,165,0)", "rgb(100,100,100)", "black"]; var WIDTH = 200; var HEIGHT = 200; var centralPoint = {x:WIDTH/2, y:HEIGHT/2}; var radius=HEIGHT/2; var sectorsCount = data.length; var epsilon=0.0001; var myCanvas = document.getElementById("graphic"), ctx = myCanvas.getContext('2d'); myCanvas.height=HEIGHT; myCanvas.width=WIDTH; //drawing for (var i=0; i<sectorsCount-1;i++) { ctx.beginPath(); ctx.moveTo(centralPoint.x, centralPoint.y); ctx.fillStyle=colors[i]; ctx.arc(centralPoint.x,centralPoint.y, radius*data[i], i*2*Math.PI/sectorsCount+epsilon , (i+1)*2*Math.PI/sectorsCount-epsilon, false); ctx.closePath(); ctx.fill(); ctx.stroke(); } ctx.fillStyle="black"; ctx.beginPath(); ctx.arc(centralPoint.x,centralPoint.y, 10, 0, Math.PI*2, true); ctx.fill(); 
 body{ background-color: ivory; } #canvas{border:1px solid red;} 
 <canvas id="graphic" width=300 height=300></canvas> 

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

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