Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Canvas drawing issue

cbreemer

Bronze Coder
I am having fun with drawing stuff on a Canvas using Graphics2D. Basically I want to mimic a Form Editor like that of Visual Studio, where you can paint a component on the screen by drawing rectangle with the mouse. Essentially, the rectangle should grow or shrink as long as the mouse is being moved, only to be fixed when the mouse button is released. So with every mouse movement, the previously drawn rectangle should be wiped. To achieve this, before drawing the new rectangle, I restore a snapshot of the canvas that was taken when the mouse button went down.
This works well enough, the flickering is understandable but not too disturbing when you keep some continuous movement in the mouse. But what I fail to understand is why the rectangle disappears as soon as I stop the mouse movement for more than a split second. Ok, all is well once I let go of the mouse button, but this annoying.
Phew, not easy describing this issue. Best provide some code. Instead of posting the code here I decided to be modern for a change and put it on Codepen : https://codepen.io/cbreemer/pen/mdgOQdQ
Just draw a rectangle on the blank canvas and you'll see what I mean. I wonder is I'm missing something obvious here.
 
Back
Top Bottom