Help with Javascript canvas

Hello guys, I just coded a simple application where users can drag and move two circles inside the Canvas. I used Javascript to implement this. So I I’m just wondering whether someone can help me how to drag the circles without going past the edge of the canvas. The link of the code is provided below. THank you :slight_smile:

https://jsfiddle.net/rafael_dizon/4apa5sxm/3/

I tried inserting this code within the handleMouseMove function to check if the circles stop before it reaches the edge. if(mouseX > 556) { .
clearDragging();
}
But I couldn’t drag it back when it reaches the right edge.

You could modify your drag values if the circle position is outside the limits of canvas.

Extra Hint

Let’s say if the x position of the circle you’re about to draw is higher than width of canvas, set it to width of canvas, if lower than 0, set it to 0.
If you want the whole circle inside the canvas, you might have to use its radius value.

Partial solution

https://jsfiddle.net/4apa5sxm/55/

2 Likes

Thank you so much. I was also able to do it with the Y-axis.