canvas實現的簡單畫板效果程式碼例項
分享一段程式碼例項,它實現了簡單的畫板效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> html, body, div, canvas, nav, input, ul, li { margin: 0; padding: 0; } #box { width: 600px; height: 500px; margin: 100px auto; background-color: orange; } #box nav { width: 100%; height: 100px; } #box nav div { height: 50px; line-height: 50px; } #box nav .changeColor { padding-left: 15px; } .changeColor input { width: 30px; height: 30px; margin: 0 15px; vertical-align: middle; } .clear input:first-of-type { margin-left: 15px; width: 100px; height: 30px; background-color: #ffffff; } .clear input:last-of-type { margin-left: 15px; width: 30px; height: 30px; background-color: #ffffff; } canvas { background-color: yellow; } </style> </head> <body> <div id="box"> <nav> <div class="changeColor"> 請選擇顏色 <input type="button" style="background-color: pink"> <input type="button" style="background-color: purple"> <input type="button" style="background-color: red"> <input type="button" style="background-color: green"> <input type="button" style="background-color: deepskyblue"> </div> <div class="clear"> <input type="button" value="清空畫布" onclick=clearLayer()> 橡皮擦<input class="eraser" type="button"> </div> </nav> <canvas width="600" height="400"></canvas> </div> <script> var cvs = document.querySelector("canvas"); var ctx = cvs.getContext("2d"); var eraser = document.querySelector(".eraser"); cvs.addEventListener("mousedown", function (e) { var x = e.clientX - this.offsetLeft; var y = e.clientY - this.offsetTop; cvs.oldPoint = { x: x - 1, y: y - 1, } drawLine(x, y); this.addEventListener("mousemove", move); this.addEventListener("mouseup", up); }) function move(e) { var x = e.clientX - this.offsetLeft; var y = e.clientY - this.offsetTop; drawLine(x, y); cvs.oldPoint = { x: x, y: y, } } function up(e) { this.removeEventListener("mousemove", move); this.removeEventListener("mouseup", up); } function drawLine(x, y) { ctx.beginPath(); ctx.lineWidth = 5; ctx.lineJoin = "round"; ctx.lineCap = "round"; ctx.moveTo(cvs.oldPoint.x, cvs.oldPoint.y); ctx.lineTo(x, y); ctx.stroke(); ctx.closePath(); } function clearLayer() { ctx.clearRect(0, 0, cvs.width, cvs.height); } var colorBtn = document.querySelectorAll(".changeColor input"); var colorBtnArr = [].slice.call(colorBtn); colorBtnArr.forEach(function (item, index) { item.onclick = function () { changeColor(this); } }) function changeColor(btn) { ctx.strokeStyle = getComputedStyle(btn).backgroundColor; } eraser.onclick = function () { ctx.strokeStyle = getComputedStyle(cvs).backgroundColor; }; </script> </body> </html>
相關文章
- canvas實現的簡單塗鴉板程式碼例項Canvas
- canvas簡單畫板效果Canvas
- canvas實現的七巧板效果程式碼例項Canvas
- canvas實現的鋸齒效果程式碼例項Canvas
- canvas實現的鐘表效果程式碼例項Canvas
- canvas實現的驗證碼效果程式碼例項Canvas
- html實現簡單ListViews效果的例項程式碼HTMLView
- javascript實現的動畫效果簡單例項程式碼JavaScript動畫單例
- canvas實現的簡單餅狀圖程式碼例項Canvas
- 用canvas實現一個簡單的畫板Canvas
- 利用canvas實現的驗證碼效果程式碼例項Canvas
- canvas實現的雪花飄落效果程式碼例項Canvas
- canvas實現的藍色雨滴效果程式碼例項Canvas
- canvas實現的彈力小球效果程式碼例項Canvas
- javascript實現的簡單驗證碼效果程式碼例項JavaScript
- canvas實現的七色板程式碼例項Canvas
- canvas實現煙花燃放效果程式碼例項Canvas
- canvas實現的動態心形效果程式碼例項Canvas
- canvas實現的圓形鐘錶效果程式碼例項Canvas
- canvas畫圖程式碼例項Canvas
- angularJS結合canvas實現的畫圖程式碼例項AngularJSCanvas
- canvas火焰效果程式碼例項Canvas
- canvas實現的賽貝爾曲線效果程式碼例項Canvas
- canvas實現的小球四壁碰撞效果程式碼例項Canvas
- canvas實現文字線性漸變效果程式碼例項Canvas
- canvas實現的刮刮樂程式碼例項Canvas
- canvas實現的旋轉的太極圖效果程式碼例項Canvas
- jQuery實現的簡單投票簡單程式碼例項jQuery
- JavaScript 動畫效果簡單例項程式碼JavaScript動畫單例
- canvas字母雨效果程式碼例項Canvas
- canvas載入效果程式碼例項Canvas
- canvas擺動效果程式碼例項Canvas
- canvas寫字板功能程式碼例項Canvas
- canvas實現的美猴王程式碼例項Canvas
- JavaScript簡單的日曆效果程式碼例項JavaScript
- canvas簡單徑向漸變程式碼例項Canvas
- js簡單日曆效果程式碼例項JS
- js簡單摺紙效果程式碼例項JS