自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS 實現(xiàn)一個手繪板

系統(tǒng) OpenHarmony
本篇文章分享一下我實現(xiàn)的一個手繪板,利用openHarmony內(nèi)置的API cnavas組件實現(xiàn)。

??想了解更多關(guān)于開源的內(nèi)容,請訪問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??

前言

最近在學(xué)習(xí)openHarmony,恰好之前了解過canvas,所以本篇文章分享一下我實現(xiàn)的一個手繪板,利用openHarmony內(nèi)置的API cnavas組件實現(xiàn)。

介紹

這是一個手繪板,并且可以根據(jù)滑動屏幕速度,動態(tài)生成線條大小,當(dāng)用戶觸摸屏幕,會生成線條,并且速度越快,線條越細(xì)。

效果展示

#夏日挑戰(zhàn)賽# HarmonyOS 實現(xiàn)一個手繪板-開源基礎(chǔ)軟件社區(qū)

原理分析

1、繪制原理

使用前,需要線了解canvas組件,可以參考harmonyOS開發(fā)者文檔,文檔介紹的非常詳細(xì),這里就不多介紹了。

首先,我們需要將canvas上下文對象,需要在觸摸移動事件中綁定,因為我們是通過觸摸來生成對應(yīng)線條的。

然后,屬性選擇lineCap,屬性值有三種:butt、round、square,我嘗試了后發(fā)現(xiàn)round效果比較好。

  • 屬性值為butt時的效果

#夏日挑戰(zhàn)賽# HarmonyOS 實現(xiàn)一個手繪板-開源基礎(chǔ)軟件社區(qū)

  • 屬性值為round

  • 屬性值為square

其實butt效果也還行,就是鋸齒太嚴(yán)重,雖然API中有內(nèi)置抗鋸齒屬性,但是不知道為啥設(shè)置了沒有效果,可能粒度太大了,現(xiàn)在這個粒度已經(jīng)有點卡了,如果把粒度小設(shè)置小一點估計更卡。
綜上還是選擇了round,它會將線端點以圓形結(jié)束,所以效果上更圓潤。

最后將數(shù)組中的最后一個值取出,作為moveTo的坐標(biāo),將鼠標(biāo)移動后的點作為lineTo的坐標(biāo),然后再通過stroke就能繪制出圖像。

繪制直線,通常使用moveTo ()與lineTo ()兩個方法。. moveTo ()方法用于將畫筆移至指定點并以改點為直線的開始點,lineTo ()則為結(jié)束點。

const el = this.$refs.canvas;
this.ctx = el.getContext('2d')
this.ctx.lineWidth =this.lineWidth/2
this.ctx.beginPath()
// 向線條的每個末端添加圓形線帽。
this.ctx.lineCap = 'square'
// 每次將數(shù)組中最后一個值取出,作為起始點
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ì)

想要通過速度來計算線條粗細(xì),那么可以是需要獲取兩點之間的時間,通過時間和距離得到速度。

當(dāng)觸發(fā)touchmove事件,將當(dāng)前時間戳存儲起來,通過上一次觸發(fā)事件獲得的時間-當(dāng)前觸發(fā)事件獲得的時間,就可以得到兩次觸發(fā)事件的事件間隔,此時我們就獲得了兩點之間的時間。

再計算兩點之間的距離(平方和再開根號),通過 路程/時間 = 速度計算出兩點之間的速度,從而可以動態(tài)生成線條粗細(xì)。

// 計算線條粗細(xì)
const currTime = Date.now()
if(this.startTime !== 0){
const duration = currTime - this.startTime
// 傳入倒數(shù)第二個點和最后一個點,和持續(xù)時間,會返回加速度
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:[],
// 開始時間
startTime:0,
lineWidth:14
},
// 偏移很多
touchstart(e){
// 開始時間清空
this.startTime = 0
this.ArrX.push(e.touches[0].localX)
this.ArrY.push(e.touches[0].localY)
},
// 計算最后兩點的速度
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){
// 計算線條粗細(xì)
const currTime = Date.now()
if(this.startTime !== 0){
const duration = currTime - this.startTime
// 傳入倒數(shù)第二個點和最后一個點,和持續(xù)時間,會返回加速度
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()
// 向線條的每個末端添加圓形線帽。
this.ctx.lineCap = 'square'
// 每次將數(shù)組中最后一個值取出,作為起始點
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é)

不足點:使用體驗不是很好,后續(xù)還需要優(yōu)化。

最后,通過自定義組件,加深對HarmonyOS的開發(fā),共建鴻蒙生態(tài)!

??想了解更多關(guān)于開源的內(nèi)容,請訪問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??。

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2024-10-12 16:38:09

2022-08-02 14:21:20

滑動驗證碼鴻蒙

2022-07-28 14:20:44

懸浮球鴻蒙

2022-08-01 14:07:26

canvas繪畫板

2009-07-02 10:02:40

JSP程序

2020-11-19 10:25:24

MQTT

2020-10-27 10:00:26

鴻蒙開發(fā)板物聯(lián)網(wǎng)

2021-09-07 07:34:42

CSS 技巧代碼重構(gòu)

2021-06-02 16:32:23

鴻蒙HarmonyOS應(yīng)用

2020-09-24 11:46:03

Promise

2017-12-12 15:24:32

Web Server單線程實現(xiàn)

2020-12-17 12:06:49

鴻蒙應(yīng)用鴻蒙開發(fā)

2022-01-04 11:08:02

實現(xiàn)Localcache存儲

2014-04-14 15:54:00

print()Web服務(wù)器

2020-10-30 17:12:05

Hi3861

2023-05-22 09:10:53

CSSloading 效

2011-03-28 09:56:03

存儲增刪操作

2023-02-26 01:37:57

goORM代碼

2023-03-01 09:39:40

調(diào)度系統(tǒng)

2022-11-29 17:34:43

虛擬形象系統(tǒng)
點贊
收藏

51CTO技術(shù)棧公眾號