摘要:撤銷清空等操作撤銷前進(jìn)清空清空前后數(shù)據(jù)恢復(fù)到默認(rèn)數(shù)據(jù)地址查看代碼
Canvas API簡(jiǎn)介 調(diào)用方法效果展示
getImageData() 返回ImageData對(duì)象,該對(duì)象為畫布上指定的矩形復(fù)制像素?cái)?shù)據(jù)
putImageData() 把圖像數(shù)據(jù)(從指定的 ImageData 對(duì)象)放回畫布上
clearRect() 在給定的矩形內(nèi)清除指定的像素
toDataURL() 返回canvas圖像的URL
lineTo() 添加一個(gè)新點(diǎn),創(chuàng)建從該點(diǎn)到最后指定點(diǎn)的線條
stroke() 繪制已定義的路徑
beginPath() 起始一條路徑,或重置當(dāng)前路徑
moveTo() 把路徑移動(dòng)到畫布中的指定點(diǎn),不創(chuàng)建線條
調(diào)用屬性strokeStyle 設(shè)置或返回用于筆觸的顏色、漸變或模式
shadowBlur 設(shè)置或返回用于陰影的模糊級(jí)別
shadowColor 設(shè)置或返回用于陰影的顏色
lineWidth 設(shè)置或返回當(dāng)前的線條寬度
功能需求說(shuō)明更多API請(qǐng)參考 canvas基本使用
基礎(chǔ)線條繪制功能
筆觸顏色修改
筆刷粗細(xì)調(diào)整
撤回、前進(jìn)、情況功能
生成圖片
初始化數(shù)據(jù)colors: 筆觸顏色列表
brushs: 筆刷對(duì)應(yīng)的粗細(xì)
context: canvas context
imgUrl: 用于存放保存圖片的地址
canvasMoveUse: 是否允許執(zhí)行move時(shí)候繪制線條
preDrawAry: 存儲(chǔ)當(dāng)前表面狀態(tài)數(shù)組-上一步
nextDrawAry: 存儲(chǔ)當(dāng)前表面狀態(tài)數(shù)組-下一步
middleAry: 中間數(shù)組
lineWidth: 線條寬度
lineColor: 線條顏色
shadowBlur: 陰影
data() { return { colors: ["#fef4ac","#0018ba","#ffc200","#f32f15","#cccccc","#5ab639"], brushs: [{ className: "small fa fa-paint-brush", lineWidth: 3 },{ className: "middle fa fa-paint-brush", lineWidth: 6 },{ className: "big fa fa-paint-brush", lineWidth: 12 }], context: {}, imgUrl: [], canvasMoveUse: true, preDrawAry: [], nextDrawAry: [], middleAry: [], config: { lineWidth: 1, lineColor: "#f2849e", shadowBlur: 2 } } }設(shè)置繪畫配置
setCanvasStyle() { this.context.lineWidth = this.config.lineWidth this.context.shadowBlur = this.config.shadowBlur this.context.shadowColor = this.config.lineColor this.context.strokeStyle = this.config.lineColor }
筆觸顏色及粗細(xì)相關(guān)設(shè)置(點(diǎn)擊修改config數(shù)據(jù)):
畫筆的移動(dòng)操作
// 當(dāng)在屏幕中移動(dòng)時(shí)即開始繪制準(zhǔn)備 beginPath(e){ const canvas = document.querySelector("#canvas") if (e.target !== canvas) { this.context.beginPath() } }
// 在canvas中鼠標(biāo)按下 canvasDown(e) { // 讓move方法可用 this.canvasMoveUse = true // client是基于整個(gè)頁(yè)面的坐標(biāo) // offset是cavas距離頂部以及左邊的距離 const canvasX = e.clientX - e.target.parentNode.offsetLeft const canvasY = e.clientY - e.target.parentNode.offsetTop // 設(shè)置canvas的配置 this.setCanvasStyle() //清除子路徑 this.context.beginPath() // 移動(dòng)的起點(diǎn) this.context.moveTo(canvasX, canvasY) //當(dāng)前繪圖表面狀態(tài) const preData = this.context.getImageData(0, 0, 600, 400) //當(dāng)前繪圖表面進(jìn)棧 // 按下相當(dāng)于新的操作的開始,所以把當(dāng)前記錄數(shù)據(jù)放到prev中 this.preDrawAry.push(preData) },
// canvas中鼠標(biāo)移動(dòng) canvasMove(e) { if(this.canvasMoveUse) { // 只有允許移動(dòng)時(shí)調(diào)用 const t = e.target let canvasX let canvasY // 由于手機(jī)端和pc端獲取頁(yè)面坐標(biāo)方式不同,所以需要做出判斷 if(this.isPc()){ canvasX = e.clientX - t.parentNode.offsetLeft canvasY = e.clientY - t.parentNode.offsetTop }else { canvasX = e.changedTouches[0].clientX - t.parentNode.offsetLeft canvasY = e.changedTouches[0].clientY - t.parentNode.offsetTop } // 連接到移動(dòng)的位置并上色 this.context.lineTo(canvasX, canvasY) this.context.stroke() } },
// canvas中鼠標(biāo)放開 canvasUp(e){ const preData = this.context.getImageData(0, 0, 600, 400) if (!this.nextDrawAry.length) { // 在沒有撤銷過的情況下,將當(dāng)前數(shù)據(jù)放入prev //當(dāng)前繪圖表面進(jìn)棧 this.middleAry.push(preData) } else { // 在撤銷的情況下,將在后面步驟的數(shù)據(jù)情況記錄 this.middleAry = [] this.middleAry = this.middleAry.concat(this.preDrawAry) this.middleAry.push(preData) this.nextDrawAry = [] } // 設(shè)置move時(shí)不可繪制 this.canvasMoveUse = false }
為了保證移動(dòng)端的可用性,加入touchstart等。
撤銷清空等操作// 撤銷 if (this.preDrawAry.length) { const popData = this.preDrawAry.pop() const midData = this.middleAry[this.preDrawAry.length + 1] this.nextDrawAry.push(midData) this.context.putImageData(popData, 0, 0) }
// 前進(jìn) if (this.nextDrawAry.length) { const popData = this.nextDrawAry.pop() const midData = this.middleAry[this.middleAry.length - this.nextDrawAry.length - 2] this.preDrawAry.push(midData) this.context.putImageData(popData, 0, 0) }
// 清空 this.context.clearRect(0, 0, this.context.canvas.width, this.context.canvas.height) // 清空前后數(shù)據(jù) this.preDrawAry = [] this.nextDrawAry = [] // middleAry恢復(fù)到默認(rèn)數(shù)據(jù) this.middleAry = [this.middleAry[0]]
demo地址
查看代碼
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/81082.html
摘要:系列種優(yōu)化頁(yè)面加載速度的方法隨筆分類中個(gè)最重要的技術(shù)點(diǎn)常用整理網(wǎng)頁(yè)性能管理詳解離線緩存簡(jiǎn)介系列編寫高性能有趣的原生數(shù)組函數(shù)數(shù)據(jù)訪問性能優(yōu)化方案實(shí)現(xiàn)的大排序算法一怪對(duì)象常用方法函數(shù)收集數(shù)組的操作面向?qū)ο蠛驮屠^承中關(guān)鍵詞的優(yōu)雅解釋淺談系列 H5系列 10種優(yōu)化頁(yè)面加載速度的方法 隨筆分類 - HTML5 HTML5中40個(gè)最重要的技術(shù)點(diǎn) 常用meta整理 網(wǎng)頁(yè)性能管理詳解 HTML5 ...
摘要:系列種優(yōu)化頁(yè)面加載速度的方法隨筆分類中個(gè)最重要的技術(shù)點(diǎn)常用整理網(wǎng)頁(yè)性能管理詳解離線緩存簡(jiǎn)介系列編寫高性能有趣的原生數(shù)組函數(shù)數(shù)據(jù)訪問性能優(yōu)化方案實(shí)現(xiàn)的大排序算法一怪對(duì)象常用方法函數(shù)收集數(shù)組的操作面向?qū)ο蠛驮屠^承中關(guān)鍵詞的優(yōu)雅解釋淺談系列 H5系列 10種優(yōu)化頁(yè)面加載速度的方法 隨筆分類 - HTML5 HTML5中40個(gè)最重要的技術(shù)點(diǎn) 常用meta整理 網(wǎng)頁(yè)性能管理詳解 HTML5 ...
摘要:使用庫(kù)讀寫環(huán)境光照度傳感器本文將教大家如何快速使用庫(kù)讀取光照度數(shù)據(jù)。五實(shí)驗(yàn)樣機(jī)測(cè)試展示通過之前配置好的面板,通過涂鴉智能進(jìn)行配網(wǎng)實(shí)時(shí)采集光照度傳感器的數(shù)據(jù)。 使用STM32 HAL庫(kù)讀寫環(huán)境光照度傳感器(BH1750) 本文將教大家如何快速使用STM32HAL庫(kù)讀取光照度數(shù)據(jù)。 實(shí)現(xiàn)功能:通...
摘要:開啟掃描時(shí)需要設(shè)備處于配網(wǎng)狀態(tài)一分類配網(wǎng)子設(shè)備可以通過使用手機(jī)藍(lán)牙直接掃描獲取設(shè)備到設(shè)備基礎(chǔ)信息,再使用配網(wǎng)接口實(shí)現(xiàn)設(shè)備的本地配網(wǎng)。 ? (一)分類 ? (二)設(shè)備配置 ? (三)設(shè)備管理 ? ? 設(shè)備管理,大體分為兩類,mesh 和 其他 ? ? 獲取設(shè)備列表,給涂鴉sdk發(fā)送當(dāng)前房間id...
閱讀 3593·2023-04-25 14:20
閱讀 1233·2021-09-10 10:51
閱讀 1173·2019-08-30 15:53
閱讀 482·2019-08-30 15:43
閱讀 2338·2019-08-30 14:13
閱讀 2817·2019-08-30 12:45
閱讀 1227·2019-08-29 16:18
閱讀 1183·2019-08-29 16:12