konvajs / konva

Konva.js is an HTML5 Canvas JavaScript framework that extends the 2d context by enabling canvas interactivity for desktop and mobile applications.
http://konvajs.org/
Other
11.43k stars 918 forks source link

Cannot override shape.rotation() in on transform event #1724

Closed VanquishedWombat closed 7 months ago

VanquishedWombat commented 7 months ago

Version: Konva 9+

Summary: We can use the shape.onTransform method like a dragBoundFunc to limit where the shape may be placed etc. However, we cannot override the shape.rotation().

Cause: In Transformer.ts in method _fitNodesInto(), the loop that invokes the shape.onTransform event on the nodes in the transformer.nodes list runs before the setting of the transformer rotation. Which means that we can never override the transformer rotation with code in the shape.onTransform event.

Workaround: Only one I found is to place the code altering the rotation into a setTimeout(fn, 0). My assumption is that this is added to the end of the execution queue with the result that it runs after the _fitNodesInto() code is completed. However this is a poor workaround and may not give a pleasing UX result if a processor has a heavy workload or is overloaded.

Solution: In _fitNodesInto() reposition the setting of the transformer rotation above the transformer.nodes[] onTransform loop.

Demo: See https://codepen.io/JEE42/pen/abMMevp

Example image: 2024-02-20_10-22-14