|
|
@@ -0,0 +1,92 @@
|
|
|
+import { DrawTextAlign } from "."
|
|
|
+
|
|
|
+/**
|
|
|
+ * 传入文本,图案,可以画出对应的文字
|
|
|
+ */
|
|
|
+
|
|
|
+@Component
|
|
|
+export struct DrawText{
|
|
|
+ private settings: RenderingContextSettings = new RenderingContextSettings(true)
|
|
|
+ private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 画的范围
|
|
|
+ */
|
|
|
+ @Prop drawWidth:number=100
|
|
|
+ @Prop drawHeight:number=100
|
|
|
+ @Prop draw_FontSize:number=16
|
|
|
+ @Prop draw_TextStyle:string="sans-serif"
|
|
|
+ @Prop bgc:ResourceColor=Color.Black
|
|
|
+
|
|
|
+ @Prop draw_Position:CanvasTextAlign='center'
|
|
|
+ // @Prop textAlign:
|
|
|
+ @Prop text:string="你好你好你好"
|
|
|
+
|
|
|
+ @Prop underline:string='bottom'
|
|
|
+ @Prop colors:string[][]=[['0','#ff0000'],['0.5','#ffffff'],['1','#00ff00']]
|
|
|
+ @State opacityValue:number=1
|
|
|
+
|
|
|
+ startWidth:number=0
|
|
|
+ startHeight:number=0
|
|
|
+
|
|
|
+ // this.grad=this.context.createLinearGradient(0, 0, this.drawWidth, this.drawHeight)
|
|
|
+ // grad.addColorStop(0.0, '#ff0000')
|
|
|
+ // grad.addColorStop(0.5, '#ffffff')
|
|
|
+ // grad.addColorStop(1.0, '#00ff00')
|
|
|
+ /**
|
|
|
+ * 如果有自定义的文字样式
|
|
|
+ * // 加载rawfile目录下的自定义字体文件HarmonyOS_Sans_Thin_Italic.ttf
|
|
|
+ let fontCollection = text.FontCollection.getGlobalInstance();
|
|
|
+ fontCollection.loadFontSync('HarmonyOS_Sans_Thin_Italic', $rawfile("HarmonyOS_Sans_Thin_Italic.ttf"))
|
|
|
+ // 加粗,字体大小为30vp,字体系列为HarmonyOS_Sans_Thin_Italic
|
|
|
+ this.context.font = "bold 30vp HarmonyOS_Sans_Thin_Italic"
|
|
|
+ */
|
|
|
+
|
|
|
+ grad:CanvasGradient=this.context.createLinearGradient(0, 0, this.drawWidth, this.drawHeight)
|
|
|
+
|
|
|
+ aboutToAppear(): void {
|
|
|
+ // this.drawCircleWithCustomRadii()
|
|
|
+ this.grad=this.context.createLinearGradient(0, 0, this.drawWidth, this.drawHeight)
|
|
|
+ this.colors.forEach((item)=>{
|
|
|
+ this.grad?.addColorStop(Number(item[0]),item[1])
|
|
|
+ })
|
|
|
+
|
|
|
+ }
|
|
|
+ private drawCircleWithCustomRadii() {
|
|
|
+ // this.context.clearRect(0,0,0,0)
|
|
|
+ this.context.clearRect(0, 0, this.drawWidth, this.drawHeight); // 清空Canvas的内容
|
|
|
+ this.context.textAlign =this.draw_Position
|
|
|
+ //有很多值
|
|
|
+ // this.context.textBaseline = 'middle'
|
|
|
+ // this.context.fillStyle = '#333333'
|
|
|
+ this.context.fillStyle =this.grad
|
|
|
+ this.context.font = `${this.draw_FontSize}vp ${this.draw_TextStyle}`
|
|
|
+ this.context.fillText(this.text,this.drawWidth/2,this.drawHeight/2)
|
|
|
+ this.context.restore()
|
|
|
+ }
|
|
|
+
|
|
|
+ build() {
|
|
|
+ // Column(){
|
|
|
+ Canvas(this.context)
|
|
|
+ .width(this.drawWidth)
|
|
|
+ .height(this.drawHeight)
|
|
|
+ .opacity(this.opacityValue)
|
|
|
+ .animation({
|
|
|
+ duration: 500,
|
|
|
+ playMode: PlayMode.Alternate,
|
|
|
+ iterations: -1,
|
|
|
+ curve: Curve.EaseInOut
|
|
|
+ })
|
|
|
+ .onAppear(() => {
|
|
|
+ this.opacityValue = 0.8 //文本闪烁效果
|
|
|
+ })
|
|
|
+ .backgroundColor(Color.Transparent)
|
|
|
+ .onReady(() => {
|
|
|
+ this.drawCircleWithCustomRadii()
|
|
|
+ })
|
|
|
+ // }.width(this.drawWidth)
|
|
|
+ // .height(this.drawHeight)
|
|
|
+ // .backgroundColor(this.bgc)
|
|
|
+
|
|
|
+ }
|
|
|
+}
|