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

使用HTML5 的canvas標(biāo)簽和 three.js 實(shí)現(xiàn)了 3D 的雪花飛舞效果。你可以拖動(dòng)鼠標(biāo)進(jìn)行旋轉(zhuǎn),需要在支持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>

}

更多信息請(qǐng)查看網(wǎng)頁(yè)制作
易賢網(wǎng)手機(jī)網(wǎng)站地址:html5的canvas實(shí)現(xiàn)3d雪花飄舞效果
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
相關(guān)閱讀網(wǎng)頁(yè)制作

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)