cameras-orthographic-canvas-top-left-origin.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Cameras - Orthographic Canvas Top Left Origin</title>
  8. <style>
  9. html, body {
  10. margin: 0;
  11. height: 100%;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. .split {
  19. position: absolute;
  20. left: 0;
  21. top: 0;
  22. width: 100%;
  23. height: 100%;
  24. display: flex;
  25. }
  26. .split>div {
  27. width: 100%;
  28. height: 100%;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <canvas id="c"></canvas>
  34. </body>
  35. <!-- Import maps polyfill -->
  36. <!-- Remove this when import maps will be widely supported -->
  37. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  38. <script type="importmap">
  39. {
  40. "imports": {
  41. "three": "../../build/three.module.js"
  42. }
  43. }
  44. </script>
  45. <script type="module">
  46. import * as THREE from 'three';
  47. function main() {
  48. const canvas = document.querySelector('#c');
  49. const renderer = new THREE.WebGLRenderer({canvas});
  50. const left = 0;
  51. const right = 300; // default canvas size
  52. const top = 0;
  53. const bottom = 150; // defautl canvas size
  54. const near = -1;
  55. const far = 1;
  56. const camera = new THREE.OrthographicCamera(left, right, top, bottom, near, far);
  57. camera.zoom = 1;
  58. const scene = new THREE.Scene();
  59. scene.background = new THREE.Color('black');
  60. const loader = new THREE.TextureLoader();
  61. const textures = [
  62. loader.load('resources/images/flower-1.jpg'),
  63. loader.load('resources/images/flower-2.jpg'),
  64. loader.load('resources/images/flower-3.jpg'),
  65. loader.load('resources/images/flower-4.jpg'),
  66. loader.load('resources/images/flower-5.jpg'),
  67. loader.load('resources/images/flower-6.jpg'),
  68. ];
  69. const planeSize = 256;
  70. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  71. const planes = textures.map((texture) => {
  72. const planePivot = new THREE.Object3D();
  73. scene.add(planePivot);
  74. texture.magFilter = THREE.NearestFilter;
  75. const planeMat = new THREE.MeshBasicMaterial({
  76. map: texture,
  77. side: THREE.DoubleSide,
  78. });
  79. const mesh = new THREE.Mesh(planeGeo, planeMat);
  80. planePivot.add(mesh);
  81. // move plane so top left corner is origin
  82. mesh.position.set(planeSize / 2, planeSize / 2, 0);
  83. return planePivot;
  84. });
  85. function resizeRendererToDisplaySize(renderer) {
  86. const canvas = renderer.domElement;
  87. const width = canvas.clientWidth;
  88. const height = canvas.clientHeight;
  89. const needResize = canvas.width !== width || canvas.height !== height;
  90. if (needResize) {
  91. renderer.setSize(width, height, false);
  92. }
  93. return needResize;
  94. }
  95. function render(time) {
  96. time *= 0.001; // convert to seconds;
  97. if (resizeRendererToDisplaySize(renderer)) {
  98. camera.right = canvas.width;
  99. camera.bottom = canvas.height;
  100. camera.updateProjectionMatrix();
  101. }
  102. const distAcross = Math.max(20, canvas.width - planeSize);
  103. const distDown = Math.max(20, canvas.height - planeSize);
  104. // total distance to move across and back
  105. const xRange = distAcross * 2;
  106. const yRange = distDown * 2;
  107. const speed = 180;
  108. planes.forEach((plane, ndx) => {
  109. // compute a unique time for each plane
  110. const t = time * speed + ndx * 300;
  111. // get a value between 0 and range
  112. const xt = t % xRange;
  113. const yt = t % yRange;
  114. // set our position going forward if 0 to half of range
  115. // and backward if half of range to range
  116. const x = xt < distAcross ? xt : xRange - xt;
  117. const y = yt < distDown ? yt : yRange - yt;
  118. plane.position.set(x, y, 0);
  119. });
  120. renderer.render(scene, camera);
  121. requestAnimationFrame(render);
  122. }
  123. requestAnimationFrame(render);
  124. }
  125. main();
  126. </script>
  127. </html>