canvas-ui
canvas-ui copied to clipboard
canvas injection
Hello! 您好
This builds on my work in https://github.com/alibaba/canvas-ui/pull/32, I suggest you will want to look at the last commit to understand this PR. When that other PR is merged I will update this PR and remove the draft designation.
This commit enables passing any canvas element (HTMLCanvasElement or OffscreenCanvas) to RenderCanvas via constructor injection, following the established dependency injection pattern (frameScheduler, binding).
Changes:
- Add canvas parameter to RenderCanvas constructor and createElement factory
- Expand el property type to accept both HTMLCanvasElement and OffscreenCanvas
- Update CanvasSurface to accept both HTMLCanvasElement and OffscreenCanvas
- Rename createOrUpdateEl → ensureCanvasSize (reflects actual behavior)
- Guard cursor management with instanceof check for HTMLCanvasElement
- Make drawFrame public for manual rendering control
- Add JSDoc documentation for constructor and drawFrame
Benefits:
- Clean dependency injection pattern consistent with other constructor parameters
- Manual frame control via public drawFrame method
- Cursor management gracefully degrades for OffscreenCanvas
- Backwards compatible (canvas parameter is optional)