1
0

moon-orbit.html 3.4 KB

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