Angular Konva 简单动画教程
在 Angular 中使用 Konva
创建简单动画,可以使用 ng2-konva
库中的 Animation
组件或者手动动画循环。
有关属性和方法的完整列表,请参见 Konva API 参考。
简单动画示例
import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core'; import { StageConfig } from 'konva/lib/Stage'; import { CircleConfig } from 'konva/lib/shapes/Circle'; import * as Konva from 'konva'; import { CoreShapeComponent, StageComponent, } from 'ng2-konva'; @Component({ selector: 'app-root', template: ` <ko-stage [config]="configStage"> <ko-layer> <ko-circle #circle [config]="configCircle"></ko-circle> </ko-layer> </ko-stage> `, imports: [StageComponent, CoreShapeComponent], }) export default class App implements OnInit, OnDestroy { @ViewChild('circle') circle!: any; private animation: any = null; public configStage: StageConfig = { width: window.innerWidth, height: window.innerHeight, }; public configCircle: CircleConfig = { x: 100, y: 100, radius: 50, fill: 'red', stroke: 'black', strokeWidth: 4 }; ngAfterViewInit() { const circle = this.circle.getNode(); this.animation = new Konva.Animation((frame: any) => { const time = frame.time; const x = 100 + Math.sin(time / 1000) * 100; circle.x(x); }, circle.getLayer()); this.animation.start(); } ngOnDestroy() { if (this.animation) { this.animation.stop(); } } }