canvas畫流程圖


用canvas畫流程圖:

需求:最后一個圓圈無直線

遇到問題:需要畫多個圓圈時,畫布超出顯示屏加滾動條,解決方法是<canvas>外層<div>的width=100%,且overflow-y: auto;js里通過document.getElementById("workflow").width = 10*180設置畫布的寬度(假定有畫10個圓)

接來下就是圓和直線、斜線的x、y坐標的計算。

 

 

 

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div class="workflow_left_content" style="width:100% ;overflow-x: auto">
        <canvas id="workflow" height=300></canvas>
    </div>

</body>
<script src="../jquery-1.11.3.js"></script>
<script type="text/javascript">

$(function(){
    var cont = document.getElementById("workflow").getContext("2d");
    document.getElementById("workflow").width = 10 * 180;//根據數據的多少來設定畫布的寬度
    for (var i =0; i<11; i++){
        arcTopStroke(cont,200 + i*150 ,100 ,20, i, 10,"yyyy-MM-dd hh:mm:ss","hash","createBy");
        arcStroke(cont,100 + i*150 , 180 ,20, i, 10, "text");
        lineStroke(cont,110 + i*150 , 165  ,20, i, 10)
    }

});

//畫top圓
function arcTopStroke(cont, x, y, r, i, len, time, hash, ID){
    cont.beginPath();
    cont.arc(x, y, r, 0, 2*Math.PI);
    cont.lineWidth = 5;
    cont.strokeStyle = "#999999";
    cont.stroke();
    cont.closePath();
    cont.fillText(time, x, y-45);
    cont.fillText(hash, x, y-30);
    cont.fillText(ID, x - 120, y+140);

    if( i < len ){
        cont.moveTo(x + r, y);
        cont.lineTo(x + r + 110, y);
        cont.lineWidth = 2;
        cont.stroke();
    }
}

//畫底部圓
function arcStroke(cont, x, y, r, i, len, activityName) {

    cont.beginPath();
    cont.arc(x, y, r, 0, 2*Math.PI);
    cont.lineWidth = 5;
    cont.strokeStyle = "#bcbcbc";
    cont.stroke();
    cont.closePath();
    cont.textAlign = "center";
    cont.fillText(activityName, x, y+38);
    cont.closePath();

    if( i < len ){
        cont.moveTo(x + r, y);
        cont.lineTo(x + r + 110, y);
        cont.lineWidth = 2;
        cont.stroke();
    }
}

//斜線
function lineStroke(cont, x, y) {
    cont.moveTo(x, y);
    cont.lineTo(x + 75, y-55);
    cont.lineWidth = 2;
    cont.stroke();

}
</script>
</html>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM