前回の第26回
パーティクルの動き始めもランダムにする
前回できあがりの第26回コード4
var total = 200; // 100;
function resetParticle(particle) {
var lifetime = Math.random()* total | 0;
particle.reset(center.x, center.y, radius, angle, lifetime);
}
ここはやはり,
// Particle.prototype.reset = function(x, y, radius, angle, lifetime) {
Particle.prototype.reset = function(x, y, radius, angle, lifetime, delay) {
this.delay = delay;
this.index = 0;
};
Particle.prototype.isMovable = function() {
var movable = (++this.index > this.delay);
return movable;
};
アニメーションの関数
タメの値
function initialize() {
for(var i = 0; i < total; i++) {
// resetParticle(particle);
resetParticle(particle, i);
}
}
// function resetParticle(particle) {
function resetParticle(particle, delay) {
delay *= Math.random();
// particle.reset(center.x, center.y, radius, angle, lifetime);
particle.reset(center.x, center.y, radius, angle, lifetime, delay);
}
function tick() {
for(var i = 0; i < count; i++) {
var particle = particles[i];
// moveParticle(particle);
if (particle.isMovable()) {
moveParticle(particle, i);
}
}
}
// function moveParticle(particle) {
function moveParticle(particle, index) {
if (particle.lifetime > 0) {
} else {
// resetParticle(particle);
resetParticle(particle, index);
}
}
これらの書替えをおこなったパーティクルのクラス
コード1 楕円軌道の動き始めにタメが加わるパーティクルのクラス
function Particle(radius, color) {
this.initialize();
this.graphics.beginFill(color)
.drawCircle(0, 0, radius)
.endFill();
this.compositeOperation = "lighter";
}
Particle.prototype = new createjs.Shape();
Particle.prototype.reset = function(x, y, radius, angle, lifetime, delay) {
this.x = x;
this.y = y;
this.speedX = radius * Math.cos(angle);
this.speedY = radius * Math.sin(angle);
this.lifetime = lifetime;
this.angle = 0;
this.delay = delay;
this.index = 0;
};
Particle.prototype.move = function(advance) {
var angle = this.angle + advance;
var velocityX = this.speedX * Math.sin(angle);
var velocityY = this.speedY * Math.cos(angle);
this.x += velocityX;
this.y += velocityY;
this.angle = angle;
this.lifetime--;
};
Particle.prototype.isMovable = function() {
var movable = (++this.index > this.delay);
return movable;
};
コード2 パーティクルの動き始めにランダムなタメを加えてから楕円軌道で回す
var stage;
var total = 200;
var center = new createjs.Point();
var particles = [];
var fading = 0.04;
function initialize() {
var canvasElement = document.getElementById("myCanvas");
var stageWidth = canvasElement.width;
var stageHeight = canvasElement.height;
stage = new createjs.Stage(canvasElement);
stage.autoClear = false;
center.x = stageWidth / 2;
center.y = stageHeight / 2;
for(var i = 0; i < total; i++) {
var radius = 1 + Math.random() * 4;
var particle = new Particle(radius, "#0016E9");
resetParticle(particle, i);
particles.push(particle);
stage.addChild(particle);
}
addBackground(stageWidth, stageHeight, fading);
createjs.Ticker.timingMode = createjs.Ticker.RAF;
createjs.Ticker.addEventListener("tick", tick);
}
function resetParticle(particle, delay) {
var radius = 1 + Math.random();
var angle = Math.random() * Math.PI * 2;
var lifetime = Math.random()* total | 0;
delay *= Math.random();
particle.reset(center.x, center.y, radius, angle, lifetime, delay);
}
function tick() {
var count = particles.length;
for(var i = 0; i < count; i++) {
var particle = particles[i];
if (particle.isMovable()) {
moveParticle(particle, i);
}
}
stage.update();
}
function moveParticle(particle, index) {
if (particle.lifetime > 0) {
particle.move(Math.PI / 90);
} else {
resetParticle(particle, index);
}
}
function addBackground(width, height, alpha) {
var background = new createjs.Shape();
background.graphics.beginFill("black")
.drawRect(0, 0, width, height)
.endFill();
stage.addChild(background);
background.alpha = alpha;
}