This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
var $ = require('jquery');
$(document).ready(() => {
jsCanvasSnow.init();
jsCanvasSnow.start();
}, false);
function jsParticle(origin, velocity, size, amplitude) {
this.origin = origin;
this.position = new Vector2(origin.x, origin.y);
this.velocity = velocity || new Vector2(0, 0);
this.size = size;
this.amplitude = amplitude;
this.dx = Math.random() * 100;
this.update = function(delta_time) {
this.position.y = this.velocity.y * delta_time;
this.dx = this.velocity.x * delta_time;
this.position.x = this.origin.x * (this.amplitude * Math.sin(this.dx));
};
}
var jsCanvasSnow =
{
canvas : null,
ctx : null,
particles : \[\],
running : false,
start\_time : 0,
frame\_time : 0,
init : function( )
{
// use the container width and height
this.canvas = document.getElementById('particle\_canvas');
this.ctx = this.canvas.getContext('2d');
this.resize(window.innerWidth, window.innerHeight);
// change these values
// the 2 element arrays represent min and max values
this.pAmount = 5000; // amount of particles
this.pSize = \[0.5 , 1.5\]; // min and max size
this.pSwing = \[0.1, 1\]; // min and max oscilation speed for x movement
this.pSpeed = \[40, 100\], // min and max y speed
this.pAmplitude = \[25, 50\]; // min and max distance for x movement
this._init_particles();
},
start : function()
{
this.running = true;
this.start\_time = this.frame\_time = microtime();
this.\_loop();
},
stop : function()
{
this.running = false;
},
resize : function(w, h)
{
this.canvas.width = w;
this.canvas.height = h;
},
\_loop : function()
{
if ( jsCanvasSnow.running )
{
jsCanvasSnow.\_clear();
jsCanvasSnow.\_update();
jsCanvasSnow.\_draw();
jsCanvasSnow.\_queue();
}
},
\_init\_particles : function()
{
// clear the particles array
this.particles.length = 0;
for ( var i = 0 ; i < this.pAmount ; i )
{
var origin = new Vector2(frand(0, this.canvas.width), frand(-this.canvas.height, 0));
var velocity = new Vector2(frand(this.pSwing\[0\],this.pSwing\[1\]), frand(this.pSpeed\[0\],this.pSpeed\[1\]));
var size = frand(this.pSize\[0\], this.pSize\[1\]);
var amplitude = frand(this.pAmplitude\[0\], this.pAmplitude\[1\]);
this.particles.push(new jsParticle(origin, velocity, size, amplitude));
}
},
\_update : function()
{
// calculate the time since the last frame
var now\_time = microtime();
var delta\_time = now\_time - this.frame\_time;
for ( var i = 0 ; i < this.particles.length ; i )
{
var particle = this.particles\[i\];
particle.update(delta\_time);
if (particle.position.y-particle.size > this.canvas.height)
{
// reset the particle to the top and a random x position
particle.position.y = -particle.size;
particle.position.x = particle.origin.x = Math.random() * this.canvas.width;
particle.dx = Math.random() * 100;
}
}
// save this time for the next frame
this.frame\_time = now\_time;
},
\_draw : function()
{
this.ctx.fillStyle = 'rgb(255,255,255)';
for ( var i = 0 ; i < this.particles.length ; i )
{
var particle = this.particles\[i\];
this.ctx.fillRect(particle.position.x, particle.position.y, particle.size, particle.size);
}
},
\_clear : function()
{
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
\_queue : function()
{
window.requestAnimationFrame( jsCanvasSnow.\_loop );
}
};
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/CodingHelp/...