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

HarmonyOS 自定義JS組件之畫(huà)板組件

開(kāi)發(fā) 前端 OpenHarmony
隨著科技的發(fā)達(dá),在日常生活中我們逐漸的脫離的筆和紙,但往往還有許多場(chǎng)景我們還是需要涂涂畫(huà)畫(huà),不論是開(kāi)會(huì)或者設(shè)計(jì)等等剛好身邊沒(méi)有筆紙,我們的畫(huà)板就排上用場(chǎng)了

[[432129]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

前言

隨著科技的發(fā)達(dá),在日常生活中我們逐漸的脫離的筆和紙,但往往還有許多場(chǎng)景我們還是需要涂涂畫(huà)畫(huà),不論是開(kāi)會(huì)或者設(shè)計(jì)等等剛好身邊沒(méi)有筆紙,我們的畫(huà)板就排上用場(chǎng)了。我們還可以擴(kuò)展將其作為和鍵盤(pán)鼠標(biāo)一樣的輸入設(shè)備等等。還有更多的使用場(chǎng)景讓我們一起探索。

功能介紹

畫(huà)板組件是基于HarmonyOS下的JavaScript UI框架開(kāi)發(fā)的一款組件,主要特點(diǎn)如下:

  1. 支持畫(huà)筆粗細(xì)選擇
  2. 支持畫(huà)筆顏色定義選中
  3. 畫(huà)筆顏色除了默認(rèn)顏色,可點(diǎn)擊色盤(pán),選擇自己想要的顏色
  4. 一鍵清除畫(huà)板
  5. 支持橡皮擦功能
  6. 支持保存圖片功能

注意:

  • 保存功能需要api >= 6,得到的是一個(gè)base64的數(shù)據(jù),可以根據(jù)自己的需要自行調(diào)整
  • 功能操作區(qū)域可左右滑動(dòng)選擇

組件使用說(shuō)明

  1. <element name="drawing-board" src="../../common/component/drawingBoard.hml"></element> 
  2. <drawing-board @event-dataurl="getUrl"></drawing-board> 
  3.  
  4. // 獲取圖片信息,可以根據(jù)自己的圖片組件需要,處理對(duì)應(yīng)的base64 數(shù)據(jù) 
  5. getUrl(valueUrl){ 
  6.     console.log('得到圖片信息'+JSON.stringify(valueUrl)); // "data:image/png;base64,xxxxxxxx..." 

主要代碼

組件傳值

  1. // 可以根據(jù)自己的實(shí)際情況,傳入需要的畫(huà)筆顏色和畫(huà)筆大小 
  2. props: { 
  3. // 畫(huà)筆粗細(xì) 
  4. brushSizes: { 
  5.     type: Array, 
  6.     default: [3,8,16] 
  7. }, 
  8. // 畫(huà)筆顏色 
  9. brushColor: { 
  10.     type: Array, 
  11.     default: ['#ffffff',"#000000","#ff9800",'#3f51b5','#ff5722',"#4caf50"

初始化畫(huà)布

  1. initCanvas(){ 
  2.        this.canvas = this.$refs.canvas; 
  3.        this.canvasTxt = this.canvas.getContext('2d',{ antialias: true }); 
  4.        this.canvasTxt.strokeStyle = this.signColor; 
  5.        this.canvasTxt.lineJoin = "round"
  6.        this.canvasTxt.lineCap = "round"
  7.        this.canvasTxt.globalCompositeOperation = this.penType; 
  8.        this.canvasTxt.lineWidth = this.lineWidth; 
  9.    } 

設(shè)置畫(huà)板工具

  1. setTool(item,index) { 
  2.        console.log(index); 
  3.        if(index == 0 || index == 1){ 
  4.            this.toolOn = index
  5.        } 
  6.        let type = item.type; 
  7.        switch(type){ 
  8.            case 1: 
  9.                // 畫(huà)筆 
  10.                this.penType = 'source-over'
  11.                this.canvasTxt.lineWidth = this.lineWidth; 
  12.                this.canvasTxt.globalCompositeOperation = this.penType; 
  13.                break; 
  14.            case 2: 
  15.               // 橡皮檫 
  16.                this.penType = 'destination-out'
  17.                this.canvasTxt.lineWidth = this.lineWidth; 
  18.                this.canvasTxt.globalCompositeOperation = this.penType; 
  19.                break; 
  20.            case 3: 
  21.                this.overwrite(); 
  22.                break; 
  23.            case 4: 
  24.                // 保存 
  25.                this.savaCanvas(); 
  26.                break; 
  27.        } 
  28.    }, 

畫(huà)筆顏色選擇

  1. selectColor(color,index) { 
  2.     this.colorOn = index
  3.     this.signColor = color; 
  4.     this.canvasTxt.fillStyle = this.signColor; 
  5.     this.canvasTxt.strokeStyle = this.signColor; 
  6.     this.canvasTxt.lineWidth = this.lineWidth; 
  7. }, 

畫(huà)筆粗細(xì)設(shè)置

  1. selectSize(size,index) { 
  2.         this.sizeOn = index
  3.         this.lineWidth = size
  4.         this.canvasTxt.lineWidth = this.lineWidth; 
  5.     }, 

開(kāi)始畫(huà)線

  1. drawLine(beginPoint, controlPoint, endPoint) { 
  2.   this.canvasTxt.beginPath(); 
  3.   this.canvasTxt.moveTo(beginPoint.x, beginPoint.y); 
  4.   this.canvasTxt.quadraticCurveTo( 
  5.       controlPoint.x, 
  6.       controlPoint.y, 
  7.       endPoint.x, 
  8.       endPoint.y 
  9.   ); 
  10.   this.canvasTxt.stroke(); 
  11.   this.canvasTxt.closePath(); 
  12. }, 

一鍵清除功能

  1. overwrite() { 
  2.   this.canvasTxt.clearRect(0, 0, 1920,1920); 
  3.   this.points = []; 
  4.   this.isDraw = false; //畫(huà)板標(biāo)記 
  5. }, 

保存功能

  1. savaCanvas(){ 
  2.     var dataURL = this.$refs.canvas.toDataURL(); 
  3.     // 保存畫(huà)板的到的是base64信息 
  4.     this.$emit('eventDataurl',dataURL) 

效果演示

#星光計(jì)劃1.0# HarmonyOS 自定義JS組件之畫(huà)板組件-鴻蒙HarmonyOS技術(shù)社區(qū)

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

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

2022-10-25 15:12:24

自定義組件鴻蒙

2022-10-26 15:54:46

canvas組件鴻蒙

2022-07-06 20:24:08

ArkUI計(jì)時(shí)組件

2022-05-20 14:34:20

list組件鴻蒙操作系統(tǒng)

2021-09-15 10:19:15

鴻蒙HarmonyOS應(yīng)用

2022-02-21 15:16:30

HarmonyOS鴻蒙操作系統(tǒng)

2021-11-24 10:02:53

鴻蒙HarmonyOS應(yīng)用

2022-06-30 14:02:07

鴻蒙開(kāi)發(fā)消息彈窗組件

2022-07-15 16:45:35

slider滑塊組件鴻蒙

2022-06-20 15:43:45

switch開(kāi)關(guān)鴻蒙

2022-02-16 16:09:12

鴻蒙游戲操作系統(tǒng)

2022-02-16 15:25:31

JS代碼Canvas鴻蒙

2022-04-24 15:17:56

鴻蒙操作系統(tǒng)

2021-02-20 12:34:53

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

2021-12-24 15:46:23

鴻蒙HarmonyOS應(yīng)用

2022-07-12 16:56:48

自定義組件鴻蒙

2022-05-23 10:53:54

canvas柱狀圖鴻蒙

2023-02-20 15:20:43

啟動(dòng)頁(yè)組件鴻蒙

2021-11-22 10:00:33

鴻蒙HarmonyOS應(yīng)用

2021-12-21 15:22:22

鴻蒙HarmonyOS應(yīng)用
點(diǎn)贊
收藏

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