Skip to content
Advertisement

Repeated drawings of bounding box

I would like to simply make a bounding box that uses points(any amount) to follow my mouse around.

Error being when I draw the bounding box currently it repeats all the previous drawings and have repeated bounding boxes enter image description here

JavaScript

Advertisement

Answer

You had already the right idea creating a clone from the original image, but just forgot to set the clone back to the original image before painting the new bounding-box upon it. Adding one line at the position as below solves the problem:

JavaScript

The code you have provided has along with the issue described in your question and solved by above described change also some other issues you haven’t addressed in your question. The most annoying one is that it unnecessary heats up the CPU because it updates the screen in both, the keyboard and the mouse events loops with highest possible rate for mouse events and a very high rate for keyboard events. This causes heating up the CPU even in case the mouse stands still.

The code provided below does not heat up the CPU. It uses the time module for own time queries and sets the frame rate to 25 frames per second for mouse events. The keyboard input loop runs at a rate of 4 queries per second and raises the window again if closed by Alt-F4 or clicking [x]. The only way to quit the program is therefore pressing [q] on the keyboard (this is the same behavior as in the code you have posted).

To make things simpler the code below flattens the depth of function calls by getting rid of the class handling the bounding box widget using global variables in the mouse handler function, the only function in the code. See comments, changed variable names (in order to better mirror their actual purpose) and the way function parameter are set in the code for further explanations.

Comprehend the behavior of both the mouse and keyboard loops checking out what the code prints to stdout:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement