2
0

threejs-tips-screenshot-bad.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 - Screenshot (bad)</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. #screenshot {
  18. position: absolute;
  19. left: 10px;
  20. top: 10px;
  21. padding: 10px;
  22. background: rgba(0,0,0,0.9);
  23. color: white;
  24. border: 1px solid gray;
  25. cursor: pointer;
  26. }
  27. #screenshot:hover {
  28. background: #444;
  29. }
  30. #screenshot:focus {
  31. outline: none;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <canvas id="c"></canvas>
  37. <button id="screenshot" type="button">Save...</button>
  38. </body>
  39. <script type="module">
  40. import * as THREE from './resources/threejs/r112/build/three.module.js';
  41. function main() {
  42. const canvas = document.querySelector('#c');
  43. const renderer = new THREE.WebGLRenderer({canvas});
  44. const fov = 75;
  45. const aspect = 2; // the canvas default
  46. const near = 0.1;
  47. const far = 5;
  48. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  49. camera.position.z = 2;
  50. const scene = new THREE.Scene();
  51. {
  52. const color = 0xFFFFFF;
  53. const intensity = 1;
  54. const light = new THREE.DirectionalLight(color, intensity);
  55. light.position.set(-1, 2, 4);
  56. scene.add(light);
  57. }
  58. const boxWidth = 1;
  59. const boxHeight = 1;
  60. const boxDepth = 1;
  61. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  62. function makeInstance(geometry, color, x) {
  63. const material = new THREE.MeshPhongMaterial({color});
  64. const cube = new THREE.Mesh(geometry, material);
  65. scene.add(cube);
  66. cube.position.x = x;
  67. return cube;
  68. }
  69. const cubes = [
  70. makeInstance(geometry, 0x44aa88, 0),
  71. makeInstance(geometry, 0x8844aa, -2),
  72. makeInstance(geometry, 0xaa8844, 2),
  73. ];
  74. function resizeRendererToDisplaySize(renderer) {
  75. const canvas = renderer.domElement;
  76. const width = canvas.clientWidth;
  77. const height = canvas.clientHeight;
  78. const needResize = canvas.width !== width || canvas.height !== height;
  79. if (needResize) {
  80. renderer.setSize(width, height, false);
  81. }
  82. return needResize;
  83. }
  84. function render(time) {
  85. time *= 0.001;
  86. if (resizeRendererToDisplaySize(renderer)) {
  87. const canvas = renderer.domElement;
  88. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  89. camera.updateProjectionMatrix();
  90. }
  91. cubes.forEach((cube, ndx) => {
  92. const speed = 1 + ndx * .1;
  93. const rot = time * speed;
  94. cube.rotation.x = rot;
  95. cube.rotation.y = rot;
  96. });
  97. renderer.render(scene, camera);
  98. requestAnimationFrame(render);
  99. }
  100. requestAnimationFrame(render);
  101. const elem = document.querySelector('#screenshot');
  102. elem.addEventListener('click', () => {
  103. canvas.toBlob((blob) => {
  104. saveBlob(blob, `screencapture-${canvas.width}x${canvas.height}.png`);
  105. });
  106. });
  107. const saveBlob = (function() {
  108. const a = document.createElement('a');
  109. document.body.appendChild(a);
  110. a.style.display = 'none';
  111. return function saveData(blob, fileName) {
  112. const url = window.URL.createObjectURL(blob);
  113. a.href = url;
  114. a.download = fileName;
  115. a.click();
  116. };
  117. }());
  118. }
  119. main();
  120. </script>
  121. </html>