How do i refactor the code so the rectangle can move without increasing??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do i refactor the code so the rectangle can move without increasing???

https://code.sololearn.com/WMr2aM5hLPSl/?ref=app

3rd Jul 2022, 1:53 PM
Patrick agbenyo paul
Patrick agbenyo paul - avatar
5 Answers
+ 1
You have to redraw the entire rectangle each time the user pushes the button.
3rd Jul 2022, 3:31 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Move your ctx.clearRect into your drawcircle function and change the parameters like this. ctx.clearRect(startX,startY, canvasWidth, canvasHeight); function drawcircle() { ctx.clearRect(0,0,200,200); ctx.beginPath(); ctx.arc(100, 140, 30, 56, Math.PI * 2, true); ctx.fillStyle = "red"; ctx.fill(); ctx.closePath(); }
3rd Jul 2022, 6:53 PM
Anthony Johnson
Anthony Johnson - avatar
+ 1
Thks Anthony Joshua it's working perfectly 👌👍 buh pls 🙏 i need some explanation why does the clearRect needed to be in draw circle fuction??
3rd Jul 2022, 7:12 PM
Patrick agbenyo paul
Patrick agbenyo paul - avatar
+ 1
Okay, the proper way to use clearRect is to call it before you redraw to your canvas. In an animation for example. The order would be. randomFunction = function() { Clear canvas Update (anything thats changing) Draw } Its not the best example but straight forward to the point. Every time the function is called it clears, updates, and redraws with the new parameters.
4th Jul 2022, 4:27 AM
Anthony Johnson
Anthony Johnson - avatar
0
How do I do that
3rd Jul 2022, 6:33 PM
Patrick agbenyo paul
Patrick agbenyo paul - avatar