Angular Konva 自定义形状教程
要使用 ng2-konva 创建自定义形状,请使用 ko-shape 组件并提供一个 sceneFunc 绘图函数。
在 sceneFunc 内部,您可以使用原生 canvas 上下文以及 Konva 辅助方法(例如 context.fillStrokeShape(shape))来正确应用填充、描边和阴影样式。
说明:此演示使用 canvas 路径命令绘制 一个自定义的类三角形形状。
更多详情,请参阅 Shape API 参考。
自定义形状示例
import { Component } from '@angular/core';
import { StageConfig } from 'konva/lib/Stage';
import { ShapeConfig } from 'konva/lib/shapes/Shape';
import {
CoreShapeComponent,
StageComponent,
} from 'ng2-konva';
@Component({
selector: 'app-root',
standalone: true,
template: `
<ko-stage [config]="configStage">
<ko-layer>
<ko-shape [config]="configShape"></ko-shape>
</ko-layer>
</ko-stage>
`,
imports: [StageComponent, CoreShapeComponent],
})
export default class App {
public configStage: StageConfig = {
width: window.innerWidth,
height: window.innerHeight,
};
public configShape: ShapeConfig = {
x: 100,
y: 100,
fill: 'red',
stroke: 'black',
strokeWidth: 2,
sceneFunc: (context: any, shape: any) => {
context.beginPath();
context.moveTo(0, 0);
context.lineTo(100, 0);
context.lineTo(50, 100);
context.closePath();
context.fillStrokeShape(shape);
}
};
}