tips-screenshot-bad.html 3.6 KB

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