JS canvas實(shí)現(xiàn)畫(huà)板和簽字板功能
本文實(shí)例為大家分享了JS canvas實(shí)現(xiàn)畫(huà)板/簽字板功能的具體代碼,供大家參考,具體內(nèi)容如下
前言
常見(jiàn)的電子教室里的電子黑板。
本文特點(diǎn):
原生JS
封裝好的模塊
最簡(jiǎn)代碼樣例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <canvas id="canvas"></canvas> <script> let c = document.getElementById('canvas'); c.width = window.innerWidth; c.height = window.innerHeight; let ctx = c.getContext('2d'); // draw one black board ctx.fillStyle = "black"; ctx.fillRect(0,0,600,300); // 按下標(biāo)記 let onoff = false, oldx = -10, oldy = -10; // 設(shè)置顏色 let linecolor = "white"; // 設(shè)置線寬 let linw = 4; // 添加鼠標(biāo)事件 // 按下 c.addEventListener('mousedown', event => { onoff = true; // 位置 - 10是為了矯正位置,把繪圖放在鼠標(biāo)指針的頂端 oldx = event.pageX - 10; oldy = event.pageY - 10; },false); // 移動(dòng) c.addEventListener('mousemove', event => { if(onoff == true){ let newx = event.pageX - 10, newy = event.pageY - 10; // 繪圖 ctx.beginPath(); ctx.moveTo(oldx,oldy); ctx.lineTo(newx,newy); ctx.strokeStyle = linecolor; ctx.lineWidth = linw; ctx.lineCap = "round"; ctx.stroke(); // 每次移動(dòng)都要更新坐標(biāo)位置 oldx = newx, oldy = newy; } }, true); // 彈起 c.addEventListener('mouseup', ()=> { onoff = false; },false); </script> </body> </html>
結(jié)果展示
代碼講解
思路
1、鼠標(biāo)按下,開(kāi)始描畫(huà)。鼠標(biāo)按下事件。
2、鼠標(biāo)彈起,結(jié)束描畫(huà)。鼠標(biāo)彈起事件。
3、鼠標(biāo)按下移動(dòng),路徑畫(huà)線。鼠標(biāo)移動(dòng)事件。
代碼講解
整體思路:按下鼠標(biāo),觸發(fā)移動(dòng)的開(kāi)關(guān),移動(dòng)后開(kāi)始記錄線條(用移動(dòng)后的坐標(biāo)-移動(dòng)前的坐標(biāo),然后繪線),每次移動(dòng)都會(huì)更新舊坐標(biāo)。松開(kāi)鼠標(biāo)后,釋放移動(dòng)開(kāi)關(guān)。
1、只有在鼠標(biāo)按下,才會(huì)觸發(fā)移動(dòng)繪圖的效果,所以需要增加一個(gè)狀態(tài)判斷。
2、因?yàn)槭髽?biāo)指針和實(shí)際位置有一個(gè)偏移量,所以在坐標(biāo)定位的時(shí)候,需要增加pagex-10從而使坐標(biāo)位于指針的尖端處。
3、每次移動(dòng)都要更新坐標(biāo)位置,用小段的線段來(lái)模擬不規(guī)則的線。
封裝模塊
<canvas id="canvas"></canvas> <script> class Board{ constructor(canvasName = 'canvas', data = new Map([ ["onoff", false], ["oldx", -10], ["oldy", -10], ["fillStyle", "black"], ["lineColor", "white"], ["lineWidth", 4], ["lineCap", "round"], ["canvasWidth", window.innerWidth], ["canvasHeight", window.innerHeight] ])){ // this.data = data; this.c = document.getElementById(canvasName); this.ctx = this.c.getContext('2d'); this.onoff = data.get("onoff"); this.oldx = data.get("oldx"); this.oldy = data.get("oldy"); this.lineColor = data.get("lineColor"); this.lineWidth = data.get("lineWidth"); this.lineCap = data.get("lineCap"); this.c.width = data.get("canvasWidth"); this.c.height = data.get("canvasHeight"); this.ctx.fillStyle = data.get("fillStyle"); this.ctx.fillRect(0,0,600,300); } eventOperation(){ // 添加鼠標(biāo)事件 // 按下 this.c.addEventListener('mousedown', event => { this.onoff = true; // 位置 - 10是為了矯正位置,把繪圖放在鼠標(biāo)指針的頂端 this.oldx = event.pageX - 10; this.oldy = event.pageY - 10; },false); // 移動(dòng) this.c.addEventListener('mousemove', event => { if(this.onoff == true){ let newx = event.pageX - 10, newy = event.pageY - 10; // 繪圖 this.ctx.beginPath(); this.ctx.moveTo(this.oldx,this.oldy); this.ctx.lineTo(newx,newy); this.ctx.strokeStyle = this.lineColor; this.ctx.lineWidth = this.lineWidth; this.ctx.lineCap = this.lineCap; this.ctx.stroke(); // 每次移動(dòng)都要更新坐標(biāo)位置 this.oldx = newx, this.oldy = newy; } }, true); // 彈起 this.c.addEventListener('mouseup', ()=> { this.onoff = false; },false); } } let board = new Board(); board.eventOperation(); </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript canvas實(shí)現(xiàn)圍繞旋轉(zhuǎn)動(dòng)畫(huà)
- JS+canvas畫(huà)一個(gè)圓錐實(shí)例代碼
- js canvas畫(huà)布實(shí)現(xiàn)高斯模糊效果
- JavaScript canvas動(dòng)畫(huà)實(shí)現(xiàn)時(shí)鐘效果
- js+canvas實(shí)現(xiàn)畫(huà)板功能
- JS Canvas接口和動(dòng)畫(huà)效果大全
- JS使用canvas繪制旋轉(zhuǎn)風(fēng)車動(dòng)畫(huà)
- JavaScript結(jié)合Canvas繪畫(huà)畫(huà)運(yùn)動(dòng)小球
相關(guān)文章
PHP中使用微秒計(jì)算腳本執(zhí)行時(shí)間例子
這篇文章主要介紹了PHP中使用微秒計(jì)算腳本執(zhí)行時(shí)間例子,本文先是講解了microtime函數(shù)的一些知識(shí),然后給出了一個(gè)計(jì)算腳本運(yùn)行時(shí)間的類,需要的朋友可以參考下2014-11-11JavaScript實(shí)現(xiàn)數(shù)組對(duì)象轉(zhuǎn)換為鍵值對(duì)的四種方式
本文探討了將包含 {icon: "abc", url: "123"} 形式對(duì)象的數(shù)組轉(zhuǎn)換為鍵值對(duì)形式的四種方法,并從實(shí)現(xiàn)方式的簡(jiǎn)潔性、可讀性和性能角度進(jìn)行了分析比較,感興趣的朋友可以參考下2024-02-02在vs2010中調(diào)試javascript代碼方法
只在IE瀏覽器中測(cè)試成功了,在谷歌瀏覽中沒(méi)有測(cè)試成功,其他瀏覽器沒(méi)有測(cè)試。2011-02-02JavaScript實(shí)現(xiàn)簡(jiǎn)單的倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單的倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06利用Javascript實(shí)現(xiàn)簡(jiǎn)單的轉(zhuǎn)盤抽獎(jiǎng)
這篇文章主要介紹了利用Javascript實(shí)現(xiàn)的簡(jiǎn)單的轉(zhuǎn)盤抽獎(jiǎng),文中分享了兩種抽獎(jiǎng)效果,一種是默認(rèn)轉(zhuǎn)動(dòng),一種是需要點(diǎn)擊開(kāi)始轉(zhuǎn)動(dòng)的,并給出了晚上的示例代碼,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-02-02小程序按鈕避免多次調(diào)用接口和點(diǎn)擊方案實(shí)現(xiàn)(不用showLoading)
這篇文章主要介紹了小程序按鈕避免多次調(diào)用接口和點(diǎn)擊方案實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04