forked from mit-rss/website2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive.js
More file actions
110 lines (99 loc) · 3.29 KB
/
Copy pathinteractive.js
File metadata and controls
110 lines (99 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const title = document.getElementById('home');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Set initial canvas size
function setCanvasSize() {
canvas.width = title.offsetWidth;
canvas.height = title.offsetHeight;
}
setCanvasSize();
const shapes = [];
const img = new Image();
img.src = 'img/ruby.png'; // Set PNG image path
img.onload = () => {
for (let i = 0; i < 5; i++) {
shapes.push(new Shape(
Math.random() * canvas.width,
Math.random() * canvas.height,
canvas.width / 25 + Math.random() * 30
));
}
animate();
};
class Shape {
constructor(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.velocity = { x: Math.random() * 6 - 3, y: Math.random() * 6 - 3 };
this.blur = 5;
this.friction = 0.98;
this.rotation = Math.random() * Math.PI * 2;
this.angularVelocity = Math.random() * 0.1;
this.angularFriction = 0.98;
}
draw() {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.rotation);
ctx.filter = `blur(${this.blur}px)`;
ctx.drawImage(img, -this.radius, -this.radius, this.radius * 2, this.radius * 2);
ctx.restore();
}
move() {
this.x += this.velocity.x;
this.y += this.velocity.y;
this.velocity.x *= this.friction;
this.velocity.y *= this.friction;
this.rotation += this.angularVelocity;
this.angularVelocity *= this.angularFriction;
this.handleBoundaries();
}
handleBoundaries() {
if (this.x - this.radius < 0 || this.x + this.radius > canvas.width) {
this.velocity.x *= -1;
this.x = Math.max(this.radius, Math.min(canvas.width - this.radius, this.x));
}
if (this.y - this.radius < 0 || this.y + this.radius > canvas.height) {
this.velocity.y *= -1;
this.y = Math.max(this.radius, Math.min(canvas.height - this.radius, this.y));
}
}
interact(cursorX, cursorY) {
const dist = Math.hypot(cursorX - this.x, cursorY - this.y);
const threshold = 150;
if (dist < threshold) {
const angle = Math.atan2(cursorY - this.y, cursorX - this.x);
const force = (threshold - dist) / threshold;
this.velocity.x -= Math.cos(angle) * force;
this.velocity.y -= Math.sin(angle) * force;
this.angularVelocity += (Math.random() - 0.5) * 0.01;
}
}
}
let cursorX = -100, cursorY = -100;
title.addEventListener('mousemove', (e) => {
const rect = canvas.getBoundingClientRect();
cursorX = e.clientX - rect.left;
cursorY = e.clientY - rect.top;
});
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
shapes.forEach(shape => {
shape.move();
shape.interact(cursorX, cursorY);
shape.draw();
});
requestAnimationFrame(animate);
}
window.addEventListener('resize', () => {
const newWidth = title.offsetWidth;
const newHeight = title.offsetHeight;
const widthRatio = newWidth / canvas.width;
const heightRatio = newHeight / canvas.height;
shapes.forEach(shape => {
shape.x *= widthRatio;
shape.y *= heightRatio;
});
setCanvasSize();
});