-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
fix(Object): support specyfing toCanvasElement canvas #9652
Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
src/shapes/Object/Object.ts
Outdated
toCanvasElement(options: any = {}) { | ||
toCanvasElement( | ||
options: any = {}, | ||
createToCanvas = (el: HTMLCanvasElement) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we rename the arg?
maybe to createCanvas
? I don't know.
I dislike the way this method is impl.
A lot of mess, especially the fact that we are mutating the ref itself instead of cloning and returning a promise.
Maybe that is a better impl.
- clone
- send to canvas plane so that tl is positioned at 0,0
- create canvas
- render
- return both canvas and clone
Thoughts?
For now this is a good enough patch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also the setCoords calls seem to me quite redundant unless the object uses them for the exported image
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and if that is the case I would argue the dev should subclass and call setCoords themselves
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setCords is there to avoid offScreen check issues.
i THINK it can be removed by specifying for the new canvas that it should not use the offscreen check.
I do not think there is any other reason
But let's do it in a different PR
src/shapes/Object/Object.ts
Outdated
toCanvasElement(options: any = {}) { | ||
toCanvasElement( | ||
options: any = {}, | ||
createToCanvas = (el: HTMLCanvasElement) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason for this function to not be another option?
Also as pointed out the name of the function could be improved, some suggestions
- canvasCreator
- canvasProvider
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good points, done
I understand the point of the pr, but i m still curious about the actual hiccup you found. I would argue that any subclass should be safe to render on a static canvas, maybe it won't give the exact same pixels but should be at least safe. So what is the real issue you found? |
The use case was implementing the drag preview of an objectfor drag-and-drop, thus using
True, but that static canvas would be some custom class anyway, not |
And what did you find not working when doing so? What happened? |
The dragged object was using I don't think objects should ensure they can be rendered correctly on a StaticCanvas, it's not something you think of when developing an object. The DnD logic should be responsible of correctly generating a drag preview for any existing object, without having the latter adapt to |
Sorry had full days a work forgot about this |
Independently from this PR that is ok, Objects have to safely render on the staticCanvas, without crashing. |
the reason why this would not work 'new this.canvas.constructor(el)` is because an object doesn't have to have a canvas to be exported, or there is more i don't understand? |
It's because a derived Canvas class can take a different set of parameters. It just needs to do |
Couldn't we have used |
better as is |
It's fundamentally wrong to assume
StaticCanvas
is enough for rendering any generic object to a new canvastoCanvasElement
. The object typically assumes the canvas reference is the usual canvas, e.g.fabric.Canvas
, so it's unsafe to replace it with a StaticCanvas. I could even just do this in the object:This is just a quick fix so that one can use a different canvas if needed. It's difficult for fabric to know exactly what canvas class should be used. Something like
new this.canvas.constructor(el)
would not work.