HarmonyOS 實(shí)現(xiàn)一個(gè)手繪板
??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??
前言
最近在學(xué)習(xí)openHarmony,恰好之前了解過(guò)canvas,所以本篇文章分享一下我實(shí)現(xiàn)的一個(gè)手繪板,利用openHarmony內(nèi)置的API cnavas組件實(shí)現(xiàn)。
介紹
這是一個(gè)手繪板,并且可以根據(jù)滑動(dòng)屏幕速度,動(dòng)態(tài)生成線條大小,當(dāng)用戶觸摸屏幕,會(huì)生成線條,并且速度越快,線條越細(xì)。
效果展示
原理分析
1、繪制原理
使用前,需要線了解canvas組件,可以參考harmonyOS開(kāi)發(fā)者文檔,文檔介紹的非常詳細(xì),這里就不多介紹了。
首先,我們需要將canvas上下文對(duì)象,需要在觸摸移動(dòng)事件中綁定,因?yàn)槲覀兪峭ㄟ^(guò)觸摸來(lái)生成對(duì)應(yīng)線條的。
然后,屬性選擇lineCap,屬性值有三種:butt、round、square,我嘗試了后發(fā)現(xiàn)round效果比較好。
- 屬性值為butt時(shí)的效果
- 屬性值為round
- 屬性值為square
其實(shí)butt效果也還行,就是鋸齒太嚴(yán)重,雖然API中有內(nèi)置抗鋸齒屬性,但是不知道為啥設(shè)置了沒(méi)有效果,可能粒度太大了,現(xiàn)在這個(gè)粒度已經(jīng)有點(diǎn)卡了,如果把粒度小設(shè)置小一點(diǎn)估計(jì)更卡。
綜上還是選擇了round,它會(huì)將線端點(diǎn)以圓形結(jié)束,所以效果上更圓潤(rùn)。
最后將數(shù)組中的最后一個(gè)值取出,作為moveTo的坐標(biāo),將鼠標(biāo)移動(dòng)后的點(diǎn)作為lineTo的坐標(biāo),然后再通過(guò)stroke就能繪制出圖像。
繪制直線,通常使用moveTo ()與lineTo ()兩個(gè)方法。. moveTo ()方法用于將畫筆移至指定點(diǎn)并以改點(diǎn)為直線的開(kāi)始點(diǎn),lineTo ()則為結(jié)束點(diǎn)。
const el = this.$refs.canvas;
this.ctx = el.getContext('2d')
this.ctx.lineWidth =this.lineWidth/2
this.ctx.beginPath()
// 向線條的每個(gè)末端添加圓形線帽。
this.ctx.lineCap = 'square'
// 每次將數(shù)組中最后一個(gè)值取出,作為起始點(diǎn)
this.ctx.moveTo(this.ArrX[this.ArrX.length-1],this.ArrY[this.ArrY.length-1])
this.ctx.lineTo(e.touches[0].localX,e.touches[0].localY)
this.ctx.stroke()
this.ArrX.push(e.touches[0].localX)
this.ArrY.push(e.touches[0].localY)
2、線條粗細(xì)
想要通過(guò)速度來(lái)計(jì)算線條粗細(xì),那么可以是需要獲取兩點(diǎn)之間的時(shí)間,通過(guò)時(shí)間和距離得到速度。
當(dāng)觸發(fā)touchmove事件,將當(dāng)前時(shí)間戳存儲(chǔ)起來(lái),通過(guò)上一次觸發(fā)事件獲得的時(shí)間-當(dāng)前觸發(fā)事件獲得的時(shí)間,就可以得到兩次觸發(fā)事件的事件間隔,此時(shí)我們就獲得了兩點(diǎn)之間的時(shí)間。
再計(jì)算兩點(diǎn)之間的距離(平方和再開(kāi)根號(hào)),通過(guò) 路程/時(shí)間 = 速度計(jì)算出兩點(diǎn)之間的速度,從而可以動(dòng)態(tài)生成線條粗細(xì)。
// 計(jì)算線條粗細(xì)
const currTime = Date.now()
if(this.startTime !== 0){
const duration = currTime - this.startTime
// 傳入倒數(shù)第二個(gè)點(diǎn)和最后一個(gè)點(diǎn),和持續(xù)時(shí)間,會(huì)返回加速度
const v = this.speed(this.ArrX[this.ArrX.length-2],this.ArrY[this.ArrY.length-2],this.ArrX[this.ArrX.length-1],this.ArrY[this.ArrY.length-1],duration)
this.lineWidth = this.lineWidth/v
if(this.lineWidth>25){
this.lineWidth = 25
}
if(this.lineWidth<1){
this.lineWidth = 1
}
}
this.startTime = currTime
完整代碼
index.js:
// @ts-nocheck
export default {
data: {
ctx:'',
ArrX:[],
ArrY:[],
// 開(kāi)始時(shí)間
startTime:0,
lineWidth:14
},
// 偏移很多
touchstart(e){
// 開(kāi)始時(shí)間清空
this.startTime = 0
this.ArrX.push(e.touches[0].localX)
this.ArrY.push(e.touches[0].localY)
},
// 計(jì)算最后兩點(diǎn)的速度
speed(x1,y1,x2,y2,s){
const x = Math.abs(x1-x2)*Math.abs(x1-x2)
const y = Math.abs(y1-y2)*Math.abs(y1-y2)
return Math.sqrt(x+y)/s
},
touchmove(e){
// 計(jì)算線條粗細(xì)
const currTime = Date.now()
if(this.startTime !== 0){
const duration = currTime - this.startTime
// 傳入倒數(shù)第二個(gè)點(diǎn)和最后一個(gè)點(diǎn),和持續(xù)時(shí)間,會(huì)返回加速度
const v = this.speed(this.ArrX[this.ArrX.length-2],this.ArrY[this.ArrY.length-2],this.ArrX[this.ArrX.length-1],this.ArrY[this.ArrY.length-1],duration)
this.lineWidth = this.lineWidth/v
if(this.lineWidth>25){
this.lineWidth = 25
}
if(this.lineWidth<1){
this.lineWidth = 1
}
}
this.startTime = currTime
const el = this.$refs.canvas;
this.ctx = el.getContext('2d')
this.ctx.lineWidth =this.lineWidth/2
this.ctx.beginPath()
// 向線條的每個(gè)末端添加圓形線帽。
this.ctx.lineCap = 'square'
// 每次將數(shù)組中最后一個(gè)值取出,作為起始點(diǎn)
this.ctx.moveTo(this.ArrX[this.ArrX.length-1],this.ArrY[this.ArrY.length-1])
this.ctx.lineTo(e.touches[0].localX,e.touches[0].localY)
this.ctx.stroke()
this.ArrX.push(e.touches[0].localX)
this.ArrY.push(e.touches[0].localY)
},
touchend(e){
this.startTime = 0
}
}
index.hml:
<div class="container">
<canvas ref="canvas" class="canvas" @touchstart="touchstart"
@touchmove="touchmove" @touchend="touchend"/>
</div>
index.css:
.container{
margin: 50px;
}
.canvas{
height: 100%;
width: 100%;
background-color: #eee;
border: 1px solid #ffc300;
}
總結(jié)
不足點(diǎn):使用體驗(yàn)不是很好,后續(xù)還需要優(yōu)化。
最后,通過(guò)自定義組件,加深對(duì)HarmonyOS的開(kāi)發(fā),共建鴻蒙生態(tài)!
??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??