Good day! Need help in the implementation of the movement of the object in a circle ... Would you advise how this can be done? Very desirable detail)

  • Thanks to the author) + 10000k to Karma) - user181955

1 answer 1

Threw a sample, do not kick for global variables, used to simplify the example as much as possible.

var context; // пСрСмСнная для хранСния контСкста var container; // пСрСмСнная для хранСния canvas элСмСнта var currentAngle = 0; // Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΡƒΠ³Π»Π° var radius = 100; // радиус окруТности var baseX = 200; // x ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° Ρ†Π΅Π½Ρ‚Ρ€Π° окруТности var baseY = 200; // y ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° Ρ†Π΅Π½Ρ‚Ρ€Π° окруТности window.onload = function() { container = document.createElement("canvas"); // создаСм canvas элСмСнт container.width = 400; // устанваливаСм ΡˆΠΈΡ€ΠΈΠ½Ρƒ container.height = 400; // устанавливаСм высоту document.body.appendChild(container); // добавляСм canvas Π² DOM context = container.getContext('2d'); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ конткСст context.fillStyle = '#000'; // устанавливаСм Ρ†Π²Π΅Ρ‚ Π·Π°Π»ΠΈΠ²ΠΊΠΈ Π² Ρ‡Π΅Ρ€Π½Ρ‹ΠΉ context.fillRect(0, 0, container.width, container.height); // собствСнно Π·Π°Π»ΠΈΠ²Π°Π΅ΠΌ всС Ρ‡Π΅Ρ€Π½Ρ‹ΠΌ setInterval(function() { // считаСм косинус Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ значСния ΡƒΠ³Π»Π° // ΠΈ ΡƒΠΌΠ½ΠΎΠΆΠ°Π΅ΠΌ Π½Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ радиуса var vx = Math.cos(currentAngle) * radius; // считаСм синус Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ значСния ΡƒΠ³Π»Π° // ΠΈ ΡƒΠΌΠ½ΠΎΠΆΠ°Π΅ΠΌ Π½Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ радиуса var vy = Math.sin(currentAngle) * radius; context.fillStyle = '#fff'; // устанавливаСм Ρ†Π²Π΅Ρ‚ Π·Π°Π»ΠΈΠ²ΠΊΠΈ Π² Π±Π΅Π»Ρ‹ΠΉ context.fillRect(baseX + vx, baseY + vy, 5, 5); // считСм Π½ΠΎΠ²ΡƒΡŽ ΠΏΠΎΠ·ΠΈΡ†ΠΈΡŽ ΠΏΠΎ x ΠΈ y ΠΎΡ‚Π½ΠΎΡΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ Ρ†Π΅Π½Ρ‚Ρ€Π° окруТности // ΠΈ Π·Π°Π»ΠΈΠ²Π°Π΅ΠΌ Π΅Π΅ ΠΊΠ²Π°Π΄Ρ€Π°Ρ‚ΠΈΠΊΠΎΠΌ высотой ΠΈ ΡˆΠΈΡ€ΠΈΠ½ΠΎΠΉ Π² 5 пиксСлСй // ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΡƒΠ³Π»Π° currentAngle += 0.1; }, 5) } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Π”Π²ΠΈΠΆΠ΅Π½ΠΈΠ΅ ΠΏΠΎ ΠΊΡ€ΡƒΠ³Ρƒ</title> </head> <body></body> </html> 

If there are any questions, they say - why a circle is drawn, why we consider angles exactly like that and the like - google help or textbook on trigonometry :)

PS: It looks quite interesting, so I advise you to run it all :)


UPD:
In order for the square to move, but the circle was not drawn, you need to change the function in the interval as follows:

  setInterval(function(){ context.fillStyle = '#000'; context.fillRect(0, 0, container.width, container.height); // Ρ‚.Π΅. Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΌ "Ρ‚ΠΈΠΊΠ΅ ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π»Π°" Π½Π°Π΄ΠΎ снова всС Π·Π°Π»ΠΈΠ²Π°Ρ‚ΡŒ Ρ‡Π΅Ρ€Π½Ρ‹ΠΌ var vx = Math.cos(currentAngle)*radius; var vy = Math.sin(currentAngle)*radius; context.fillStyle = '#fff'; context.fillRect(baseX+vx, baseY+vy, 5, 5); currentAngle+=0.1; }, 5) 

Well, to change the direction of the square you need to change the sign of the sine or cosine.

To change the starting position of a square, you must change the starting value of the angle currentAngle .

  • Thank you so much) The textbook board helped most) - MrFranke
  • Please, in own time leafed through: DDD - Zowie