html5的canvas實現(xiàn)3d雪花飄舞效果
來源:易賢網 閱讀:1700 次 日期:2016-07-11 11:09:04
溫馨提示:易賢網小編為您整理了“html5的canvas實現(xiàn)3d雪花飄舞效果”,方便廣大網友查閱!

使用HTML5 的canvas標簽和 three.js 實現(xiàn)了 3D 的雪花飛舞效果。你可以拖動鼠標進行旋轉,需要在支持CSS3屬性的瀏覽器下瀏覽

代碼如下:

var SCREEN_WIDTH = window.innerWidth;

var SCREEN_HEIGHT = window.innerHeight;</p> <p> var container;</p> <p> var particle;</p> <p> var camera;

var scene;

var renderer;</p> <p> var mouseX = 0;

var mouseY = 0;</p> <p> var windowHalfX = window.innerWidth / 2;

var windowHalfY = window.innerHeight / 2;

var particles = [];

var particleImage = new Image();//THREE.ImageUtils.loadTexture( "img/ParticleSmoke.png" );

particleImage.src = 'images/ParticleSmoke.png'; </p> <p>

function init() {</p> <p> container = document.createElement('div');

document.body.appendChild(container);</p> <p> camera = new THREE.PerspectiveCamera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );

camera.position.z = 1000;</p> <p> scene = new THREE.Scene();

scene.add(camera);

renderer = new THREE.CanvasRenderer();

renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);

var material = new THREE.ParticleBasicMaterial( { map: new THREE.Texture(particleImage) } );

for (var i = 0; i < 500; i++) {</p> <p> particle = new Particle3D( material);

particle.position.x = Math.random() * 2000 - 1000;

particle.position.y = Math.random() * 2000 - 1000;

particle.position.z = Math.random() * 2000 - 1000;

particle.scale.x = particle.scale.y = 1;

scene.add( particle );

particles.push(particle);

}</p> <p> container.appendChild( renderer.domElement );</p> <p>

document.addEventListener( 'mousemove', onDocumentMouseMove, false );

document.addEventListener( 'touchstart', onDocumentTouchStart, false );

document.addEventListener( 'touchmove', onDocumentTouchMove, false );

setInterval( loop, 1000 / 60 );

}

function onDocumentMouseMove( event ) {</p> <p> mouseX = event.clientX - windowHalfX;

mouseY = event.clientY - windowHalfY;

}</p> <p> function onDocumentTouchStart( event ) {</p> <p> if ( event.touches.length == 1 ) {</p> <p> event.preventDefault();</p> <p> mouseX = event.touches[ 0 ].pageX - windowHalfX;

mouseY = event.touches[ 0 ].pageY - windowHalfY;

}

}</p> <p> function onDocumentTouchMove( event ) {</p> <p> if ( event.touches.length == 1 ) {</p> <p> event.preventDefault();</p> <p> mouseX = event.touches[ 0 ].pageX - windowHalfX;

mouseY = event.touches[ 0 ].pageY - windowHalfY;

}

}</p> <p> //</p> <p> function loop() {</p> <p> for(var i = 0; i<particles.length; i++)

{</p> <p> var particle = particles[i];

particle.updatePhysics();

with(particle.position)

{

if(y<-1000) y+=2000;

if(x>1000) x-=2000;

else if(x<-1000) x+=2000;

if(z>1000) z-=2000;

else if(z<-1000) z+=2000;

}

}

camera.position.x += ( mouseX - camera.position.x ) * 0.05;

camera.position.y += ( - mouseY - camera.position.y ) * 0.05;

camera.lookAt(scene.position); </p> <p> renderer.render( scene, camera );</p> <p>

}

更多信息請查看網頁制作

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯(lián)系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網