css3d_youtube.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>three.js css3d - youtube</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #ffffff;
  11. }
  12. #blocker {
  13. position: absolute;
  14. top: 0;
  15. left: 0;
  16. bottom: 0;
  17. right: 0;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="container"></div>
  23. <div id="blocker"></div>
  24. <script type="module">
  25. import * as THREE from '../build/three.module.js';
  26. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  27. import { CSS3DRenderer, CSS3DObject } from './jsm/renderers/CSS3DRenderer.js';
  28. var camera, scene, renderer;
  29. var controls;
  30. var Element = function ( id, x, y, z, ry ) {
  31. var div = document.createElement( 'div' );
  32. div.style.width = '480px';
  33. div.style.height = '360px';
  34. div.style.backgroundColor = '#000';
  35. var iframe = document.createElement( 'iframe' );
  36. iframe.style.width = '480px';
  37. iframe.style.height = '360px';
  38. iframe.style.border = '0px';
  39. iframe.src = [ 'https://www.youtube.com/embed/', id, '?rel=0' ].join( '' );
  40. div.appendChild( iframe );
  41. var object = new CSS3DObject( div );
  42. object.position.set( x, y, z );
  43. object.rotation.y = ry;
  44. return object;
  45. };
  46. init();
  47. animate();
  48. function init() {
  49. var container = document.getElementById( 'container' );
  50. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 5000 );
  51. camera.position.set( 500, 350, 750 );
  52. scene = new THREE.Scene();
  53. renderer = new CSS3DRenderer();
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. container.appendChild( renderer.domElement );
  56. var group = new THREE.Group();
  57. group.add( new Element( 'SJOz3qjfQXU', 0, 0, 240, 0 ) );
  58. group.add( new Element( 'Y2-xZ-1HE-Q', 240, 0, 0, Math.PI / 2 ) );
  59. group.add( new Element( 'IrydklNpcFI', 0, 0, - 240, Math.PI ) );
  60. group.add( new Element( '9ubytEsCaS0', - 240, 0, 0, - Math.PI / 2 ) );
  61. scene.add( group );
  62. controls = new TrackballControls( camera, renderer.domElement );
  63. controls.rotateSpeed = 4;
  64. window.addEventListener( 'resize', onWindowResize, false );
  65. // Block iframe events when dragging camera
  66. var blocker = document.getElementById( 'blocker' );
  67. blocker.style.display = 'none';
  68. controls.addEventListener( 'start', function () {
  69. blocker.style.display = '';
  70. } );
  71. controls.addEventListener( 'end', function () {
  72. blocker.style.display = 'none';
  73. } );
  74. }
  75. function onWindowResize() {
  76. camera.aspect = window.innerWidth / window.innerHeight;
  77. camera.updateProjectionMatrix();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. }
  80. function animate() {
  81. requestAnimationFrame( animate );
  82. controls.update();
  83. renderer.render( scene, camera );
  84. }
  85. </script>
  86. </body>
  87. </html>