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
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
|