mirror of
https://github.com/movie-web/movie-web.git
synced 2024-11-11 01:05:08 +01:00
Upgrade particle rendering (fix cos/sin being in wrong order)
This commit is contained in:
parent
b94b6b0249
commit
4ffbe45ab1
BIN
public/fishie.png
Normal file
BIN
public/fishie.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 314 B |
@ -16,7 +16,14 @@ class Particle {
|
|||||||
|
|
||||||
ran = 0;
|
ran = 0;
|
||||||
|
|
||||||
constructor(canvas: HTMLCanvasElement) {
|
image: null | HTMLImageElement = null;
|
||||||
|
|
||||||
|
constructor(canvas: HTMLCanvasElement, { doFish } = { doFish: false }) {
|
||||||
|
if (doFish) {
|
||||||
|
this.image = new Image();
|
||||||
|
if (this.image) this.image.src = "/fishie.png";
|
||||||
|
}
|
||||||
|
|
||||||
this.reset(canvas);
|
this.reset(canvas);
|
||||||
this.initialize(canvas);
|
this.initialize(canvas);
|
||||||
}
|
}
|
||||||
@ -26,12 +33,17 @@ class Particle {
|
|||||||
this.y = Math.random() * 100 + 5;
|
this.y = Math.random() * 100 + 5;
|
||||||
|
|
||||||
this.radius = 1 + Math.floor(Math.random() * 0.5);
|
this.radius = 1 + Math.floor(Math.random() * 0.5);
|
||||||
this.direction = -((Math.random() * Math.PI) / 2) + Math.PI / 4;
|
this.direction = (Math.random() * Math.PI) / 2 + Math.PI / 4;
|
||||||
this.speed = 0.02 + Math.random() * 0.08;
|
this.speed = 0.02 + Math.random() * 0.08;
|
||||||
|
|
||||||
const second = 60;
|
const second = 60;
|
||||||
this.lifetime = second * 3 + Math.random() * (second * 30);
|
this.lifetime = second * 3 + Math.random() * (second * 30);
|
||||||
|
|
||||||
|
if (this.image) {
|
||||||
|
this.direction = Math.random() <= 0.5 ? 0 : Math.PI;
|
||||||
|
this.lifetime = 30 * second;
|
||||||
|
}
|
||||||
|
|
||||||
this.ran = 0;
|
this.ran = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,8 +58,8 @@ class Particle {
|
|||||||
update(canvas: HTMLCanvasElement) {
|
update(canvas: HTMLCanvasElement) {
|
||||||
this.ran += 1;
|
this.ran += 1;
|
||||||
|
|
||||||
const addX = this.speed * Math.sin(this.direction);
|
const addX = this.speed * Math.cos(this.direction);
|
||||||
const addY = this.speed * Math.cos(this.direction);
|
const addY = this.speed * Math.sin(this.direction);
|
||||||
this.x += addX;
|
this.x += addX;
|
||||||
this.y += addY;
|
this.y += addY;
|
||||||
|
|
||||||
@ -67,17 +79,25 @@ class Particle {
|
|||||||
const o = (x - x * x) * 4;
|
const o = (x - x * x) * 4;
|
||||||
ctx.globalAlpha = Math.max(0, o * 0.8);
|
ctx.globalAlpha = Math.max(0, o * 0.8);
|
||||||
|
|
||||||
ctx.ellipse(
|
if (this.image) {
|
||||||
this.x,
|
ctx.translate(this.x, this.y);
|
||||||
this.y,
|
const w = 10;
|
||||||
this.radius,
|
const h = (this.image.naturalWidth / this.image.naturalHeight) * w;
|
||||||
this.radius * 1.5,
|
ctx.rotate(this.direction - Math.PI);
|
||||||
this.direction,
|
ctx.drawImage(this.image, -w / 2, h, h, w);
|
||||||
0,
|
} else {
|
||||||
Math.PI * 2
|
ctx.ellipse(
|
||||||
);
|
this.x,
|
||||||
ctx.fillStyle = "white";
|
this.y,
|
||||||
ctx.fill();
|
this.radius,
|
||||||
|
this.radius * 1.5,
|
||||||
|
this.direction,
|
||||||
|
0,
|
||||||
|
Math.PI * 2
|
||||||
|
);
|
||||||
|
ctx.fillStyle = "white";
|
||||||
|
ctx.fill();
|
||||||
|
}
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -93,8 +113,13 @@ function ParticlesCanvas() {
|
|||||||
canvas.width = canvas.scrollWidth;
|
canvas.width = canvas.scrollWidth;
|
||||||
canvas.height = canvas.scrollHeight;
|
canvas.height = canvas.scrollHeight;
|
||||||
|
|
||||||
for (let i = 0; i < 20; i += 1) {
|
const shouldShowFishie = Math.floor(Math.random() * 600) === 1;
|
||||||
const particle = new Particle(canvas);
|
const particleCount = 20;
|
||||||
|
|
||||||
|
for (let i = 0; i < particleCount; i += 1) {
|
||||||
|
const particle = new Particle(canvas, {
|
||||||
|
doFish: shouldShowFishie && i <= particleCount / 2,
|
||||||
|
});
|
||||||
particles.push(particle);
|
particles.push(particle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user