question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Drawing a shape using click, mouse move, and click

See original GitHub issue

Konva 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:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ejarzocommented, Jan 26, 2018

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 letting render handle the rest. React is declarative, meaning that you describe your app in state, and render automatically reflects any changes you make to state.

If you represent your shapes as an array of objects (each having { x, y, width, height }), and then display that array in render, any time you change values in the array, the shapes will change on the screen. The dimension values will be props for a ColoredRect, so ColoredRect will not need to manage its own dimensions - it can simply render the props that it receives.

I made a demo here: https://codesandbox.io/s/wwpyr3j737

1reaction
ejarzocommented, Apr 11, 2019

@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 the ColoredRect component using shouldComponentUpdate() 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found