将二次切片拆分为3个较小的切片HTML5 CANVAS JS

时间:2012-01-12 12:26:37

标签: javascript html5 canvas split quadratic

我有一条二次曲线,用于创建一个饼图切片。切片位于x和y的轴上,中心点位于(0,0)。半径在radiusX和radiusY处可变。这个切片行进90度。

我需要将这个切片分成3个单独的切片(每个切片具有30度角)并使它们匹配其父切片所具有的任何曲线。

以下图像显示了切片的可能示例。黑色圆圈调整切片的大小/形状:

enter image description here enter image description here enter image description here

这是我所做的功能,但它没有正常工作:

//globalPosX and globalPosY equal whatever position each of the two large black circles have repectively.
    var canvas = document.getElementById('CV_slices');
    var context = canvas.getContext('2d');
    var cenX = canvas.width/2;
    var cenY = canvas.height/2;
    var blackDotX = globalPosX - cenX;
    var blackDotY = cenY - globalPosY;
    var endX;
    var endY;
    var controlX;
    var controlY;
//set first slice
    var startCoOrds = {
        x: cenX  ,
        y: globalPosY  
    };                  

    for (i=1; i < 4; i++) {
//make end(x,y) of previous slice the start(x,y) for the next slice.
    endX = startCoOrds.x - (blackDotX*Math.sin(30));
    endY = startCoOrds.y + (blackDotY*Math.cos(30));
//set position of control point using position of start/end positions (at the moment only adjustibng using +10 -10 at end)
    controlX = ((endX - startCoOrds.x) /2) + (startCoOrds.x) + 10;
    controlY = ((endY - startCoOrds.y) / 2) + (startCoOrds.y) - 10;

// draw slice
    context.save();
    context.beginPath();
    context.moveTo(cenX, cenY);
    context.lineTo(startCoOrds.x, startCoOrds.y);
    context.quadraticCurveTo(controlX, controlY, endX, endY);
    context.lineTo(cenX, cenY);
//make end(x,y) of previous slice the start(x,y) for the next slice
    startCoOrds.x = endX;
    startCoOrds.y = endY;

    context.closePath();
    context.globalAlpha = 0.1;
    context.fillStyle = "#333333";
    context.fill();
    context.lineWidth = 2;
    context.strokeStyle = "#ffffff";
    context.stroke(); 
    context.restore();
    }

1 个答案:

答案 0 :(得分:0)

使用最近的“blackDot”作为圆的半径, 使用圆圈,将您的象限划分为3(see wiki) 然后用0,0和“blackDot”的

之间的距离比例来缩放点数

实际上,您的弧是在x或y轴上缩放的圆的象限。