html5悬浮圆圈背景动画特效,html5圆圈浮动背景动画特效

特效描述:html5 圆圈浮动 背景动画特效。html5圆圈浮动背景动画特效

代码结构

1. 引入JS

2. HTML代码

var container;

var camera, scene, projector, renderer;

var PI2 = Math.PI * 2;

var programFill = function ( context ) {

context.beginPath();

context.arc( 0, 0, 1, 0, PI2, true );

context.closePath();

context.fill();

}

var programStroke = function ( context ) {

context.lineWidth = 0.05;

context.beginPath();

context.arc( 0, 0, 1, 0, PI2, true );

context.closePath();

context.stroke();

}

var mouse = { x: 0, y: 0 }, INTERSECTED;

init();

animate();

function init() {

container = document.createElement( 'div' );

container.id = 'bgc';

container.style.position = 'relative';

container.style.zIndex = '0';

document.body.appendChild( container );

camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );

camera.position.set( 0, 300, 500 );

scene = new THREE.Scene();

for ( var i = 0; i < 100; i ++ ) {

var particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808080 + 0x808080, program: programStroke } ) );

particle.position.x = Math.random() * 800 - 400;

particle.position.y = Math.random() * 800 - 400;

particle.position.z = Math.random() * 800 - 400;

particle.scale.x = particle.scale.y = Math.random() * 10 + 10;

scene.add( particle );

}

projector = new THREE.Projector();

renderer = new THREE.CanvasRenderer();

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

container.appendChild( renderer.domElement );

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

//

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

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;

camera.updateProjectionMatrix();

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

}

function onDocumentMouseMove( event ) {

event.preventDefault();

mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;

mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

}

//

function animate() {

requestAnimationFrame( animate );

render();

}

var radius = 600;

var theta = 0;

function render() {

// rotate camera

theta += 0.2;

camera.position.x = radius * Math.sin( theta * Math.PI / 360 );

camera.position.y = radius * Math.sin( theta * Math.PI / 360 );

camera.position.z = radius * Math.cos( theta * Math.PI / 360 );

camera.lookAt( scene.position );

// find intersections

camera.updateMatrixWorld();

var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );

projector.unprojectVector( vector, camera );

var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );

var intersects = ray.intersectObjects( scene.children );

if ( intersects.length > 0 ) {

if ( INTERSECTED != intersects[ 0 ].object ) {

if ( INTERSECTED ) INTERSECTED.material.program = programStroke;

INTERSECTED = intersects[ 0 ].object;

INTERSECTED.material.program = programFill;

}

} else {

if ( INTERSECTED ) INTERSECTED.material.program = programStroke;

INTERSECTED = null;

}

renderer.render( scene, camera );

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值