-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
205 lines (181 loc) · 6.72 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>曼德勃罗集演示</title>
<link rel="icon" href="symbol.png" type="image/png">
<style>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: black;
overflow: hidden;
}
canvas {
cursor: grab;
width: 100vw;
height: 100vh;
}
canvas:active {
cursor: grabbing;
}
#controlsContainer {
position: fixed;
top: 10px;
left: 10px;
background-color: rgba(255, 255, 255, 0.7);
padding: 10px;
border-radius: 5px;
font-family: sans-serif;
z-index: 100;
}
#iterationsLabel {
margin-right: 10px;
}
#iterationsSlider {
width: 200px;
}
#colorPickerLabel {
margin-right: 10px;
}
</style>
</head>
<body>
<canvas id="mandelbrotCanvas"></canvas>
<div id="controlsContainer">
<div>
<label id="iterationsLabel">迭代次数: 100</label>
<input type="range" id="iterationsSlider" min="50" max="1000" value="100" step="10">
</div>
<div>
<label id="colorPickerLabel">颜色: </label>
<input type="color" id="colorPicker" value="#ffffff">
</div>
</div>
<script>
const canvas = document.getElementById('mandelbrotCanvas');
const ctx = canvas.getContext('2d');
const iterationsLabel = document.getElementById('iterationsLabel');
const iterationsSlider = document.getElementById('iterationsSlider');
const colorPicker = document.getElementById('colorPicker');
let maxIterations = parseInt(iterationsSlider.value);
let zoom = 250;
let offsetX = 0;
let offsetY = 0;
let outerColor = colorPicker.value;
let isDragging = false;
let startX, startY;
// 根据窗口调整画布大小
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
drawMandelbrot();
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// 将屏幕坐标转换为复平面坐标
function mandelbrot(cx, cy) {
let x = 0, y = 0;
let iteration = 0;
while (x * x + y * y <= 4 && iteration < maxIterations) {
let xTemp = x * x - y * y + cx;
y = 2 * x * y + cy;
x = xTemp;
iteration++;
}
return iteration;
}
// 绘制曼德勃罗集
function drawMandelbrot() {
const width = canvas.width;
const height = canvas.height;
const imageData = ctx.createImageData(width, height);
const pixels = imageData.data;
const rgbColor = hexToRgb(outerColor);
for (let py = 0; py < height; py++) {
for (let px = 0; px < width; px++) {
const cx = (px - width / 2 + offsetX) / zoom;
const cy = (py - height / 2 + offsetY) / zoom;
const iteration = mandelbrot(cx, cy);
const pixelIndex = (py * width + px) * 4;
if (iteration === maxIterations) {
pixels[pixelIndex] = 0;
pixels[pixelIndex + 1] = 0;
pixels[pixelIndex + 2] = 0;
pixels[pixelIndex + 3] = 255;
} else {
const colorFactor = iteration / maxIterations;
pixels[pixelIndex] = rgbColor.r * colorFactor;
pixels[pixelIndex + 1] = rgbColor.g * colorFactor;
pixels[pixelIndex + 2] = rgbColor.b * colorFactor;
pixels[pixelIndex + 3] = 255; // Alpha
}
}
}
ctx.putImageData(imageData, 0, 0);
}
function hexToRgb(hex) {
const bigint = parseInt(hex.slice(1), 16);
return {
r: (bigint >> 16) & 255,
g: (bigint >> 8) & 255,
b: bigint & 255
};
}
// 缩放
function zoomMandelbrot(event) {
const zoomFactor = event.deltaY < 0 ? 1.1 : 1 / 1.1;
const mouseX = event.offsetX;
const mouseY = event.offsetY;
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const mousePosX = (mouseX - canvasWidth / 2) / zoom + offsetX / zoom;
const mousePosY = (mouseY - canvasHeight / 2) / zoom + offsetY / zoom;
zoom *= zoomFactor;
offsetX = mousePosX * zoom - (mouseX - canvasWidth / 2);
offsetY = mousePosY * zoom - (mouseY - canvasHeight / 2);
drawMandelbrot();
}
// 鼠标拖动
function startDrag(event) {
isDragging = true;
startX = event.clientX;
startY = event.clientY;
}
function dragMandelbrot(event) {
if (isDragging) {
const deltaX = startX - event.clientX;
const deltaY = startY - event.clientY;
offsetX += deltaX;
offsetY += deltaY;
startX = event.clientX;
startY = event.clientY;
drawMandelbrot();
}
}
function stopDrag() {
isDragging = false;
}
canvas.addEventListener('wheel', zoomMandelbrot);
canvas.addEventListener('mousedown', startDrag);
canvas.addEventListener('mousemove', dragMandelbrot);
canvas.addEventListener('mouseup', stopDrag);
canvas.addEventListener('mouseleave', stopDrag);
iterationsSlider.addEventListener('input', function() {
maxIterations = parseInt(iterationsSlider.value);
iterationsLabel.textContent = `Iterations: ${maxIterations}`;
drawMandelbrot();
});
// 颜色选择
colorPicker.addEventListener('input', function() {
outerColor = colorPicker.value;
drawMandelbrot();
});
// 初始化
drawMandelbrot();
</script>
</body>
</html>