HarmonyOS Canvas 实现动画

HarmonyOS
2024-12-18 15:39:39
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-canvasrenderingcontext2d-V5

ArkTs的Canvas使用与webcanvas类似,相关动画实现可以移植webcanvas参考demo:

@Entry
@Component
struct Index {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  @State timerId: number = 0
  @State startAngle: number = Math.PI * 0.2 + (Math.PI / 180) * 90; // 起始角度(弧度制)
  @State endAngle: number = Math.PI * 1.8 + (Math.PI / 180) * 90; // 结束角度(弧度制)
  lengthToAdd: number = Math.PI / 360; // 每次绘制的长度(弧度制)
  @State currentAngle: number = 0
  @State step: number = 1;
  // 设置圆心坐标和半径
  centerX = 200;
  centerY = 200;
  radius = 100;

  aboutToAppear(): void {
    this.currentAngle = this.startAngle
    this.timerId = setInterval(() => {
      if (this.step %2 == 1) {
        this.currentAngle += this.lengthToAdd;
        this.drawArc();
        if (this.currentAngle > this.endAngle) {
          this.step++;
          this.context.globalCompositeOperation = "destination-out"; // 修改混合模式,准备擦除
          this.context.lineWidth = 21; // 擦除时,线更宽一些,防止有残留
        }
      } else {
        this.eraseArc();
        this.currentAngle -= this.lengthToAdd;
        if (this.currentAngle<= this.startAngle) {
          this.step++;
          this.context.globalCompositeOperation = "source-over"; // 修改混合模式,正常绘制
          this.context.lineWidth = 20; // 正常线宽
        }
      }
    }, 10)
  }

  drawArc() {
    // 绘制圆弧
    this.context.beginPath();
    this.context.arc(
      this.centerX, // 圆弧中心的 x 坐标
      this.centerY + 30, // 圆弧中心的 y 坐标
      this.radius, // 圆弧的半径
      this.currentAngle - this.lengthToAdd, // 圆弧的起始角度(弧度制)
      this.currentAngle + this.lengthToAdd // 圆弧的结束角度(弧度制)
    );
    this.context.stroke();
  }

  eraseArc() {
    this.context.beginPath();
    this.context.arc(
      this.centerX,
      this.centerY + 30,
      this.radius,
      this.currentAngle - this.lengthToAdd,
      this.currentAngle + 2 * this.lengthToAdd // 擦除时,擦除的更宽一些,防止有残留
    );
    this.context.stroke();
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() => {
          // 设置圆弧的颜色和宽度
          this.context.strokeStyle = Color.Green;
          this.context.lineWidth = 20;
        })
    }
    .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.
  • 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.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
分享
微博
QQ
微信
回复
2024-12-18 17:09:42
相关问题
HarmonyOS Canvas动画实现
490浏览 • 1回复 待解决
HarmonyOS canvas动画如何实现逐帧动画
838浏览 • 1回复 待解决
HarmonyOS 环形渐变canvas实现方式
590浏览 • 1回复 待解决
HarmonyOS 动画实现
909浏览 • 1回复 待解决
HarmonyOS canvas如何实现画线跟手效果
684浏览 • 1回复 待解决
canvas如何实现水印效果
1779浏览 • 1回复 待解决
HarmonyOS 如何实现RippleView动画
886浏览 • 1回复 待解决
HarmonyOS 如何实现WaveView动画
888浏览 • 1回复 待解决
HarmonyOS 动画效果实现
1022浏览 • 1回复 待解决
HarmonyOS 组合动画如何实现
625浏览 • 1回复 待解决
HarmonyOS clipShape 动画效果实现
618浏览 • 0回复 待解决
HarmonyOS 如何实现音频声浪动画
1542浏览 • 1回复 待解决
HarmonyOS 如何实现旋转动画
1124浏览 • 1回复 待解决
HarmonyOS 点赞动画实现方案
999浏览 • 1回复 待解决
HarmonyOS如何实现动态缩放动画
1029浏览 • 1回复 待解决
HarmonyOS 如何实现动画集合?
1008浏览 • 1回复 待解决
HarmonyOS动画用什么实现
903浏览 • 1回复 待解决
HarmonyOS Navigation实现Dialog转场动画
693浏览 • 1回复 待解决