JavaScript結(jié)合canva實(shí)現(xiàn)簡(jiǎn)單的繪圖工具
使用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="上一頁"/></div><input id="nextpage" type="button" value="下一頁"/><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)文章
微信小程序五子棋游戲的棋盤,重置,對(duì)弈實(shí)現(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了微信小程序五子棋游戲,可實(shí)現(xiàn)五子棋的棋盤繪制,重置,對(duì)弈判定等功能實(shí)現(xiàn),并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2019-02-02
Node.js實(shí)戰(zhàn) 建立簡(jiǎn)單的Web服務(wù)器
本章我們同樣通過實(shí)戰(zhàn)的演練,利用Node.js建立一個(gè)簡(jiǎn)單的Web服務(wù)器2012-03-03
JS數(shù)組實(shí)現(xiàn)分類統(tǒng)計(jì)實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了js數(shù)組實(shí)現(xiàn)分類統(tǒng)計(jì)的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
對(duì)于防止按鈕重復(fù)點(diǎn)擊的嘗試詳解
這篇文章主要介紹了對(duì)于防止按鈕重復(fù)點(diǎn)擊的嘗試,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
js實(shí)現(xiàn)window.open不被攔截的解決方法匯總
這篇文章主要介紹了js實(shí)現(xiàn)window.open不被攔截的解決方法,實(shí)例匯總了常用的不被攔截的解決方法,需要的朋友可以參考下2014-10-10
微信小程序bindtap與catchtap的區(qū)別詳解
本文主要介紹了微信小程序bindtap與catchtap的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

