tips-screenshot-bad.html 3.4 KB

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