使用canvas渲染文本时,如何基于文本变化,正确绘制出对应的内容?

使用canvas渲染文本时,如何基于文本变化,正确绘制出对应的内容?

HarmonyOS
2024-05-06 23:08:23
922浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

content值的变化会同时刷新build的ui页面,但是不会重调this.context.fillText这个方法,需要监听并重新调用这个方法,同时需要清理画布内容;示例如下:

@Prop @Watch(‘contentUpdate’)content: string = “” 
 
contentUpdate() { 
this.context.clearRect(0, 0, 200, 200) // 清理画布内容 
this.context.fillText(this.content, 50, 50) // 重新填充 
} 
 
build() { 
Column() { 
Canvas(this.context) 
.width(‘100%’) 
.height(‘25%’) 
.backgroundColor(’#F5DC62’) 
.onReady(() => { 
//可以在这里绘制内容。 
this.context.font = ‘55px sans-serif’ 
this.context.fillText(this.content, 50, 50) 
console.error(“aaa — content=” + this.content) 
}) 
Text(this.content) 
.textAlign(TextAlign.Center)// .backgroundColor(‘gray’) 
.width(‘100%’) 
.height(‘50%’) 
.fontSize(60) 
} 
.borderColor(’#31525B’) 
.borderWidth(12) 
.width(‘100%’) 
.height(‘100%’) 
}
  • 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.
分享
微博
QQ
微信
回复
2024-05-07 15:54:11
相关问题
如何取消订阅输入法文本内容变化
1297浏览 • 1回复 待解决
Canvas绘制内容如何动态更新
2853浏览 • 1回复 待解决
HarmonyOS Canvas绘制内容如何更新
650浏览 • 1回复 待解决
基于measure实现文本测量
1653浏览 • 1回复 待解决
HarmonyOS 如何清空canvas绘制内容
862浏览 • 1回复 待解决
如何实现文本内容竖向布局
1034浏览 • 1回复 待解决
HarmonyOS如何测量文本内容长度?
948浏览 • 0回复 待解决
HarmonyOS 富文本渲染问题
1199浏览 • 1回复 待解决
webview 如何显示纯文本html内容
3201浏览 • 1回复 待解决