Drawing a shape using click, mouse move, and click
See original GitHub issueKonva
is a great canvas framework and react-konva
also nice. Thanks for the cool things!
I want to dynamic create shape on layers, and I tried add shapes into a list, then Layer
show the children, but it’s doesn’t draw the shape as I move mouse. Here is a reproduce demo.
The problem seems like can’t change the shape’s props after update it’s width and height with setState
, but don’t know why. Am I miss something or use wrong solution?
Thanks for reply.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Drawing a rectangle using click, mouse move, and click
I'm trying to draw a rectangle by a user click, mouse move, and click. There are two problems with my code. Firstly, after...
Read more >Draw and Edit Shapes with Mouse - VisPy
Simple demonstration of drawing and editing shapes with the mouse This demo implements mouse ... Objects can be selected by clicking, and moved...
Read more >Drawing a shape and moving it by mouse (using C#.Net)
Inside the other event handlers, if the mouse button is clicked down, then isMouseDown = true. If this condition is true, then make...
Read more >How to draw on an HTML Canvas with a Mouse
With the mousedown event, we set a boolean called drawing to true to signal that we started drawing. Then, inside the mousemove event,...
Read more >86. Drawing shapes with your mouse - Fun Programming
Learn programming 86: Drawing shapes with your mouse ... now able to move an object over the screen, and make it stick on...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I realize this isn’t a bug/issue but just to follow up - @navono Instead of creating React elements in your
handleClick
function, try only setting the state there, and lettingrender
handle the rest. React is declarative, meaning that you describe your app instate
, andrender
automatically reflects any changes you make tostate
.If you represent your shapes as an array of objects (each having
{ x, y, width, height }
), and then display that array inrender
, any time you change values in the array, the shapes will change on the screen. The dimension values will beprops
for aColoredRect
, soColoredRect
will not need to manage its own dimensions - it can simply render theprops
that it receives.I made a demo here: https://codesandbox.io/s/wwpyr3j737
@physicsai Very true. Using
to()
could definitely work but depending on the situation, it may not be desirable because the state of your application would not be in sync with what is being drawn on the canvas. To keep the same declarative approach without ruining the performance, we could optimize theColoredRect
component usingshouldComponentUpdate()
or, similar to your suggestion, we could separate the shape that is currently being drawn from the rest so that only that one is re-rendered on drag. Then once it is completed, we add it to the list of static shapes.