-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-fractal-art-20260321-140245.html
More file actions
406 lines (343 loc) · 12.9 KB
/
Copy pathai-fractal-art-20260321-140245.html
File metadata and controls
406 lines (343 loc) · 12.9 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>分形艺术 - Fractal Art</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
overflow: hidden;
background: #000;
font-family: 'Segoe UI', system-ui, sans-serif;
min-height: 100vh;
}
canvas {
display: block;
position: fixed;
top: 0;
left: 0;
}
.info {
position: fixed;
bottom: 20px;
left: 20px;
color: rgba(255, 255, 255, 0.8);
font-size: 14px;
z-index: 100;
pointer-events: none;
text-shadow: 0 0 10px rgba(150, 100, 255, 0.5);
}
.info span {
display: block;
margin: 5px 0;
}
.fps-counter {
position: fixed;
top: 20px;
right: 20px;
color: rgba(255, 255, 255, 0.6);
font-size: 12px;
font-family: monospace;
z-index: 100;
}
</style>
</head>
<body>
<canvas id="fractal"></canvas>
<div class="info">
<span>🔮 分形艺术</span>
<span>🖱️ 移动鼠标 - 变换分形参数</span>
<span>👆 点击 - 切换分形类型</span>
</div>
<div class="fps-counter" id="fps">FPS: 60</div>
<script>
// ============ 配置 ============
const CONFIG = {
maxIterations: 100,
zoomSpeed: 0.001,
colorSchemes: [
['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7'],
['#667EEA', '#764BA2', '#F093FB', '#F5576C', '#4FACFE'],
['#FA709A', '#FEE140', '#00C9FF', '#92FE9D', '#FAD961'],
['#A8E6CF', '#DCEDC1', '#FFD3B6', '#FFAAA5', '#FF8B94'],
['#6B5B95', '#88B04B', '#F7CAC9', '#92A8D1', '#955251']
]
};
// ============ Canvas 设置 ============
const canvas = document.getElementById('fractal');
const ctx = canvas.getContext('2d');
let width, height, imageData, pixels;
function resize() {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
imageData = ctx.createImageData(width, height);
pixels = imageData.data;
}
resize();
window.addEventListener('resize', resize);
// ============ 工具函数 ============
function random(min, max) {
return Math.random() * (max - min) + min;
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
function hslToRgb(h, s, l) {
let r, g, b;
if (s === 0) {
r = g = b = l;
} else {
const hue2rgb = (p, q, t) => {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
};
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
// ============ 分形状态 ============
let fractalType = 0; // 0: Mandelbrot, 1: Julia, 2: Burning Ship
let centerX = -0.5;
let centerY = 0;
let zoom = 1;
let colorScheme = 0;
let mouseX = width / 2;
let mouseY = height / 2;
let time = 0;
// Julia 集参数
let juliaC = { x: -0.7, y: 0.27015 };
// ============ 分形计算函数 ============
function mandelbrot(x0, y0, maxIter) {
let x = 0, y = 0;
let iter = 0;
while (x * x + y * y <= 4 && iter < maxIter) {
const xTemp = x * x - y * y + x0;
y = 2 * x * y + y0;
x = xTemp;
iter++;
}
if (iter === maxIter) return 0;
// 平滑着色
const log_zn = Math.log(x * x + y * y) / 2;
const nu = Math.log(log_zn / Math.log(2)) / Math.log(2);
return iter + 1 - nu;
}
function julia(x0, y0, maxIter) {
let x = x0, y = y0;
let iter = 0;
while (x * x + y * y <= 4 && iter < maxIter) {
const xTemp = x * x - y * y + juliaC.x;
y = 2 * x * y + juliaC.y;
x = xTemp;
iter++;
}
if (iter === maxIter) return 0;
const log_zn = Math.log(x * x + y * y) / 2;
const nu = Math.log(log_zn / Math.log(2)) / Math.log(2);
return iter + 1 - nu;
}
function burningShip(x0, y0, maxIter) {
let x = 0, y = 0;
let iter = 0;
while (x * x + y * y <= 4 && iter < maxIter) {
const xTemp = x * x - y * y + x0;
y = Math.abs(2 * x * y) + y0;
x = Math.abs(xTemp);
iter++;
}
if (iter === maxIter) return 0;
return iter;
}
// ============ 渲染分形 ============
function renderFractal() {
const aspectRatio = width / height;
const scale = 3 / zoom;
const colors = CONFIG.colorSchemes[colorScheme];
for (let py = 0; py < height; py++) {
for (let px = 0; px < width; px++) {
const x0 = centerX + (px - width / 2) / width * scale * aspectRatio;
const y0 = centerY + (py - height / 2) / height * scale;
let iter;
switch (fractalType) {
case 0: iter = mandelbrot(x0, y0, CONFIG.maxIterations); break;
case 1: iter = julia(x0, y0, CONFIG.maxIterations); break;
case 2: iter = burningShip(x0, y0, CONFIG.maxIterations); break;
default: iter = mandelbrot(x0, y0, CONFIG.maxIterations);
}
const idx = (py * width + px) * 4;
if (iter === 0) {
pixels[idx] = 0;
pixels[idx + 1] = 0;
pixels[idx + 2] = 0;
pixels[idx + 3] = 255;
} else {
// 使用 HSL 着色
const hue = (iter / CONFIG.maxIterations + time * 0.0001) % 1;
const saturation = 0.8;
const lightness = 0.5 + 0.2 * Math.sin(iter * 0.1);
const [r, g, b] = hslToRgb(hue, saturation, lightness);
pixels[idx] = r;
pixels[idx + 1] = g;
pixels[idx + 2] = b;
pixels[idx + 3] = 255;
}
}
}
ctx.putImageData(imageData, 0, 0);
}
// ============ 粒子效果 ============
class Particle {
constructor(x, y) {
this.x = x;
this.y = y;
const angle = random(0, Math.PI * 2);
const speed = random(1, 4);
this.vx = Math.cos(angle) * speed;
this.vy = Math.sin(angle) * speed;
this.size = random(2, 5);
this.life = 1;
this.decay = random(0.01, 0.03);
this.hue = random(0, 1);
}
update() {
this.x += this.vx;
this.y += this.vy;
this.vx *= 0.98;
this.vy *= 0.98;
this.life -= this.decay;
}
draw(ctx) {
if (this.life <= 0) return;
const [r, g, b] = hslToRgb(this.hue, 0.8, 0.6);
ctx.beginPath();
ctx.arc(this.x, this.y, this.size * this.life, 0, Math.PI * 2);
ctx.fillStyle = `rgba(${r}, ${g}, ${b}, ${this.life})`;
ctx.fill();
}
isDead() {
return this.life <= 0;
}
}
const particles = [];
// ============ 鼠标交互 ============
canvas.addEventListener('mousemove', (e) => {
mouseX = e.clientX;
mouseY = e.clientY;
// 根据鼠标位置调整分形参数
if (fractalType === 1) {
juliaC.x = (mouseX / width - 0.5) * 2;
juliaC.y = (mouseY / height - 0.5) * 2;
}
// 添加粒子轨迹
if (Math.random() < 0.3) {
particles.push(new Particle(mouseX, mouseY));
}
});
canvas.addEventListener('click', () => {
// 切换分形类型
fractalType = (fractalType + 1) % 3;
// 重置视图
switch (fractalType) {
case 0:
centerX = -0.5;
centerY = 0;
break;
case 1:
centerX = 0;
centerY = 0;
juliaC = { x: -0.7, y: 0.27015 };
break;
case 2:
centerX = -0.5;
centerY = -0.5;
break;
}
// 切换配色
colorScheme = (colorScheme + 1) % CONFIG.colorSchemes.length;
// 爆发粒子
for (let i = 0; i < 30; i++) {
particles.push(new Particle(width / 2, height / 2));
}
});
// 滚轮缩放
canvas.addEventListener('wheel', (e) => {
e.preventDefault();
const zoomFactor = e.deltaY > 0 ? 0.9 : 1.1;
zoom *= zoomFactor;
// 向鼠标位置缩放
const dx = (mouseX - width / 2) / width;
const dy = (mouseY - height / 2) / height;
centerX += dx * 0.1 / zoom;
centerY += dy * 0.1 / zoom;
});
// ============ FPS 计算 ============
let frameCount = 0;
let lastFpsTime = performance.now();
let fps = 60;
let lastRenderTime = 0;
const renderInterval = 50; // 降低渲染频率以提高性能
function updateFPS() {
frameCount++;
const now = performance.now();
if (now - lastFpsTime >= 1000) {
fps = frameCount;
frameCount = 0;
lastFpsTime = now;
document.getElementById('fps').textContent = `FPS: ${fps}`;
}
}
// ============ 主动画循环 ============
function animate(timestamp) {
time = timestamp;
// 降低分形渲染频率
if (timestamp - lastRenderTime > renderInterval) {
renderFractal();
lastRenderTime = timestamp;
}
// 自动缩放
// zoom *= 1.001;
// 更新和绘制粒子
for (let i = particles.length - 1; i >= 0; i--) {
particles[i].update();
particles[i].draw(ctx);
if (particles[i].isDead()) {
particles.splice(i, 1);
}
}
// 限制粒子数量
if (particles.length > 100) {
particles.splice(0, particles.length - 100);
}
// 绘制分形类型指示
const names = ['Mandelbrot Set', 'Julia Set', 'Burning Ship'];
ctx.fillStyle = 'rgba(255, 255, 255, 0.5)';
ctx.font = '14px monospace';
ctx.fillText(names[fractalType], 10, 30);
ctx.fillText(`Zoom: ${zoom.toFixed(2)}x`, 10, 50);
updateFPS();
requestAnimationFrame(animate);
}
// 启动动画
animate(0);
// ============ 性能优化 ============
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
// 页面隐藏时可以暂停
}
});
</script>
</body>
</html>