欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript結(jié)合canva實(shí)現(xiàn)簡(jiǎn)單的繪圖工具

 更新時(shí)間:2024年12月23日 09:35:58   作者:cpp-csharp-developer  
這篇文章主要給大家介紹了如何使用JavaScript和HTML的Canvas標(biāo)簽創(chuàng)建一個(gè)簡(jiǎn)單的圖表工具,文中通過代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

使用Javascript 結(jié)合 html 的canva標(biāo)簽實(shí)現(xiàn)的簡(jiǎn)單圖表工具

使用瀏覽器打開html文件

點(diǎn)擊【選擇文件】按鈕,打開分行的測(cè)試數(shù)據(jù),測(cè)試數(shù)據(jù)是0~10 之間的隨機(jī)數(shù)
效果圖如下:

測(cè)試數(shù)據(jù) randodata.dat 中的部分內(nèi)容:

7
0
0
6
3
3
6
6
4
1
6
4
6
3
8
5
9
7
9
6
7
6
9

HTML代碼

<html>
<body width="100%">
<canvas id="canvas1" style="border:solid 2px black"></canvas><br/>
<input type="button" id="prepage" value="上一頁(yè)"/></div><input id="nextpage" type="button" value="下一頁(yè)"/><input type="file" id="file1" onchange="fileSelected(this)" />
<div id="pageIndexDiv">1/1</div>
</body>
<script>
    var curPageIndex = 0;
    var totleCount = 0;
    var contentArr =[];
    var pageSize = 1500;
    this.ctx = canvas1.getContext('2d');
    canvas1.width = pageSize;
    canvas1.height = 200;
    
    function drawLine(x1, y1, x2, y2) {
        this.ctx.beginPath();
        this.ctx.strokeStyle = "#008888";
        this.ctx.moveTo(x1, y1);
        this.ctx.lineTo(x2, y2);
        this.ctx.stroke();
    }
    //drawLine(0, 0, 50, 50);  drawLine(50, 50, 100, 0);
    const reader = new FileReader();
    reader.onload = function (e) {
        let content = e.target.result;
        contentArr = content.split('\n');
        curPage = 0;
        totleCount = contentArr.length;
        drawPage(0);
    };
    function clearCanvas() { 
        this.ctx.fillStyle = "#e0e0e0";
        this.ctx.fillRect(0, 0, canvas1.width, canvas1.height);
    }
    function drawPage(index) {
        if(index < totleCount/pageSize && index>=0) {
            for(var i = 0;i<  pageSize-1;i++) {
                drawLine(i,parseInt(contentArr[i+ index* pageSize]) + 100,i+1,parseInt(contentArr[index* pageSize+1])+100);
            }
        }   
        pageIndexDiv.innerText = curPageIndex.toString() + "/" + (totleCount/pageSize).toString(); 
    }
    function fileSelected() {
        reader.readAsText(file1.files[0]);
    }
    prepage.onclick = function(){
        clearCanvas();
        curPageIndex--;
        drawPage(curPageIndex);
    };
    nextpage.onclick = function(){
        clearCanvas();
        curPageIndex++;
        drawPage(curPageIndex);
    };
    pageIndexDiv.innerText = curPageIndex.toString() + "/" + (totleCount/pageSize).toString();
    clearCanvas();
</script>
</html>

總結(jié) 

到此這篇關(guān)于JavaScript結(jié)合canva實(shí)現(xiàn)簡(jiǎn)單的繪圖工具的文章就介紹到這了,更多相關(guān)JS繪圖工具內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論