threejs-tips-screenshot-good.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 src="resources/threejs/r105/three.min.js"></script>
  40. <script>
  41. 'use strict';
  42. /* global THREE */
  43. function main() {
  44. const canvas = document.querySelector('#c');
  45. const renderer = new THREE.WebGLRenderer({canvas});
  46. const fov = 75;
  47. const aspect = 2; // the canvas default
  48. const near = 0.1;
  49. const far = 5;
  50. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  51. camera.position.z = 2;
  52. const scene = new THREE.Scene();
  53. {
  54. const color = 0xFFFFFF;
  55. const intensity = 1;
  56. const light = new THREE.DirectionalLight(color, intensity);
  57. light.position.set(-1, 2, 4);
  58. scene.add(light);
  59. }
  60. const boxWidth = 1;
  61. const boxHeight = 1;
  62. const boxDepth = 1;
  63. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  64. function makeInstance(geometry, color, x) {
  65. const material = new THREE.MeshPhongMaterial({color});
  66. const cube = new THREE.Mesh(geometry, material);
  67. scene.add(cube);
  68. cube.position.x = x;
  69. return cube;
  70. }
  71. const cubes = [
  72. makeInstance(geometry, 0x44aa88, 0),
  73. makeInstance(geometry, 0x8844aa, -2),
  74. makeInstance(geometry, 0xaa8844, 2),
  75. ];
  76. function resizeRendererToDisplaySize(renderer) {
  77. const canvas = renderer.domElement;
  78. const width = canvas.clientWidth;
  79. const height = canvas.clientHeight;
  80. const needResize = canvas.width !== width || canvas.height !== height;
  81. if (needResize) {
  82. renderer.setSize(width, height, false);
  83. }
  84. return needResize;
  85. }
  86. const state = {
  87. time: 0,
  88. };
  89. function render() {
  90. if (resizeRendererToDisplaySize(renderer)) {
  91. const canvas = renderer.domElement;
  92. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  93. camera.updateProjectionMatrix();
  94. }
  95. cubes.forEach((cube, ndx) => {
  96. const speed = 1 + ndx * .1;
  97. const rot = state.time * speed;
  98. cube.rotation.x = rot;
  99. cube.rotation.y = rot;
  100. });
  101. renderer.render(scene, camera);
  102. }
  103. function animate(time) {
  104. state.time = time * 0.001;
  105. render();
  106. requestAnimationFrame(animate);
  107. }
  108. requestAnimationFrame(animate);
  109. const elem = document.querySelector('#screenshot');
  110. elem.addEventListener('click', () => {
  111. render();
  112. canvas.toBlob((blob) => {
  113. saveBlob(blob, `screencapture-${canvas.width}x${canvas.height}.png`);
  114. });
  115. });
  116. const saveBlob = (function() {
  117. const a = document.createElement('a');
  118. document.body.appendChild(a);
  119. a.style.display = 'none';
  120. return function saveData(blob, fileName) {
  121. const url = window.URL.createObjectURL(blob);
  122. a.href = url;
  123. a.download = fileName;
  124. a.click();
  125. };
  126. }());
  127. }
  128. main();
  129. </script>
  130. </html>