前言:
canvas可以單獨算為前端的一大知識模塊, 今天就研究一下.
先做下前文鋪墊:
①創建canvas
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;"></canvas>
②獲取canvas
var cavs = document.getElementById("cavs"); //獲取canvas var ctx = cavs.getContext("2d"); //可以理解為生成一個2d畫筆 ctx.moveTo(100, 100); //落筆點 ctx.lineTo(200, 200); //結束點
③常用樣式
ctx.strokeStyle = "#f00" //線條的顏色2 ctx.lineWidth = 10; //線條粗細
④繪制線條
ctx.stroke(); //繪制線條
⑤填充圖案
ctx.fillStyle = "yellow"; //填充顏色
ctx.fill(); //進行填充
⑥開始結束
ctx.beginPath(); //開始 ctx.closePath(); //結束
⑦其他
var img = ctx.getImageData(x, y, width, height); //截取canvas中圖案 ctx.putImageData(img, width, height) //將img放到canvas中 ctx.clearRect(x,y,canvas.width,canvas.height) //清除畫布
正文:
說再多也沒用, 最終還是進行實戰, 終於進入正文了;
HTML部分
<div class="wrapper"> <canvas id="cavs" width="1000" height="500"></canvas> <ul class="btn-list"> <li><input type="color" id="colorBoard"></li> <li><input type="button" id="cleanBoard" value="清屏"></li> <li><input type="button" id="eraser" value="橡皮"></li> <li><input type="button" id="rescind" value="撤銷"></li> <li><input type="range" id="lineRuler" value="線條" min="1" max="30"></li> </ul> </div>
css部分
*{ padding: 0; margin: 0; } .wrapper{ margin: 15px; } .wrapper canvas{ border:1px solid #000; border-radius: 25px; box-shadow:10px 10px 5px #999; margin-bottom: 20px; } .wrapper ul{ width: 1000px; text-align: center; } .wrapper ul li{ display: inline-block; margin-left: 40px; } .wrapper ul li input{ display: block; background: #ddd; color: #000; border-radius: 25px; border:none; padding: 10px 20px; font-size: 15px; transition-duration: 0.2s; } .wrapper ul li input:hover{ background: #999; border: 1px solid #f00; box-shadow: 0 12px 16px 0 rgba(0,0,0,0.3); }
js部分
function ID(str) { return document.getElementById(str); } var darwingLineObj = { cavs:this.ID("canvas"), color:this.ID("color"), clear:this.ID("clear"), eraser:this.ID("eraser"), rescind:this.ID("rescind"), weight:this.ID("weight"), bool:false, arrImg:[], //初始化 init:function(){ this.draw(); }, draw:function(){ var cavs = this.cavs, self = this, ctx = cavs.getContext("2d"); ctx.lineWidth = 15; ctx.lineCap = "round"; //線條始終的樣式 ctx.lineJoin = "round"; //轉彎的圓角 var c_x = cavs.offsetLeft, //元素距離左側的位置 c_y = cavs.offsetTop; //canvas距離頂部 cavs.onmousedown = function(e){ e = e || window.event; self.bool = true; var m_x = e.pageX - c_x; var m_y = e.pageY - c_y; ctx.beginPath(); ctx.moveTo(m_x,m_y); //鼠標在畫布上的觸點 var imgData = ctx.getImageData(0, 0, cavs.width, cavs.height); //將每次畫完拷貝到數組中 self.arrImg.push(imgData); } cavs.onmousemove = function(e){ if(self.bool){ ctx.lineTo(e.pageX-c_x, e.pageY-c_y); ctx.stroke(); } } cavs.onmouseup = function(e){ self.bool = false; ctx.closePath(); } self.color.onchange = function(e){ //設置顏色 e = e || window.event; //console.log(e.target.value) ctx.strokeStyle = e.target.value; } self.clear.onclick = function(){ ctx.clearRect(0,0,cavs.width,cavs.height) } self.eraser.onclick = function(){ ctx.strokeStyle = "#fff"; } self.rescind.onclick = function(){ //撤銷的重點難點 if (self.arrImg.length>0) { ctx.putImageData(self.arrImg.pop(),0,0) } } self.weight.onchange = function(e){ //設置線條粗細 //console.log(e.target.value) ctx.lineWidth = e.target.value; } }//draw end } darwingLineObj.init();
結語:
本文還有不足之處, 歡迎大家指正.
聲明:
參考: 渡一教育