分形,这个充满魔力的词汇,来源于数学领域,却因其独特的形态美被广泛应用于艺术和设计中。在HTML5中,我们可以利用Canvas元素轻松绘制各种分形图案。本文将带领你从基础入门,逐步探索如何绘制从简单到复杂的分形图案。
一、分形简介
分形是一种复杂、无限嵌套的几何形状,它们在自然界中无处不在,如雪花、海岸线、山川等。分形的特点是局部与整体具有相似性,即“自相似性”。这种特性使得分形在计算机图形学中有着广泛的应用。
二、HTML5 Canvas简介
Canvas是HTML5中新增的一个功能,它允许开发者使用JavaScript在网页上绘制图形。Canvas元素提供了丰富的绘图API,包括绘制矩形、圆形、线条、文字等,还可以进行图像处理和动画制作。
三、绘制基础分形
1. 迷宫分形
迷宫分形是一种简单的分形图案,其特点是具有复杂而有趣的路径。下面是一个使用JavaScript和Canvas绘制迷宫分形的示例代码:
function drawMaze() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
const cellSize = 10;
// 绘制迷宫路径
for (let y = 0; y < height; y += cellSize) {
for (let x = 0; x < width; x += cellSize) {
if (Math.random() < 0.5) {
ctx.moveTo(x, y);
ctx.lineTo(x + cellSize, y);
}
}
}
}
drawMaze();
2. 龙卷风分形
龙卷风分形是一种具有螺旋结构的分形图案。下面是一个使用JavaScript和Canvas绘制龙卷风分形的示例代码:
function drawTornado() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
const center = { x: width / 2, y: height / 2 };
const angleStep = Math.PI / 100;
const radiusStep = 5;
// 绘制龙卷风路径
for (let angle = 0; angle < 2 * Math.PI; angle += angleStep) {
const radius = center.y + Math.sin(angle) * radiusStep;
ctx.beginPath();
ctx.arc(center.x, center.y, radius, angle, angle + angleStep);
ctx.stroke();
}
}
drawTornado();
四、绘制复杂分形
1. Mandelbrot集
Mandelbrot集是一种著名的分形图案,它描述了复平面上的一组迭代函数。下面是一个使用JavaScript和Canvas绘制Mandelbrot集的示例代码:
function drawMandelbrot() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
const zoom = 1;
const offsetX = -2.5;
const offsetY = -1;
// 计算Mandelbrot集
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const real = (x - width / 2) / (width / 4) * zoom + offsetX;
const imag = (y - height / 2) / (height / 4) * zoom + offsetY;
let zReal = real;
let zImag = imag;
let iterations = 0;
while (zReal * zReal + zImag * zImag < 4 && iterations < 1000) {
const temp = zReal * zReal - zImag * zImag + real;
zImag = 2 * zReal * zImag + imag;
zReal = temp;
iterations++;
}
const color = iterations < 1000 ? 255 - iterations * 2.5 : 0;
ctx.fillStyle = `rgb(${color}, ${color}, ${color})`;
ctx.fillRect(x, y, 1, 1);
}
}
}
drawMandelbrot();
2. Julia集
Julia集是Mandelbrot集的一种变体,它描述了复平面上的一组迭代函数。下面是一个使用JavaScript和Canvas绘制Julia集的示例代码:
function drawJulia() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
const zoom = 1;
const offsetX = -2.5;
const offsetY = -1;
const c = { x: -0.5, y: 0.5 };
// 计算Julia集
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const real = (x - width / 2) / (width / 4) * zoom + offsetX;
const imag = (y - height / 2) / (height / 4) * zoom + offsetY;
let zReal = real;
let zImag = imag;
let iterations = 0;
while (zReal * zReal + zImag * zImag < 4 && iterations < 1000) {
const temp = zReal * zReal - zImag * zImag + c.x;
zImag = 2 * zReal * zImag + c.y;
zReal = temp;
iterations++;
}
const color = iterations < 1000 ? 255 - iterations * 2.5 : 0;
ctx.fillStyle = `rgb(${color}, ${color}, ${color})`;
ctx.fillRect(x, y, 1, 1);
}
}
}
drawJulia();
五、总结
通过本文的介绍,相信你已经对HTML5绘制分形之美有了初步的了解。从基础到复杂的分形图案,都可以通过Canvas元素和JavaScript轻松实现。希望本文能激发你对分形艺术的兴趣,并在实践中不断探索和创造。
