在本文中,我將向您展示如何使用 JavaScript 和 HTML5 Canvas創(chuàng)建繪圖/繪圖應(yīng)用程序。 特征:在Canvas畫布上繪畫 多種顏色 清除畫布 將繪圖另存為圖像
首先讓我們創(chuàng)建一個帶有 canvas 元素的 index.html 文件。 <!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'> <link rel='stylesheet' href='style.css'> <title>Linux迷 www.</title> </head> <body> <canvas id='canvas'></canvas>
<script src='main.js'></script> </body> </html>
現(xiàn)在讓我們創(chuàng)建具有基本重置的style.css *{ margin: 0; padding: 0; }
最后,我們將創(chuàng)建我們的 main.js,我們將定位我們的畫布并將其大小設(shè)置為我們的屏幕大小。 const canvas = document.getElementById('canvas') canvas.height = window.innerHeight canvas.width = window.innerWidth
// ctx 是我們畫布的上下文 // 我們使用 ctx 在畫布上繪制 const ctx = canvas.getContext('2d')
//讓我們創(chuàng)建一個矩形用于測試目的 ctx.fillStyle = 'red' ctx.fillRect(100, 100, 100, 100)
現(xiàn)在,如果我們在瀏覽器中打開它,我們應(yīng)該會看到一個紅色矩形。 
好的,讓我們刪除這個矩形當(dāng)使用者移動他的鼠標(biāo)時我們想要得到鼠標(biāo)的位置。我們可以使用mousemove事件。 const canvas = document.getElementById('canvas') canvas.height = window.innerHeight canvas.width = window.innerWidth
const ctx = canvas.getContext('2d')
window.addEventListener('mousemove', (e) => { console.log('Mouse X: ' + e.clientX) console.log('Mouse Y: ' + e.clientY) })

現(xiàn)在我們還需要跟蹤上一個鼠標(biāo)位置,并從上一個鼠標(biāo)位置到當(dāng)前鼠標(biāo)位置畫一條線。 const canvas = document.getElementById('canvas') canvas.height = window.innerHeight canvas.width = window.innerWidth
const ctx = canvas.getContext('2d')
// 以前的鼠標(biāo)位置 // 它們最初將為空 let prevX = null let prevY = null
// 線條應(yīng)該有多粗 ctx.lineWidth = 5
window.addEventListener('mousemove', (e) => { // 最初之前的鼠標(biāo)位置是null //所以我們不能畫一條線 if(prevX == null || prevY == null){ // Set the previous mouse positions to the current mouse positions prevX = e.clientX prevY = e.clientY return }
//當(dāng)前鼠標(biāo)位置 let currentX = e.clientX let currentY = e.clientY
// 從上一個鼠標(biāo)位置到當(dāng)前鼠標(biāo)位置畫一條線 ctx.beginPath() ctx.moveTo(prevX, prevY) ctx.lineTo(currentX, currentY) ctx.stroke()
// 更新之前的鼠標(biāo)位置 prevX = currentX prevY = currentY })

現(xiàn)在,如果您移動鼠標(biāo),您將看到將繪制一條線。但我們不希望這條線不受控制地繪制。所以我們將聲明一個變量let draw = false 。我們將只繪制draw 是 true 。 所以我們可以監(jiān)聽mousedown 和mouseup 事件。并設(shè)置draw 為true 用戶按下鼠標(biāo)時和false 釋放鼠標(biāo)時。 const canvas = document.getElementById('canvas') canvas.height = window.innerHeight canvas.width = window.innerWidth
const ctx = canvas.getContext('2d')
let prevX = null let prevY = null
ctx.lineWidth = 5
let draw = false
// 按下鼠標(biāo)時將 draw 設(shè)置為 true window.addEventListener('mousedown', (e) => draw = true) // 釋放鼠標(biāo)時將 draw 設(shè)置為 false window.addEventListener('mouseup', (e) => draw = false)
window.addEventListener('mousemove', (e) => { // 如果 draw 是 false,那么我們不會繪制 if(prevX == null || prevY == null || !draw){ prevX = e.clientX prevY = e.clientY return }
let currentX = e.clientX let currentY = e.clientY
ctx.beginPath() ctx.moveTo(prevX, prevY) ctx.lineTo(currentX, currentY) ctx.stroke()
prevX = currentX prevY = currentY })

太棒了!現(xiàn)在讓我們在 HTML 中添加一些按鈕來更改顏色、清除畫布和保存繪圖。 <!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'> <link rel='stylesheet' href='style.css'> <title>Linux迷 www.</title> </head> <body> <canvas id='canvas'></canvas> <div class='nav'> <!-- We will be accessing the data-clr in JavaScript --> <div class='clr' data-clr='#000'></div> <div class='clr' data-clr='#EF626C'></div> <div class='clr' data-clr='#fdec03'></div> <div class='clr' data-clr='#24d102'></div> <div class='clr' data-clr='#fff'></div> <button class='clear'>clear</button> <button class='save'>save</button> </div>
<script src='main.js'></script> </body> </html>
我們還需要在我們的 css 中設(shè)置它們的樣式。*{ margin: 0; padding: 0; }
.nav{ width: 310px; height: 50px; position: fixed; top: 0; left: 50%; transform: translateX(-50%); display: flex; align-items: center; justify-content: space-around; opacity: .3; transition: opacity .5s; } .nav:hover{ opacity: 1; }
.clr{ height: 30px; width: 30px; background-color: blue; border-radius: 50%; border: 3px solid rgb(214, 214, 214); transition: transform .5s; } .clr:hover{ transform: scale(1.2); } .clr:nth-child(1){ background-color: #000; } .clr:nth-child(2){ background-color: #EF626C; } .clr:nth-child(3){ background-color: #fdec03; } .clr:nth-child(4){ background-color: #24d102; } .clr:nth-child(5){ background-color: #fff; }
button{ border: none; outline: none; padding: .6em 1em; border-radius: 3px; background-color: #03bb56; color: #fff; } .save{ background-color: #0f65d4; }

現(xiàn)在,每當(dāng)單擊具有 clr 類的 div 時,我們都會添加以將線條的顏色設(shè)置為該 div 的 data-clr 屬性。 
OK!現(xiàn)在讓清除按鈕起作用。所以當(dāng)我們點(diǎn)擊它時,它應(yīng)該清除我們的畫布。 我們已經(jīng)成功完成了。 需要完整代碼的加小編微信:linuxgs 掃描以下二維碼:
口令:HTML5
來自:Linux迷 鏈接:https://www./html5-js-drawing-app.html
|