threejs-tips-screenshot-bad.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 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. function render(time) {
  87. time *= 0.001;
  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 = time * speed;
  96. cube.rotation.x = rot;
  97. cube.rotation.y = rot;
  98. });
  99. renderer.render(scene, camera);
  100. requestAnimationFrame(render);
  101. }
  102. requestAnimationFrame(render);
  103. const elem = document.querySelector('#screenshot');
  104. elem.addEventListener('click', () => {
  105. canvas.toBlob((blob) => {
  106. saveBlob(blob, `screencapture-${canvas.width}x${canvas.height}.png`);
  107. });
  108. });
  109. const saveBlob = (function() {
  110. const a = document.createElement('a');
  111. document.body.appendChild(a);
  112. a.style.display = 'none';
  113. return function saveData(blob, fileName) {
  114. const url = window.URL.createObjectURL(blob);
  115. a.href = url;
  116. a.download = fileName;
  117. a.click();
  118. };
  119. }());
  120. }
  121. main();
  122. </script>
  123. </html>