css和js3d粒子,使用EaselJS实现的3D球形粒子运动

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

// Config

var TOTAL = 1000;

var FPS = 60;

// Vars

var particles = [];

var stage, particle, maxx, maxy, gui, stats;

var changing = false;

function init() {

// get a reference to the canvas we'll be working with:

var canvas = document.getElementById("canvas");

if (!canvas || !canvas.getContext) return;

stage = new createjs.Stage(canvas);

stage.mouseEventsEnabled = true;

stage.mouseMoveOutside = false;

// Load

var blobSrc = new Image();

blobSrc.src = 'http://www.as-flash.com/ne_brisi/canvas/ball.png';

blobSrc.name = 'particle';

blobSrc.onload = onLoaded;

function onLoaded(e) {

particle = new createjs.Bitmap(blobSrc);

maxx = canvas.width - blobSrc.width;

maxy = canvas.height - blobSrc.height;

addParticles();

build();

animate();

}

function animate() {

// TICKER

createjs.Ticker.addListener(this);

createjs.Ticker.setFPS(FPS);

createjs.Ticker.useRAF = true;

}

function addParticles() {

for (var i = 0; i < particles.length; i++) {

stage.removeChild(particles[i]);

}

particles = [];

for (i = 0; i < TOTAL; i++) {

particles[i] = particle.clone();

particles[i].vx = 0;

particles[i].vy = 0;

}

}

// STATS

stats = new Stats();

stats.setMode(0);

document.body.appendChild(stats.domElement);

// GUI

gui = new dat.GUI({

autoPlace: false

});

gui.width = 220;

var customContainer = document.getElementById('gui-container');

customContainer.appendChild(gui.domElement);

var fpsc = gui.add(window, 'FPS', 0, 60).step(5).name('FPS');

var totalc = gui.add(window, 'TOTAL', 100, 5000).step(100).name('Particles');

gui.close();

fpsc.onChange(function(value) {

createjs.Ticker.setFPS(FPS);

});

totalc.onChange(function(value) {

changing = true;

});

totalc.onFinishChange(function(value) {

changing = false;

addParticles();

build();

});

}

// Build

function build() {

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

particles[i].x = Math.random() * maxx;

particles[i].y = Math.random() * maxy;

stage.addChild(particles[i]);

}

}

function tick() {

if (changing) return;

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

var blob = particles[i];

var dx = blob.x - stage.mouseX;

var dy = blob.y - stage.mouseY;

var vx = blob.vx;

var vy = blob.vy;

if (dx * dx + dy * dy <= 10000) {

vx += dx * 0.01;

vy += dy * 0.01;

}

vx *= 0.95;

vy *= 0.95;

vx += Math.random() - 0.5;

vy += Math.random() - 0.5;

var x = blob.x += vx;

var y = blob.y += vy;

if (x < 0 || x > maxx || y < 0 || y > maxy) {

var r = Math.atan2(y - maxy / 2, x - maxx / 2);

vx = -Math.cos(r);

vy = -Math.sin(r);

}

blob.vx = vx;

blob.vy = vy;

}

// update

stage.update();

stats.update();

}

// Init

window.addEventListener('load', init, false);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值