class FractalCard extends HTMLElement {
connectedCallback() {
const seed = this.getAttribute('seed') || '42';
const color = this.getAttribute('color') || '#ff00ff';
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
`;
this.setupFractal(seed, color);
}
setupFractal(seed, color) {
const canvas = this.shadowRoot.querySelector('.fractal-canvas');
const ctx = canvas.getContext('2d');
const resizeCanvas = () => {
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
drawFractal();
};
const drawFractal = () => {
const width = canvas.width;
const height = canvas.height;
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, width, height);
// Generate fractal pattern based on seed
const centerX = width / 2;
const centerY = height / 2;
const maxRadius = Math.min(width, height) / 2;
for (let i = 0; i < 360; i += 0.5) {
const angle = i * Math.PI / 180;
const seedNum = parseInt(seed);
const radius = maxRadius * (0.5 + 0.3 * Math.sin(angle * seedNum * 0.01));
const x = centerX + radius * Math.cos(angle);
const y = centerY + radius * Math.sin(angle);
const hue = (i + seedNum * 10) % 360;
ctx.fillStyle = `hsla(${hue}, 70%, 50%, 0.8)`;
ctx.beginPath();
ctx.arc(x, y, 2, 0, Math.PI * 2);
ctx.fill();
}
// Add some recursive patterns
const drawRecursive = (x, y, size, depth) => {
if (depth === 0 || size < 2) return;
ctx.strokeStyle = color + '40';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI * 2);
ctx.stroke();
const newSize = size * 0.5;
const offset = size * 0.7;
drawRecursive(x - offset, y, newSize, depth - 1);
drawRecursive(x + offset, y, newSize, depth - 1);
drawRecursive(x, y - offset, newSize, depth - 1);
drawRecursive(x, y + offset, newSize, depth - 1);
};
drawRecursive(centerX, centerY, maxRadius * 0.3, 4);
};
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
}
}
customElements.define('fractal-card', FractalCard);