threejs-shadertoy-prep.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 - Shadertoy Prep</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <canvas id="c"></canvas>
  21. </body>
  22. <script src="resources/threejs/r103/three.min.js"></script>
  23. <script>
  24. 'use strict';
  25. /* global THREE */
  26. function main() {
  27. const canvas = document.querySelector('#c');
  28. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  29. renderer.autoClearColor = false;
  30. const camera = new THREE.OrthographicCamera(
  31. -1, // left
  32. 1, // right
  33. 1, // top
  34. -1, // bottom
  35. -1, // near,
  36. 1, // far
  37. );
  38. const scene = new THREE.Scene();
  39. const plane = new THREE.PlaneBufferGeometry(2, 2);
  40. const material = new THREE.MeshBasicMaterial({
  41. color: 'red',
  42. });
  43. scene.add(new THREE.Mesh(plane, material));
  44. function resizeRendererToDisplaySize(renderer) {
  45. const canvas = renderer.domElement;
  46. const width = canvas.clientWidth;
  47. const height = canvas.clientHeight;
  48. const needResize = canvas.width !== width || canvas.height !== height;
  49. if (needResize) {
  50. renderer.setSize(width, height, false);
  51. }
  52. return needResize;
  53. }
  54. function render() {
  55. resizeRendererToDisplaySize(renderer);
  56. renderer.render(scene, camera);
  57. requestAnimationFrame(render);
  58. }
  59. requestAnimationFrame(render);
  60. }
  61. main();
  62. </script>
  63. </html>