"use strict"; // this is a javascript directive and must be on the first line of the
// script (if included but is not a requirement).
// It forces a more code run under more stringent rules. The advantages
// are many, including making the code run faster.
var imageLoadCount = 0; // counts the number of image loading, counts down as they load
var readyToAnimate = false; // flag to indicate that resources are available
// image indexes in images array to get correct images in the animation.
const PARTICLE_IMAGE_INDEX = 0;
const BACKGROUND_IMAGE_INDEX = 1;
var images = []; // an array of images
// What follows is a self evoking function, this will isolate the loading stuff from the
// rest of the script as it is only needed once at start so no point keeping references to it all
// the self invoking function is
// (function(){...code body})()
// the() at the end forces javascript to run what is inside the() before it.
(function(){
function imageLoaded(){ // image onload function "this" is a reference to the image
imageLoadCount -= 1; // count the loaded image
// if the count is zero all images have loaded
if(imageLoadCount === 0){
readyToAnimate = true;
}
}
// a list of image urls that need to be loaded.
const imageURLs = [
"http://wightfield.com/_temp/smoke_600-60.png",
"http://wightfield.com/_temp/smoke_600-60.png", // repeating the image just as example
];
imageURLs.forEach(function(url){ // for each image url start the load process
var img = new Image();
img.src = url;
img.onload = imageLoaded; // set the image onload function
imageLoadCount += 1; // count the number of images loading
images.push(img);
});
})(); // run the function
var canvas = document.createElement('canvas');
var w = canvas.width = 800;
var h = canvas.height = 700;
var c = canvas.getContext('2d');
document.body.appendChild(canvas);
var position = {
x : 450,
y : 410
};
var mugPosition = {
x : w/3,
y : 500
};
var particles = [];
var random = function (min, max) {
// YOU had Math.random() * (max - min) * min; I assume you did not want to multiply by min
return Math.random() * (max - min) + min;
};
var particleCount = 0;
const ALPHA_CUTOFF = 1/255;
const ALPHA_START = 0.4;
const ALPHA_DECAY = 0.9
// calculate the number of particles need to show each step of the alpha decay
const MAX_PARTICLES = Math.ceil(Math.log(ALPHA_CUTOFF/ALPHA_START)/Math.log(ALPHA_DECAY));
console.log(MAX_PARTICLES)
var draw = function() {
var i;
if(readyToAnimate){ // wait for the resources to load
c.setTransform(1,0,0,1,0,0); // reset transform
c.clearRect(0, 0, w, h);
// If you want to render a background image do it here. If the image is the size of the
// canvas then there is no need to clear the canvas and you can delete the line above
/* As an example
c.drawImage(images[BACKGROUND_IMAGE_INDEX],0,0,w,h); // draws image filling the canvas
*/
if (particles[particleCount % MAX_PARTICLES] === undefined) {
particles[particleCount % MAX_PARTICLES] = new Particle(position.x, position.y);
} else {
particles[particleCount % MAX_PARTICLES].reset(position.x, position.y);
}
particleCount += 1;
for (i = 0; i < particles.length; i++) {
particles[i].update();
}
}else{
// if you wanted you can add loading progress here
}
requestAnimationFrame(draw);
};
function Particle(x, y) {
this.reset(x, y);
}
Particle.prototype = {
reset : function (x, y) {
this.x = x;
this.y = y;
this.velX = (random(1, 10) - 5)/10;
this.velY = -9;
this.size = random(3, 6)/10;
this.alpha = ALPHA_START ;
this.image = images[PARTICLE_IMAGE_INDEX];
},
update : function() {
if(this.alpha >= ALPHA_CUTOFF){ // no point in rendering a invisible sprite
this.y += this.velY;
this.x += this.velX;
this.velY *= 0.99;
c.globalAlpha = this.alpha;
c.setTransform(this.size,0,0,this.size,this.x, this.y);
c.drawImage(this.image, -this.image.width/2, -this.image.height/2);
this.alpha *= ALPHA_DECAY ;
this.size += 0.015; //
}
}
}
// start the animation. Images may not have loaded yet
requestAnimationFrame(draw);
canvas {
border: 1px dotted black;
}
使用(H、W 0,0、) '' ctx.clearRectではなく、fillRect、あなたは、あなたが望む透明キャンバスを取得します。 – Blindman67
それは何をしているのか分かりませんが、それは治療法です!ありがとうございました。 –
私は、あなたが変更を行うことができるいくつかのビットがあるので、答えを出します。 – Blindman67