|
1 | 1 | let animationCanvas; |
2 | | -let circleSize = 0; |
3 | | -let growing = true; |
| 2 | +let particles = []; |
| 3 | +const numParticles = 100; |
| 4 | +const maxParticleSize = 8; |
| 5 | +const minParticleSize = 2; |
| 6 | +const maxSpeed = 2; |
| 7 | +const repulsionRadius = 150; |
| 8 | +const repulsionStrength = 1; |
4 | 9 |
|
5 | 10 | function setup() { |
6 | | - const parent = document.getElementById( |
7 | | - "menuAnimationCanvas", |
8 | | - ).parentElement; |
9 | | - animationCanvas = createCanvas( |
10 | | - parent.offsetWidth, |
11 | | - parent.offsetHeight, |
| 11 | + let pageHeight = Math.max( |
| 12 | + document.body.scrollHeight, |
| 13 | + document.documentElement.scrollHeight, |
| 14 | + document.body.offsetHeight, |
| 15 | + document.documentElement.offsetHeight, |
| 16 | + document.body.clientHeight, |
| 17 | + document.documentElement.clientHeight, |
12 | 18 | ); |
13 | | - animationCanvas.parent("menuAnimationCanvas"); |
14 | | - background(22, 27, 34); // 背景色を一度だけ設定 |
| 19 | + |
| 20 | + animationCanvas = createCanvas(windowWidth, pageHeight); |
| 21 | + animationCanvas.position(0, 0); |
| 22 | + animationCanvas.style("z-index", "-1"); |
| 23 | + animationCanvas.style("pointer-events", "none"); |
| 24 | + |
| 25 | + for (let i = 0; i < numParticles; i++) { |
| 26 | + particles.push(new Particle()); |
| 27 | + } |
15 | 28 | } |
16 | 29 |
|
17 | 30 | function draw() { |
18 | | - background(22, 27, 34, 50); |
19 | | - noStroke(); |
20 | | - fill(255, 255, 255, 150); |
21 | | - |
22 | | - if (growing) { |
23 | | - circleSize += 0.5; |
24 | | - if (circleSize > 50) { |
25 | | - growing = false; |
26 | | - } |
27 | | - } else { |
28 | | - circleSize -= 0.5; |
29 | | - if (circleSize < 10) { |
30 | | - growing = true; |
31 | | - } |
32 | | - } |
33 | | - ellipse(width / 2, height / 2, circleSize, circleSize); |
| 31 | + clear(); |
34 | 32 |
|
35 | | - fill(255, 255, 255, 80); |
36 | | - ellipse(mouseX, mouseY, 15, 15); |
| 33 | + for (let particle of particles) { |
| 34 | + particle.update(); |
| 35 | + particle.display(); |
| 36 | + } |
37 | 37 | } |
38 | 38 |
|
39 | 39 | function windowResized() { |
40 | | - const parent = document.getElementById( |
41 | | - "menuAnimationCanvas", |
42 | | - ).parentElement; |
43 | | - resizeCanvas(parent.offsetWidth, parent.offsetHeight); |
| 40 | + let pageHeight = Math.max( |
| 41 | + document.body.scrollHeight, |
| 42 | + document.documentElement.scrollHeight, |
| 43 | + document.body.offsetHeight, |
| 44 | + document.documentElement.offsetHeight, |
| 45 | + document.body.clientHeight, |
| 46 | + document.documentElement.clientHeight, |
| 47 | + ); |
| 48 | + resizeCanvas(windowWidth, pageHeight); |
| 49 | +} |
| 50 | + |
| 51 | +class Particle { |
| 52 | + constructor() { |
| 53 | + this.pos = createVector(random(width), random(height)); |
| 54 | + this.vel = p5.Vector.random2D().mult( |
| 55 | + random(0.5, maxSpeed), |
| 56 | + ); |
| 57 | + this.acc = createVector(0, 0); |
| 58 | + this.size = random(minParticleSize, maxParticleSize); |
| 59 | + this.color = color(255, 255, 255, random(50, 150)); // 半透明の白 |
| 60 | + } |
| 61 | + |
| 62 | + applyForce(force) { |
| 63 | + this.acc.add(force); |
| 64 | + } |
| 65 | + |
| 66 | + update() { |
| 67 | + let mouse = createVector(mouseX, mouseY); |
| 68 | + let force = p5.Vector.sub(this.pos, mouse); |
| 69 | + let distance = force.mag(); |
| 70 | + |
| 71 | + if (distance < repulsionRadius) { |
| 72 | + let strength = |
| 73 | + -repulsionStrength / (distance * distance); |
| 74 | + force.setMag(strength); |
| 75 | + this.applyForce(force); |
| 76 | + } |
| 77 | + |
| 78 | + this.vel.add(this.acc); |
| 79 | + this.vel.limit(maxSpeed); |
| 80 | + this.pos.add(this.vel); |
| 81 | + this.acc.mult(0); |
| 82 | + |
| 83 | + if (this.pos.x > width || this.pos.x < 0) { |
| 84 | + this.vel.x *= -1; |
| 85 | + } |
| 86 | + if (this.pos.y > height || this.pos.y < 0) { |
| 87 | + this.vel.y *= -1; |
| 88 | + } |
| 89 | + |
| 90 | + if (this.pos.x > width) this.pos.x = 0; |
| 91 | + if (this.pos.x < 0) this.pos.x = width; |
| 92 | + if (this.pos.y > height) this.pos.y = 0; |
| 93 | + if (this.pos.y < 0) this.pos.y = height; |
| 94 | + } |
| 95 | + |
| 96 | + display() { |
| 97 | + noStroke(); |
| 98 | + fill(this.color); |
| 99 | + ellipse(this.pos.x, this.pos.y, this.size, this.size); |
| 100 | + } |
44 | 101 | } |
0 commit comments