dir.by  
Поиск >>

Компьютер, программы
→
Three.js - JavaScript library for drawing graphics 3D in the pages HTML
 Example "Rotating cube with texture (rotate cube)" library Three.js 
посмотрели 9847 раз
обновлено: 14 October 2020
Source code
Example we look, we test
  Html  
<!DOCTYPE html>
<html lang="en">

     <head>
          <title>three.js webgl - geometry - cube</title>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
          <style>
               body {
                    margin: 0px;
                    background-color: #000000;
                    overflow: hidden;
               }
          </style>
     </head>

     <body>

          <!-- connect the library three.js -->
          <script src="https://dir.by/example_lib/tree_js/three.js"></script>

          <script>

               var camera, scene, renderer;
               var mesh;

               init();
               animate();

               function init()
               {
                    camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
                    camera.position.z = 400;

                    scene = new THREE.Scene();

                    var texture = new THREE.TextureLoader().load( 'https://dir.by/developer/three_js/example_cube/textures/crate.gif' );

                    var geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
                    var material = new THREE.MeshBasicMaterial( { map: texture } );

                    mesh = new THREE.Mesh( geometry, material );
                    scene.add( mesh );

                    renderer = new THREE.WebGLRenderer( { antialias: true } );
                    renderer.setPixelRatio( window.devicePixelRatio );
                    renderer.setSize( window.innerWidth, window.innerHeight );
                    document.body.appendChild( renderer.domElement );

                    window.addEventListener( 'resize', onWindowResize, false );
               }

               function onWindowResize()
               {
                    camera.aspect = window.innerWidth / window.innerHeight;
                    camera.updateProjectionMatrix();

                    renderer.setSize( window.innerWidth, window.innerHeight );
               }

               function animate()
               {
                    requestAnimationFrame( animate );

                    mesh.rotation.x += 0.005;
                    mesh.rotation.y += 0.01;

                    renderer.render( scene, camera );
               }
          </script>

     </body>
</html>
Note!
Example does not work locally, because the image upload error crate.gif.

Access to Image at 'https://dir.by/developer/three_js/example_cube/textures/crate.gif' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
 
Error: Access to url at 'http://...' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
Решение!
На рабочем столе для иконки Google Chrome добавим парметры: --user-data-dir="C://Chrome dev session" --disable-web-security
Запустим Google Chrome нажав на иконку.
Введем url, ошибки нет.
 
← Previous topic
Example "Roller coaster (roller coaster)" library Three.js
 
Next topic →
Example "We spin the picture ourselves svg (svg image)" library Three.js
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

 
• What the Three.js and how to download?
• Example "Flying cubes" library Three.js
• Example "Roller coaster (roller coaster)" library Three.js
• Example "Rotating cube with texture (rotate cube)" library Three.js
• Example "We spin the picture ourselves svg (svg image)" library Three.js
• Example "The moon revolves around the earth (moon rotate around earth)" library Three.js
• Example "Drawing a picture with 3D cubes (interactive voxel painter)" library Three.js
• Example "Reflection balls (effects anaglyph)" library Three.js
• Create your own file .js The file contains an object with export variables and functions

  Ваши вопросы присылайте по почте: info@dir.by