canvas基礎繪製-arc

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>arc</title>
</head>
<body>
<canvas id="canvas" style="border: 1px solid #ddd;"></canvas>
<script>
    //draw an arc畫曲線
    var canvas = document.getElementById("canvas");

    canvas.width = 1024;
    canvas.height = 768;

    var context = canvas.getContext("2d");

    context.lineWidth = 5;
    context.strokeStyle = "#058";
    context.arc(300,300,200,0,0.5*Math.PI,true);
    //context.arc(
    //conterx,contery,radius,//圓心座標x,圓心座標y,半徑
    //startingAngle,endingAngle,//起始弧度值,終止弧度值
    //anticlockwise = false//false順時針繪製,true逆時針繪製
    // );
    context.stroke();
</script>
</body>
</html>

繪製單個圓:html

 

繪製多個圓:canvas

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>arc*n</title>
</head>
<body>
<canvas id="canvas" style="border: 1px solid #ddd"></canvas>
<script>
    var canvas = document.getElementById("canvas");

    canvas.width = 1024;
    canvas.height = 768;

    var context = canvas.getContext("2d");

    context.lineWidth = 5;
    context.strokeStyle = "#058";
    for(var i=0;i<10;i++){
        context.beginPath();//從新開始新的路徑
        context.arc(50+100*i,60,40,0,2*Math.PI*(i+1)/10);
        context.closePath();//結束當前路徑,自動繪製封閉未封閉的線段

        context.stroke();
    }
    for(var i=0;i<10;i++){
        context.beginPath();//從新開始新的路徑
        context.arc(50+100*i,180,40,0,2*Math.PI*(i+1)/10);

        context.stroke();
    }
    for(var i=0;i<10;i++){
        context.beginPath();//從新開始新的路徑
        context.arc(50+100*i,300,40,0,2*Math.PI*(i+1)/10,true);
        context.closePath();//結束當前路徑,自動繪製封閉未封閉的線段

        context.stroke();
    }
    for(var i=0;i<10;i++){
        context.beginPath();//從新開始新的路徑
        context.arc(50+100*i,420,40,0,2*Math.PI*(i+1)/10,true);

        context.stroke();
    }
    //填充圖形;

    context.fillStyle = "#058";
    for(var i=0;i<10;i++){
        context.beginPath();//從新開始新的路徑
        context.arc(50+100*i,540,40,0,2*Math.PI*(i+1)/10);

        context.fill();
    }
</script>
</body>
</html>

相關文章
相關標籤/搜索