JavaScript結(jié)合canva實現(xiàn)簡單的繪圖工具
使用Javascript 結(jié)合 html 的canva標(biāo)簽實現(xiàn)的簡單圖表工具
使用瀏覽器打開html文件
點擊【選擇文件】按鈕,打開分行的測試數(shù)據(jù),測試數(shù)據(jù)是0~10 之間的隨機數(shù)
效果圖如下:

測試數(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實現(xiàn)簡單的繪圖工具的文章就介紹到這了,更多相關(guān)JS繪圖工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
layui 數(shù)據(jù)表格+分頁+搜索+checkbox+緩存選中項數(shù)據(jù)的方法
今天小編就為大家分享一篇layui 數(shù)據(jù)表格+分頁+搜索+checkbox+緩存選中項數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
JS驗證輸入的是否是數(shù)字及保留幾位小數(shù)問題
這篇文章主要介紹了JS驗證輸入的是否是數(shù)字及保留幾位小數(shù)問題,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-05-05
JS對外部文件的加載及對IFRMAME的加載的實現(xiàn),當(dāng)加載完成后,指定指向方法(方法回調(diào))
callback方法回調(diào)是指當(dāng)某方法執(zhí)行完成后,去自動執(zhí)行指定的另一方法的過程.下面舉兩個代表性的例子,說說JS世界里的方法回調(diào).2011-07-07
AJAX異步從優(yōu)酷專輯中采集所有視頻及信息(JavaScript代碼)
上次寫了一個 .NET從優(yōu)酷專輯中采集所有視頻及信息(VB.NET代碼)2010-11-11
js語法學(xué)習(xí)之判斷一個對象是否為數(shù)組
這篇文章主要介紹了從javascript判斷一個對象是否為數(shù)組中學(xué)習(xí)js語法,需要的朋友可以參考下2014-05-05
JS數(shù)組索引檢測中的數(shù)據(jù)類型問題詳解
這篇文章主要給大家介紹了關(guān)于JS數(shù)組索引檢測中的數(shù)據(jù)類型問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

