threejs-tips-screenshot-good.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 (good)</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/r119/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. const state = {
  85. time: 0,
  86. };
  87. function render() {
  88. if (resizeRendererToDisplaySize(renderer)) {
  89. const canvas = renderer.domElement;
  90. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  91. camera.updateProjectionMatrix();
  92. }
  93. cubes.forEach((cube, ndx) => {
  94. const speed = 1 + ndx * .1;
  95. const rot = state.time * speed;
  96. cube.rotation.x = rot;
  97. cube.rotation.y = rot;
  98. });
  99. renderer.render(scene, camera);
  100. }
  101. function animate(time) {
  102. state.time = time * 0.001;
  103. render();
  104. requestAnimationFrame(animate);
  105. }
  106. requestAnimationFrame(animate);
  107. const elem = document.querySelector('#screenshot');
  108. elem.addEventListener('click', () => {
  109. render();
  110. canvas.toBlob((blob) => {
  111. saveBlob(blob, `screencapture-${canvas.width}x${canvas.height}.png`);
  112. });
  113. });
  114. const saveBlob = (function() {
  115. const a = document.createElement('a');
  116. document.body.appendChild(a);
  117. a.style.display = 'none';
  118. return function saveData(blob, fileName) {
  119. const url = window.URL.createObjectURL(blob);
  120. a.href = url;
  121. a.download = fileName;
  122. a.click();
  123. };
  124. }());
  125. }
  126. main();
  127. </script>
  128. </html>