-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle-Text.html
More file actions
164 lines (147 loc) · 5.69 KB
/
Copy pathParticle-Text.html
File metadata and controls
164 lines (147 loc) · 5.69 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Futuristic Particle Shifter</title>
<style>
body {
margin: 0;
background: #000;
overflow: hidden;
font-family: 'Courier New', Courier, monospace;
}
canvas {
display: block;
}
#ui {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
color: #0ff;
text-align: center;
pointer-events: none;
text-shadow: 0 0 10px #0ff;
letter-spacing: 2px;
font-size: 12px;
text-transform: uppercase;
}
</style>
</head>
<body>
<div id="ui">Move mouse to scatter — Click to shift text</div>
<canvas id="canvas1"></canvas>
<script>
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let particleArray = [];
let words = ["FUTURE", "MAX-HTML5", "SHIFTER", "AMAZED"];
let wordIndex = 0;
// Mouse properties
const mouse = {
x: null,
y: null,
radius: 100 // Area of influence
}
window.addEventListener('mousemove', function(event) {
mouse.x = event.x;
mouse.y = event.y;
});
class Particle {
constructor(x, y) {
this.x = Math.random() * canvas.width; // Start from random spot
this.y = Math.random() * canvas.height;
this.destX = x; // The pixel's "home" position
this.destY = y;
this.size = 2;
this.baseX = this.x;
this.baseY = this.y;
this.density = (Math.random() * 30) + 1; // Movement speed variation
this.color = '#00f2ff';
}
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
}
update() {
// Physics calculation: Distance from mouse
let dx = mouse.x - this.x;
let dy = mouse.y - this.y;
let distance = Math.sqrt(dx * dx + dy * dy);
let forceDirectionX = dx / distance;
let forceDirectionY = dy / distance;
// Max distance for repulsion
let maxDistance = mouse.radius;
let force = (maxDistance - distance) / maxDistance;
let directionX = forceDirectionX * force * this.density;
let directionY = forceDirectionY * force * this.density;
if (distance < mouse.radius) {
this.x -= directionX;
this.y -= directionY;
this.color = '#ff00ff'; // Turn purple when disturbed
} else {
// Magnetically pull back to original position
if (this.x !== this.destX) {
let dx = this.x - this.destX;
this.x -= dx / 10;
}
if (this.y !== this.destY) {
let dy = this.y - this.destY;
this.y -= dy / 10;
}
this.color = '#00f2ff';
}
}
}
function init(text) {
particleArray = [];
// Draw text temporarily to scan pixels
ctx.fillStyle = 'white';
ctx.font = 'bold 120px Verdana';
ctx.textAlign = 'center';
ctx.fillText(text, canvas.width/2, canvas.height/2);
// Scan the box where the text is (Red, Green, Blue, Alpha)
const textCoordinates = ctx.getImageData(0, 0, canvas.width, canvas.height);
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Loop through pixel data (scanning every 5th pixel for performance)
for (let y = 0; y < textCoordinates.height; y += 5) {
for (let x = 0; x < textCoordinates.width; x += 5) {
// Check if Alpha (4th value) is high enough (visible pixel)
if (textCoordinates.data[(y * 4 * textCoordinates.width) + (x * 4) + 3] > 128) {
let positionX = x;
let positionY = y;
particleArray.push(new Particle(positionX, positionY));
}
}
}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < particleArray.length; i++) {
particleArray[i].update();
particleArray[i].draw();
}
requestAnimationFrame(animate);
}
// Change text on click
window.addEventListener('mousedown', function() {
wordIndex = (wordIndex + 1) % words.length;
init(words[wordIndex]);
});
// Resize handler
window.addEventListener('resize', function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
init(words[wordIndex]);
});
init(words[wordIndex]);
animate();
</script>
</body>
</html>