2024年最新鸿蒙开发实例 | 分布式涂鸦_鸿蒙开发大作业(2)

    //坐标
    var p = {
        x: e.touches[0].localX,
        y: e.touches[0].localY,
        c: this.selColor,
        flag: "line"
    }
    this.sharePoints.push(p)

this.paintCanvas§
},
//Touch事件结束
paintEnd(e) {
this.cxt.closePath();
},
//本地绘制自由线条
paintCanvas(point) {
if (point.flag == “start”) {
this.cxt.beginPath();
this.cxt.strokeStyle = point.c
this.cxt.lineWidth = this.lineHeight
this.cxt.lineCap = “round”
this.cxt.lineJoin = “round”
this.cxt.moveTo(point.x,point.y);
} else if (point.flag == “line”) {
this.cxt.lineTo(point.x,point.y);
this.cxt.stroke()
}
},

//启动流转
setUpRemote: async function transfer() {
try {
await FeatureAbility.continueAbility(0,null)
} catch (e) {
console.error(“迁移出错:” + JSON.stringify(e))
}
},
onStartContinuation: function onStartContinuation() {
//判断当前的状态是不是适合迁移
return true;
},
onCompleteContinuation: function onCompleteContinuation(code) {
//迁移操作完成,code返回结果
return true
},
onSaveData: function onSaveData(saveData) {
//数据保存到savedData中进行迁移。
saveData.points = this.sharePoints;
return true
},
onRestoreData: function onRestoreData(restoreData) {
//收到迁移数据,恢复。
this.sharePoints = restoreData.points;
return true
}
}


## 2、**实现画板实时共享功能**


多设备间实现涂鸦画板数据同步,在鸿蒙操作系统中,需要通过分布式数据库来完成。同时需要Service Ability把分布式数据库中的数据推送给前端的FA(JavaScript页面)。


下面介绍如何为涂鸦画板添加实时共享功能,步骤如下。


### **步骤1:****通过分布式数据库实现数据共享。**


定义一个BoardServiceAbility 的PA,如代码示例3所示,重写onStart方法,在onStart方法中引用和创建分布式数据服务,storeID为数据库名称。


代码示例3 创建分布式数据服务



public class

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值