sin是这个角的对边和斜边的比。
cos一是这个角挨着的那条边和斜边的比;tan是这个角的对边和邻边的比。
在平面直角坐标系xOy中设∠β的始边为x轴的正半轴,设点P(x,y)为∠β的终边上不与原点O重合的任意一点,设r=OP,令∠β=∠α,则:sin a=y/r;cos a=x/r。

2 π == 360度
const a = Math.sin(num * π / 180) == BC / AC
const b = Math.cos(num * π / 180 ) = AB/ AC
公式原理

js实现圆周运动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> * { margin: 0; padding: 0; } .circle { position: absolute; left: 500px; top: 500px; border: 2px solid red; border-radius: 50%; margin-left: -150px; margin-top: -150px; width: 300px; height: 300px; } .rect { position: absolute; width: 10px; height: 10px; background-color: blue; top: 350px; left: 500px; } .red{ background: red; } .blue{ background-color: blue; } .orange{ background-color: orange; } .black{ background-color: black; } .pink{ background-color: pink; } </style> </head> <body> <div class="circle"></div> <div class=" rect rect1 red"></div> <div class="rect rect2 blue"></div> <div class="rect rect3 orange"></div> <div class="rect rect4 black"></div> <div class="rect rect5 pink"></div> </body> </html> <script> (function () { /* 普通使用 var num = 0; var circle = document.querySelector(".circle"); var middleX = circle.offsetLeft + circle.offsetWidth / 2; var middleY = circle.offsetTop + circle.offsetHeight / 2;
function ani() { if (num === 360) { num = 0; } else { num++; }
var line1Y = Math.sin((num * Math.PI) / 180) * 150; var line2X = Math.cos((num * Math.PI) / 180) * 150; console.log(line1Y, line2X); document.querySelector(".rect").style.cssText = `;left:${ line2X + middleX }px;top:${line1Y + middleY}px`; } ani(); setInterval(ani, 100); */ class AniClass { constructor() { this.num = 0; this.circle = document.querySelector(".circle"); this.middleX = this.circle.offsetLeft + this.circle.offsetWidth / 2; this.middleY = this.circle.offsetTop + this.circle.offsetHeight / 2; } ani(className, delay) { this.move(className); setTimeout(()=>{ setInterval(this.move.bind(this,className), 50); },delay) }
move(className){ if (this.num === 360) { this.num = 0; } else { this.num++; }
var line1Y = Math.sin((this.num * Math.PI) / 180) * 150; var line2X = Math.cos((this.num * Math.PI) / 180) * 150; document.querySelector(className).style.cssText = `;left:${ line2X + this.middleX }px;top:${line1Y + this.middleY}px`; } }
[1,2,3,4,5].forEach(item=>{ const Ani = new AniClass(); Ani.ani('.rect'+item,4000*item); }); })(); </script>
|