|
| 1 | +import React, { useMemo } from "react"; |
| 2 | + |
| 3 | +interface CraterProps { |
| 4 | + size?: number; |
| 5 | + intensity?: number; |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * Generates an irregular crater polygon |
| 10 | + */ |
| 11 | +function generateCraterShape(baseRadius: number, points: number = 10): string { |
| 12 | + const coords: string[] = []; |
| 13 | + |
| 14 | + for (let i = 0; i < points; i++) { |
| 15 | + const angle = (i / points) * Math.PI * 2; |
| 16 | + const radius = baseRadius * (0.75 + Math.random() * 0.3); |
| 17 | + const x = 50 + Math.cos(angle) * radius; |
| 18 | + const y = 50 + Math.sin(angle) * radius; |
| 19 | + coords.push(`${x.toFixed(1)},${y.toFixed(1)}`); |
| 20 | + } |
| 21 | + |
| 22 | + return coords.join(" "); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Generates a crack as a polygon shape (actual gap in ground) |
| 27 | + * Returns points for a wedge-shaped fissure |
| 28 | + */ |
| 29 | +function generateCrackFissure( |
| 30 | + angle: number, |
| 31 | + startRadius: number, |
| 32 | + length: number, |
| 33 | +): string { |
| 34 | + // Crack is a tapered wedge shape - wide at crater, sharp point at end |
| 35 | + const widthAtStart = 7 + Math.random() * 5; // Bit skinnier (7-12) |
| 36 | + const widthAtEnd = 0.5 + Math.random() * 1; // Sharp point at tip (0.5-1.5) |
| 37 | + |
| 38 | + // Calculate perpendicular angle for width |
| 39 | + const perpAngle = angle + Math.PI / 2; |
| 40 | + |
| 41 | + // Points along the crack with some jaggedness |
| 42 | + const segments = 3; |
| 43 | + const points: Array<{ x: number; y: number }> = []; |
| 44 | + const pointsBack: Array<{ x: number; y: number }> = []; |
| 45 | + |
| 46 | + let currentAngle = angle; |
| 47 | + |
| 48 | + for (let i = 0; i <= segments; i++) { |
| 49 | + const t = i / segments; |
| 50 | + // Start from inside crater and extend outward |
| 51 | + const radius = startRadius + length * t; |
| 52 | + const width = widthAtStart + (widthAtEnd - widthAtStart) * t; |
| 53 | + |
| 54 | + // Add jaggedness to angle |
| 55 | + if (i > 0 && i < segments) { |
| 56 | + currentAngle += (Math.random() - 0.5) * 0.3; |
| 57 | + } |
| 58 | + |
| 59 | + const centerX = 50 + Math.cos(currentAngle) * radius; |
| 60 | + const centerY = 50 + Math.sin(currentAngle) * radius; |
| 61 | + |
| 62 | + // Offset perpendicular for width |
| 63 | + const offsetX = (Math.cos(perpAngle) * width) / 2; |
| 64 | + const offsetY = (Math.sin(perpAngle) * width) / 2; |
| 65 | + |
| 66 | + // Add random jaggedness to edges - less at the tip for sharp point |
| 67 | + const jagAmount = 4 * (1 - t * 0.8); // More jagged at start, smooth at tip |
| 68 | + const jag1 = (Math.random() - 0.5) * jagAmount; |
| 69 | + const jag2 = (Math.random() - 0.5) * jagAmount; |
| 70 | + |
| 71 | + points.push({ |
| 72 | + x: centerX + offsetX + jag1, |
| 73 | + y: centerY + offsetY + jag1, |
| 74 | + }); |
| 75 | + pointsBack.unshift({ |
| 76 | + x: centerX - offsetX + jag2, |
| 77 | + y: centerY - offsetY + jag2, |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + // Combine into polygon |
| 82 | + const allPoints = [...points, ...pointsBack]; |
| 83 | + return allPoints.map((p) => `${p.x.toFixed(1)},${p.y.toFixed(1)}`).join(" "); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Crater with fissure-style cracks (actual gaps, not lines) |
| 88 | + */ |
| 89 | +export const Crater = React.memo(function Crater({ |
| 90 | + size = 100, |
| 91 | + intensity = 1, |
| 92 | +}: CraterProps) { |
| 93 | + const uniqueId = useMemo(() => Math.random().toString(36).substr(2, 9), []); |
| 94 | + |
| 95 | + // Crater shapes |
| 96 | + const outerCrater = useMemo(() => generateCraterShape(22, 12), []); |
| 97 | + const innerCrater = useMemo(() => generateCraterShape(14, 10), []); |
| 98 | + const deepCrater = useMemo(() => generateCraterShape(8, 8), []); |
| 99 | + |
| 100 | + // Generate 2-4 crack fissures (reduced for performance) |
| 101 | + const fissures = useMemo(() => { |
| 102 | + const count = 2 + Math.floor(Math.random() * 3); |
| 103 | + const result: Array<{ points: string }> = []; |
| 104 | + |
| 105 | + for (let i = 0; i < count; i++) { |
| 106 | + const baseAngle = (i / count) * Math.PI * 2; |
| 107 | + const angle = baseAngle + (Math.random() - 0.5) * 0.8; |
| 108 | + const length = 20 + Math.random() * 18; |
| 109 | + |
| 110 | + result.push({ |
| 111 | + points: generateCrackFissure(angle, 14, length), |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + return result; |
| 116 | + }, []); |
| 117 | + |
| 118 | + // Colors |
| 119 | + const voidColor = `rgba(0, 0, 0, ${intensity})`; |
| 120 | + const deepColor = `rgba(8, 5, 2, ${0.95 * intensity})`; |
| 121 | + const craterColor = `rgba(20, 15, 8, ${0.9 * intensity})`; |
| 122 | + const rimColor = `rgba(45, 35, 25, ${0.7 * intensity})`; |
| 123 | + |
| 124 | + return ( |
| 125 | + <svg |
| 126 | + width={size} |
| 127 | + height={size} |
| 128 | + viewBox="0 0 100 100" |
| 129 | + style={{ overflow: "visible", contain: "layout style" }} |
| 130 | + > |
| 131 | + <defs> |
| 132 | + <radialGradient |
| 133 | + id={`crater-grad-${uniqueId}`} |
| 134 | + cx="40%" |
| 135 | + cy="40%" |
| 136 | + r="60%" |
| 137 | + > |
| 138 | + <stop offset="0%" stopColor={voidColor} /> |
| 139 | + <stop offset="50%" stopColor={deepColor} /> |
| 140 | + <stop offset="80%" stopColor={craterColor} /> |
| 141 | + <stop offset="100%" stopColor={rimColor} /> |
| 142 | + </radialGradient> |
| 143 | + </defs> |
| 144 | + |
| 145 | + {/* Crack fissures - simplified to single polygon each */} |
| 146 | + {fissures.map((fissure, i) => ( |
| 147 | + <polygon key={i} points={fissure.points} fill={voidColor} /> |
| 148 | + ))} |
| 149 | + |
| 150 | + {/* Outer crater rim */} |
| 151 | + <polygon points={outerCrater} fill={rimColor} /> |
| 152 | + |
| 153 | + {/* Main crater */} |
| 154 | + <polygon points={outerCrater} fill={`url(#crater-grad-${uniqueId})`} /> |
| 155 | + |
| 156 | + {/* Inner crater layer */} |
| 157 | + <polygon points={innerCrater} fill={deepColor} /> |
| 158 | + |
| 159 | + {/* Deepest void */} |
| 160 | + <polygon points={deepCrater} fill={voidColor} /> |
| 161 | + |
| 162 | + {/* Crater rim edge */} |
| 163 | + <polygon |
| 164 | + points={outerCrater} |
| 165 | + fill="none" |
| 166 | + stroke="rgba(60, 50, 40, 0.4)" |
| 167 | + strokeWidth="1.5" |
| 168 | + strokeLinejoin="round" |
| 169 | + /> |
| 170 | + </svg> |
| 171 | + ); |
| 172 | +}); |
0 commit comments