canvas實現的簡單塗鴉板程式碼例項
本章節分享一段程式碼例項,它實現了簡單的塗鴉板效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style type="text/css"> * { margin: 0; padding: 0; } .hs { width: 480px; height: 50px; margin: 0 auto; } a { width: 100px; height: 50px; display: block; cursor: pointer; float: left; margin: 0 10px; } #red { background-color: red; } #green { background-color: green; } #blue { background-color: blue; } #refresh { background-color: #CCC; line-height: 50px; text-align: center; font-size: 20px; color: #fff; } #myCanvas { display:block; border:1px solid blue; margin:20px auto; } </style> <script> var flag=false; window.onload=function(){ var canvas=document.getElementById("myCanvas"); var context=canvas.getContext("2d"); context.lineWidth=5; context.strokeStyle="red"; context.lineCap="round"; var red=document.getElementById("red"); var green=document.getElementById("green"); var blue=document.getElementById("blue"); var refresh=document.getElementById("refresh"); red.onclick=function(){ context.beginPath(); context.strokeStyle = "red" } green.onclick=function(){ context.beginPath(); context.strokeStyle="green" } blue.onclick=function(){ context.beginPath(); context.strokeStyle="blue" } refresh.onclick=function(){ context.clearRect(0,0,canvas.width,canvas.height); context.beginPath(); } canvas.addEventListener("mousedown",function(event){ var event=event||window.event; if(!flag){ var startX=event.clientX-canvas.getBoundingClientRect().left; var startY=event.clientY-canvas.getBoundingClientRect().top; context.moveTo(startX,startY); flag=true; } }) canvas.addEventListener("mousemove", function (event) { if(flag){ var event=event||window.event; var curX=event.clientX-canvas.getBoundingClientRect().left; var curY=event.clientY-canvas.getBoundingClientRect().top; context.lineTo(curX,curY); context.stroke(); } }) canvas.addEventListener("mouseup",function(){ flag=false; }); canvas.addEventListener("mouseout",function(){ flag=false; }) } </script> </head> <body> <canvas id="myCanvas" width="1400" height="600"></canvas> <div class="hs"> <a id="red"></a> <a id="green"></a> <a id="blue"></a> <a id="refresh">重繪</a> </div> </body> </html>
相關文章
- canvas實現的簡單畫板效果程式碼例項Canvas
- canvas實現的簡單餅狀圖程式碼例項Canvas
- canvas實現的七巧板效果程式碼例項Canvas
- canvas實現的七色板程式碼例項Canvas
- canvas實現的刮刮樂程式碼例項Canvas
- jQuery實現的簡單投票簡單程式碼例項jQuery
- 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
- iOS 畫板 塗鴉 答題iOS
- 自定義view————塗鴉畫板View
- EasyUI實現的form表單提交簡單程式碼例項UIORM
- javascript實現的簡單驗證碼效果程式碼例項JavaScript
- 利用canvas實現的驗證碼效果程式碼例項Canvas
- jQuery實現的自動播放簡單程式碼例項jQuery
- css實現的div旋轉簡單程式碼例項CSS
- js實現的字串簡單編碼和解碼程式碼例項JS字串
- 用canvas實現一個簡單的畫板Canvas
- canvas實現的貪吃蛇遊戲程式碼例項Canvas遊戲
- canvas實現的雪花飄落效果程式碼例項Canvas
- canvas實現的藍色雨滴效果程式碼例項Canvas
- canvas實現的圖片縮放程式碼例項Canvas
- canvas實現的彈力小球效果程式碼例項Canvas
- canvas實現煙花燃放效果程式碼例項Canvas
- 線上直播系統原始碼,實現在圖片上塗鴉並記錄塗鴉軌跡原始碼
- js自定義實現的簡單編碼和解碼程式碼例項JS
- javascript實現的圖片簡單切換程式碼例項JavaScript
- canvas實現的隨機生成漂浮小球程式碼例項Canvas隨機