HarmonyOS 自定义JS组件之画板组件

开发 前端 OpenHarmony
随着科技的发达,在日常生活中我们逐渐的脱离的笔和纸,但往往还有许多场景我们还是需要涂涂画画,不论是开会或者设计等等刚好身边没有笔纸,我们的画板就排上用场了

[[432129]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

前言

随着科技的发达,在日常生活中我们逐渐的脱离的笔和纸,但往往还有许多场景我们还是需要涂涂画画,不论是开会或者设计等等刚好身边没有笔纸,我们的画板就排上用场了。我们还可以扩展将其作为和键盘鼠标一样的输入设备等等。还有更多的使用场景让我们一起探索。

功能介绍

画板组件是基于HarmonyOS下的JavaScript UI框架开发的一款组件,主要特点如下:

  1. 支持画笔粗细选择
  2. 支持画笔颜色定义选中
  3. 画笔颜色除了默认颜色,可点击色盘,选择自己想要的颜色
  4. 一键清除画板
  5. 支持橡皮擦功能
  6. 支持保存图片功能

注意:

  • 保存功能需要api >= 6,得到的是一个base64的数据,可以根据自己的需要自行调整
  • 功能操作区域可左右滑动选择

组件使用说明

  1. <element name="drawing-board" src="../../common/component/drawingBoard.hml"></element> 
  2. <drawing-board @event-dataurl="getUrl"></drawing-board> 
  3.  
  4. // 获取图片信息,可以根据自己的图片组件需要,处理对应的base64 数据 
  5. getUrl(valueUrl){ 
  6.     console.log('得到图片信息'+JSON.stringify(valueUrl)); // "data:image/png;base64,xxxxxxxx..." 

主要代码

组件传值

  1. // 可以根据自己的实际情况,传入需要的画笔颜色和画笔大小 
  2. props: { 
  3. // 画笔粗细 
  4. brushSizes: { 
  5.     type: Array, 
  6.     default: [3,8,16] 
  7. }, 
  8. // 画笔颜色 
  9. brushColor: { 
  10.     type: Array, 
  11.     default: ['#ffffff',"#000000","#ff9800",'#3f51b5','#ff5722',"#4caf50"

初始化画布

  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.    } 

设置画板工具

  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.                // 画笔 
  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.    }, 

画笔颜色选择

  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. }, 

画笔粗细设置

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

开始画线

  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; //画板标记 
  5. }, 

保存功能

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

效果演示

#星光计划1.0# HarmonyOS 自定义JS组件之画板组件-鸿蒙HarmonyOS技术社区

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2022-07-06 20:24:08

ArkUI计时组件

2021-09-15 10:19:15

鸿蒙HarmonyOS应用

2022-05-20 14:34:20

list组件鸿蒙操作系统

2022-02-21 15:16:30

HarmonyOS鸿蒙操作系统

2022-07-15 16:45:35

slider滑块组件鸿蒙

2022-06-30 14:02:07

鸿蒙开发消息弹窗组件

2022-04-24 15:17:56

鸿蒙操作系统

2022-06-20 15:43:45

switch开关鸿蒙

2022-02-16 16:09:12

鸿蒙游戏操作系统

2021-11-24 10:02:53

鸿蒙HarmonyOS应用

2022-02-16 15:25:31

JS代码Canvas鸿蒙

2021-12-24 15:46:23

鸿蒙HarmonyOS应用

2022-07-12 16:56:48

自定义组件鸿蒙

2023-02-20 15:20:43

启动页组件鸿蒙

2022-05-23 10:53:54

canvas柱状图鸿蒙

2021-11-22 10:00:33

鸿蒙HarmonyOS应用

2021-12-21 15:22:22

鸿蒙HarmonyOS应用

2022-10-25 15:12:24

自定义组件鸿蒙

2022-10-26 15:54:46

canvas组件鸿蒙

2022-06-23 07:23:34

自定义组件计时器
点赞
收藏

51CTO技术栈公众号