Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在leafer-ui/worker,无法使用 json 创建元素 #362

Open
hbalxzdl opened this issue Feb 19, 2025 · 3 comments
Open

在leafer-ui/worker,无法使用 json 创建元素 #362

hbalxzdl opened this issue Feb 19, 2025 · 3 comments

Comments

@hbalxzdl
Copy link

错误:Uncaught ReferenceError: CanvasRenderingContext2D is not defined

@leaferjs
Copy link
Owner

请给出具体复现代码

@hbalxzdl
Copy link
Author

// Custom.ts
import { Leafer, Rect, RectData, registerUI, dataProcessor, PointerEvent } from 'leafer-ui'
import { IRectInputData, IRectData } from '@leafer-ui/interface'

export interface ICustomInputData extends IRectInputData { }

export interface ICustomData extends IRectData { }

export class CustomData extends RectData implements ICustomData { }

@registerui()
export class Custom extends Rect {

public get __tag() { return 'Custom' }

@dataprocessor(CustomData)
declare public __: ICustomData

// 1. 添加普通属性,不用进json,只是辅助逻辑判断 //
public rotating: boolean

constructor(data: ICustomInputData) {
super(data)
// ...
}

// 2. 添加自定义方法, 动画开关 //
startAnimate(): void {
this.rotating = true
this.rotateAnimate()
}

stopAnimate(): void {
this.rotating = false
}

rotateAnimate(): void {
this.nextRender(() => {
this.rotation += 1
if (this.rotating) this.rotateAnimate()
})
}

}

// worker.ts
import { Leafer } from '@leafer-ui/worker'
import '@leafer-in/export'
import './Custom.ts'

const leafer = new Leafer({ width: 800, height: 600 })

const start = (value) => {
leafer.add( { x: 0, y: 0, width: 800, height: 600, fill: '#fff', tag: 'Custom' })
leafer.export('jpg').then((result) => {
self.postMessage(result.data)
})
}

@leaferjs
Copy link
Owner

leaferjs commented Feb 20, 2025

worker中引入 import './Custom.ts' 导致引入了 leafer-ui包,需要把 leafer-ui替换成 '@leafer-ui/core'就能跨平台

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants