moon-orbit.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <html>
  2. <head>
  3. <meta charset="utf8">
  4. <title>moon orbit</title>
  5. <link type="text/css" href="../../resources/threejs-tutorials.css" rel="stylesheet" />
  6. <style>
  7. body {
  8. margin: 0px;
  9. background: white;
  10. }
  11. canvas {
  12. width: 100vw;
  13. height: 100vh;
  14. display: block;
  15. }
  16. p {
  17. position: relative;
  18. }
  19. #c {
  20. position: absolute;
  21. left: 0px;
  22. top: 0px;
  23. z-index: 2;
  24. background-color: transparent;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <p><canvas id="orbit"></canvas>
  30. <canvas id="c"></canvas></p>
  31. </body>
  32. <script src="../../resources/threejs-lessons-helper.js"></script>
  33. <script src="../../resources/threejs-utils.js"></script>
  34. <script src="canvas-wrapper.js"></script>
  35. <script>
  36. 'use strict';
  37. /* global wrapCanvasRenderingContext2D, threejsUtils */
  38. function main() {
  39. const root = {
  40. name: 'sun',
  41. translation: [0, 0],
  42. color: 'yellow',
  43. radius: 30,
  44. speed: 1,
  45. children: [
  46. {
  47. name: 'earth',
  48. translation: [-5, 1],
  49. color: 'blue',
  50. radius: 10,
  51. speed: 2,
  52. children: [
  53. {
  54. name: 'moon',
  55. translation: [-1, 1],
  56. color: 'gray',
  57. drawOrbit: true,
  58. radius: 5,
  59. speed: 36.13,
  60. children: [
  61. ],
  62. },
  63. ],
  64. },
  65. ],
  66. };
  67. const canvas = document.getElementById('c');
  68. const ctx = wrapCanvasRenderingContext2D(canvas.getContext('2d'));
  69. const orbitCtx = document.getElementById('orbit').getContext('2d');
  70. const spread = 16;
  71. function updateTranslation(node) {
  72. node.translation[0] *= spread;
  73. node.translation[1] *= spread;
  74. node.rotation = 0;
  75. node.children.forEach(updateTranslation);
  76. }
  77. updateTranslation(root);
  78. let clock = 0;
  79. const maxHistory = 400;
  80. let curHistory = 0;
  81. const history = [];
  82. function drawTrail(ctx, pos, radius) {
  83. ctx.beginPath();
  84. ctx.arc(pos[0], pos[1], radius, 0, Math.PI * 2, false);
  85. ctx.fill();
  86. }
  87. function drawNode(node) {
  88. ctx.save();
  89. ctx.rotate(node.speed * clock);
  90. ctx.translate(node.translation[0], node.translation[1]);
  91. ctx.fillStyle = node.color;
  92. ctx.strokeStyle = 'black';
  93. ctx.beginPath();
  94. ctx.arc(0, 0, node.radius, 0, Math.PI * 2, false);
  95. ctx.fill();
  96. ctx.stroke();
  97. if (node.drawOrbit) {
  98. const mat = ctx.currentTransform;
  99. const point = [mat.e, mat.f];
  100. const old = history[curHistory];
  101. if (old) {
  102. orbitCtx.fillStyle = 'white';
  103. drawTrail(orbitCtx, old, 2);
  104. }
  105. history[curHistory] = point;
  106. curHistory = (curHistory + 1) % maxHistory;
  107. orbitCtx.fillStyle = 'rgba(255, 0, 0, 0.5)';
  108. drawTrail(orbitCtx, point, 1);
  109. }
  110. node.children.forEach(drawNode);
  111. ctx.restore();
  112. }
  113. function drawScene() {
  114. threejsUtils.resizeCanvasToDisplaySize(ctx.canvas);
  115. threejsUtils.resizeCanvasToDisplaySize(orbitCtx.canvas);
  116. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  117. ctx.save();
  118. ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2);
  119. drawNode(root);
  120. ctx.restore();
  121. }
  122. function render() {
  123. clock += 1 / 60 * 0.25;
  124. drawScene();
  125. requestAnimationFrame(render, canvas);
  126. }
  127. requestAnimationFrame(render, canvas);
  128. }
  129. main();
  130. </script>
  131. </html>