手把手教你使用ArkTS中的canvas实现签名板功能
一、屏幕旋转
● 实现签名板的第一个功能就是旋转屏幕。旋转屏幕在各种框架中都有不一样的方式,比如:在H5端,我们一般是使用CSS中的transform属性中的rotate()方法来强制将网页横屏,然后实现一系列功能
● 在嵌套第三方APP中,我们一般是调用对应的SDK提供的方法,即可实现旋转屏幕
● .....
实现方式还有很多,各有千秋,相信HarmonyOS也会提供对应API方法来设置旋转屏幕。
而我自己则是在页面内通过 Window 对象的 setPreferredOrientation() 方法实现横竖屏切换。以下是我实现的完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | // 在EntryAbility.ts中获取窗口实例并赋值给全局变量,如此所有页面都可以通过全局// 变量去修改窗口信息,不需要重新获取 import UIAbility from '@ohos.app.ability.UIAbility' ; import window from '@ohos.window' ; export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { windowStage.getMainWindow((err, data) => { if (err.code) { console.error( '获取失败' + JSON.stringify(err)); return ; } console.info( '获取主窗口的实例:' + JSON.stringify(data)); globalThis.windowClass = data // 赋值给全局变量windowClass }); } } // 在具体页面中的使用 import window from '@ohos.window' ; @Entry @Componentstruct SignatureBoard { onPageShow() { // 获取旋转的方向,具体可以查看对应文档 let orientation = window.Orientation.LANDSCAPE_INVERTED; try { // 设置屏幕旋转 globalThis.windowClass.setPreferredOrientation(orientation, (err) => {}); } catch (exception) { console.error( '设置失败: ' + JSON.stringify(exception)); } } } |
二、canvas画布
解决了屏幕旋转问题,接下来实现签名功能。因为在之前就已经开发过,只要将对应的语法转成ArkTS的语法就好。以下是代码解析:2.1 按照官方文档使用canvas组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @Entry@Component struct SignatureBoard { private settings: RenderingContextSettings = new RenderingContextSettings( true ) private context: CanvasRenderingContext2D = new CanvasRenderingContext2D( this .settings) build() { Column() { Canvas( this .context) .width( '100%' ) .height( '100%' ) .backgroundColor( '#fff' ) .onReady(() => { }) } .width( '100%' ) .height( '100%' ) } } |
2.2 设置画笔的属性以及绑定手势功能。在js中我们基本都是使用鼠标事件来实现的,而在ArkTS中是通过手势方法来监听手指在屏幕上的操作,有很多种,大家需要用到的可以去查看对应的文档。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | build() { Column() { Canvas( this .context) .width( '100%' ) .height( '100%' ) .backgroundColor( '#fff' ) .onReady(() => { this .context.lineWidth = 3; // 设置画笔的粗细 this .context.strokeStyle = "#000" ; // 设置画笔的颜色 // 还可以设置很多,根据自己业务需要 }) .gesture( PanGesture( this .panOption) .onActionStart(( event : any) => { // 手指按下的时候 }) .onActionUpdate(( event : any) => { // 手指移动的时候 }) .onActionEnd(() => { // 手指离开的时候 }) ) } |
2.3 我们实现的手势的绑定,那么就可以实现手指在屏幕上滑动之后画布就绘画出对应的轨迹路线了,这里将会使用到一些画布的功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | @Entry @Componentstruct SignatureBoard { private lastX: number = 0; private lastY: number = 0; private isDown: Boolean = false ; private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.All, distance: 1 }) private settings: RenderingContextSettings = new RenderingContextSettings( true ) private context: CanvasRenderingContext2D = new CanvasRenderingContext2D( this .settings) draw(startX, startY, endX, endY) { // 起点 this .context.moveTo(startX, startY); // 终点 this .context.lineTo(endX, endY); // 调用 stroke,即可看到绘制的线条 this .context.stroke(); } build() { Column() { Canvas( this .context) .width( '100%' ) .height( '100%' ) .backgroundColor( '#fff' ) .onReady(() => { this .context.lineWidth = 3; this .context.strokeStyle = "#000" ; }) .gesture( PanGesture( this .panOption) .onActionStart(( event : any) => { this .isDown = true ; // 按下时的点作为起点 this .lastX = event .localX; this .lastY = event .localY; // 创建一个新的路径 this .context.beginPath(); }) .onActionUpdate(( event : any) => { // 没有按下就不管 if (! this .isDown) return ; const offsetX = event .localX const offsetY = event .localY // 调用绘制方法 this .draw( this .lastX, this .lastY, offsetX, offsetY); // 把当前移动时的坐标作为下一次的绘制路径的起点 this .lastX = offsetX; this .lastY = offsetY; }) .onActionEnd(() => { this .isDown = false ; // 关闭路径 this .context.closePath(); }) ) } .width( '100%' ) .height( '100%' ) } } |
以上就是我们实现签名板的完整思路以及代码了,有几个需要注意的点是:
1. new PanGestureOptions实例的时候需要把distance设置小一点,值越小灵敏度就越高
2. PanGesture的回调方法中event参数,官方默认给的属性类型为GestureEvent。但是我在编辑器源码中查看该属性没有我们定义我们想要的localX、localY,但是实际是有返回的,如果直接用编辑器会报错。所以需要将event定为any类型,这样就可以获取且不报错了。
这次的画布签名板的功能就分享这些,希望能够帮助各位开发者,后续会继续分享出更多在项目中经常用到的工具。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-12-20 【征集令】寻找2022年鸿蒙智联“出行新爆款产品”