html5 canvas實現夢幻的3D刺蝟球

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   2014   

今天要為大家帶來一款html5 canvas實現的夢幻的3D刺蝟球。頁面非常夢幻。如下:

 

線上預覽   源碼下載

 

html代碼:

<div>        <canvas width="1366" height="62" style="width: 1366px; height: 62px;">        </canvas></div>

js代碼:

 var SCREEN_WIDTH = window.innerWidth,                SCREEN_HEIGHT = window.innerHeight,            r = 200,            mouseX = 0, mouseY = 0,            windowHalfX = window.innerWidth / 2,            windowHalfY = window.innerHeight / 2,            camera, scene, renderer;        init();        animate();        function init() {            var container;            container = document.createElement(‘div‘);            document.body.appendChild(container);            camera = new THREE.PerspectiveCamera(80, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 3000);            camera.position.z = 1000;            scene = new THREE.Scene();            var i, line, vertex1, vertex2, material, p,                    parameters = [[3.5, 0x90CEBF, 0.5, 1]],                    geometry = new THREE.Geometry();            for (i = 0; i < 1500; i++) {                var vertex1 = new THREE.Vector3();                vertex1.x = Math.random() * 2 - 1;                vertex1.y = Math.random() * 2 - 1;                vertex1.z = Math.random() * 2 - 1;                vertex1.normalize();                vertex1.multiplyScalar(r);                vertex2 = vertex1.clone();                vertex2.multiplyScalar(Math.random() * 0.09 + 1);                geometry.vertices.push(vertex1);                geometry.vertices.push(vertex2);            }            for (i = 0; i < parameters.length; ++i) {                p = parameters[i];                material = new THREE.LineBasicMaterial({ color: p[1], opacity: p[2], linewidth: p[3] });                line = new THREE.Line(geometry, material, THREE.LinePieces);                line.scale.x = line.scale.y = line.scale.z = p[0];                line.originalScale = p[0];                line.rotation.y = Math.random() * Math.PI;                line.updateMatrix();                scene.add(line);            }            renderer = new THREE.WebGLRenderer({ antialias: true });            renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);            container.appendChild(renderer.domElement);            document.addEventListener(‘mousemove‘, onDocumentMouseMove, false);            document.addEventListener(‘touchstart‘, onDocumentTouchStart, false);            document.addEventListener(‘touchmove‘, onDocumentTouchMove, false);            //            window.addEventListener(‘resize‘, onWindowResize, false);        }        function onWindowResize() {            windowHalfX = window.innerWidth / 2;            windowHalfY = window.innerHeight / 2;            camera.aspect = window.innerWidth / window.innerHeight;            camera.updateProjectionMatrix();            renderer.setSize(window.innerWidth, window.innerHeight);        }        function onDocumentMouseMove(event) {            mouseX = event.clientX - windowHalfX;            mouseY = event.clientY - windowHalfY;        }        function onDocumentTouchStart(event) {            if (event.touches.length > 1) {                event.preventDefault();                mouseX = event.touches[0].pageX - windowHalfX;                mouseY = event.touches[0].pageY - windowHalfY;            }        }        function onDocumentTouchMove(event) {            if (event.touches.length == 1) {                event.preventDefault();                mouseX = event.touches[0].pageX - windowHalfX;                mouseY = event.touches[0].pageY - windowHalfY;            }        }        //        function animate() {            requestAnimationFrame(animate);            render();        }        function render() {            camera.position.y += (-mouseY + 200 - camera.position.y) * .05;            camera.lookAt(scene.position);            renderer.render(scene, camera);            var time = Date.now() * 0.0001;            for (var i = 0; i < scene.children.length; i++) {                var object = scene.children[i];                if (object instanceof THREE.Line) {                    object.rotation.y = time * (i < 4 ? (i + 1) : -(i + 1));                }            }        }

註:本文愛編程原創文章,轉載請註明原文地址:http://www.w2bc.com/Article/6585

html5 canvas實現夢幻的3D刺蝟球

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.