// #创建固定宽高的 Leafer [window]
import {
Leafer, Platform,
Rect,
registerUI,
type ILeaferCanvas,
type IRenderOptions,
type IUIInputData,
} from 'leafer-ui'
export const BackgroundUITag = 'BackgroundFrame';
const svg = Platform.toURL(
`
`,
'svg',
);
class BackgroundUI extends Rect {
// 2. 定义全局唯一的 tag 名称
public get __tag() {
return BackgroundUITag;
}
constructor(input: IUIInputData) {
if (!input.fill) {
input.fill = {
type: 'image',
url: svg,
mode: 'repeat',
format: 'svg',
size: { height: 100, width: 100 },
};
}
super(input);
}
// 绘制自定义内容 https://www.leaferjs.com/ui/reference/display/custom/context.html
__draw(canvas: ILeaferCanvas, options: IRenderOptions): void {
const scale = this.zoomLayer.scaleX ?? 1;
const l = 100 / scale;
// 修改第二层级的数据不会触发渲染, 例如 this.fill.size.height = 100 / scale; 不会触发渲染,所以这里需要重新赋值 fill 属性
// 修改对象 https://www.leaferjs.com/ui/guide/basic/style.html#%E4%BF%AE%E6%94%B9%E5%AF%B9%E8%B1%A1
this.fill = {
type: 'image',
url: svg,
mode: 'repeat', // 相当于 CSS 的 background-repeat: repeat
format: 'svg',
size: { height: l, width: l },
};
super.__draw(canvas, options); // 调用父类的绘制方法,确保其他样式正常渲染
}
}
registerUI()(BackgroundUI)
const leafer = new Leafer({
view: window, // view 参数支持设置 window 对象
width: 600, // 不能设置为 0, 否则会变成自动布局
height: 300,
fill: '#333'
})
leafer.add(Rect.one({ fill: '#32cd79', draggable: true }, 200, 100))