|
| 1 | +/** @jsx jsx */ |
| 2 | +import { jsx, get, useThemeUI } from 'theme-ui' |
| 3 | +import { useState, useEffect, Fragment } from 'react' |
| 4 | +import merge from 'deepmerge' |
| 5 | +import Logo from './logo' |
| 6 | + |
| 7 | +const colors = ['background', 'primary', 'secondary', 'accent'] |
| 8 | + |
| 9 | +const initialState = { |
| 10 | + 0: { |
| 11 | + 0: 0, |
| 12 | + 1: 1, |
| 13 | + 2: 1, |
| 14 | + 3: 2, |
| 15 | + 4: 2, |
| 16 | + 5: 0, |
| 17 | + 6: 0, |
| 18 | + 7: 1, |
| 19 | + 8: 3, |
| 20 | + 9: 2, |
| 21 | + }, |
| 22 | + 1: { |
| 23 | + 0: 1, |
| 24 | + 1: 1, |
| 25 | + 2: 1, |
| 26 | + 3: 0, |
| 27 | + 4: 1, |
| 28 | + 5: 1, |
| 29 | + 6: 1, |
| 30 | + 7: 3, |
| 31 | + 8: 3, |
| 32 | + 9: 2, |
| 33 | + }, |
| 34 | + 2: { |
| 35 | + 0: 0, |
| 36 | + 1: 1, |
| 37 | + 2: 0, |
| 38 | + 3: 1, |
| 39 | + 4: 1, |
| 40 | + 5: 1, |
| 41 | + 6: 1, |
| 42 | + 7: 3, |
| 43 | + 8: 0, |
| 44 | + 9: 2, |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +const Node = ({ x, y, color = 0, ...props }) => { |
| 49 | + const inset = !!(y % 2) |
| 50 | + |
| 51 | + // adjust for logo position |
| 52 | + const isLogo = y === 2 && x === 0 |
| 53 | + |
| 54 | + return ( |
| 55 | + <circle |
| 56 | + {...props} |
| 57 | + cx={x * 3 + (inset ? 2.5 : 1)} |
| 58 | + cy={y * 3 + 1} |
| 59 | + r={1} |
| 60 | + fill="currentcolor" |
| 61 | + strokeWidth={1 / 8} |
| 62 | + sx={{ |
| 63 | + color: isLogo ? 'background' : colors[color] || 'background', |
| 64 | + transitionProperty: 'stroke, color', |
| 65 | + transitionTimingFunction: 'ease-out', |
| 66 | + transitionDuration: '.4s', |
| 67 | + '&:hover': { |
| 68 | + stroke: t => t.colors.highlight, |
| 69 | + }, |
| 70 | + }} |
| 71 | + /> |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +const Edges = ({ x, y, state }) => { |
| 76 | + const inset = !!(y % 2) |
| 77 | + const transform = `translate(${x * 3 + (inset ? 1 : 0)} ${y * 3})` |
| 78 | + const angles = [] |
| 79 | + const active = state[y] && state[y][x] |
| 80 | + if (!active) return false |
| 81 | + const start = `M ${inset ? 1.5 : 1} 1` |
| 82 | + const paths = [`${start} h3`, `${start} l1.5 3`, `${start} l-1.5 3`] |
| 83 | + |
| 84 | + if (state[y][x + 1]) { |
| 85 | + angles.push(0) |
| 86 | + } |
| 87 | + if (inset) { |
| 88 | + if (state[y + 1] && state[y + 1][x]) { |
| 89 | + angles.push(2) |
| 90 | + } |
| 91 | + if (state[y + 1] && state[y + 1][x + 1]) { |
| 92 | + angles.push(1) |
| 93 | + } |
| 94 | + } else { |
| 95 | + if (state[y + 1] && state[y + 1][x]) { |
| 96 | + angles.push(1) |
| 97 | + } |
| 98 | + if (state[y + 1] && state[y + 1][x - 1]) { |
| 99 | + angles.push(2) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return ( |
| 104 | + <g transform={transform}> |
| 105 | + {angles.map(n => ( |
| 106 | + <path |
| 107 | + key={n} |
| 108 | + d={paths[n]} |
| 109 | + fill="none" |
| 110 | + stroke="currentcolor" |
| 111 | + strokeWidth={1 / 4} |
| 112 | + sx={{ |
| 113 | + color: colors[active], |
| 114 | + transitionProperty: 'stroke, color', |
| 115 | + transitionTimingFunction: 'ease-out', |
| 116 | + transitionDuration: '.4s', |
| 117 | + }} |
| 118 | + /> |
| 119 | + ))} |
| 120 | + </g> |
| 121 | + ) |
| 122 | +} |
| 123 | + |
| 124 | +const rand = l => Math.floor(Math.random() * (l + 1)) |
| 125 | +const randomizeColors = state => { |
| 126 | + const color = rand(colors.length - 1) |
| 127 | + return merge(state, { |
| 128 | + [rand(2)]: { |
| 129 | + [rand(9)]: color, |
| 130 | + }, |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +export default ({ width = 32, height = 9, scale = 32 }) => { |
| 135 | + const { theme } = useThemeUI() |
| 136 | + const rows = Array.from({ length: height / 3 }).map((n, y) => |
| 137 | + Array.from({ |
| 138 | + length: Math.floor(width / 3 + (y % 2 ? 0 : 1)), |
| 139 | + }).map((o, x) => ({ x, y })) |
| 140 | + ) |
| 141 | + const [state, setState] = useState(initialState) |
| 142 | + const viewBox = `0 0 ${width} ${height}` |
| 143 | + const dimensions = { |
| 144 | + width: width * scale, |
| 145 | + height: height * scale, |
| 146 | + } |
| 147 | + |
| 148 | + useEffect(() => { |
| 149 | + const tick = () => { |
| 150 | + setState(randomizeColors) |
| 151 | + } |
| 152 | + const id = setInterval(tick, 600) |
| 153 | + return () => { |
| 154 | + clearInterval(id) |
| 155 | + } |
| 156 | + }, []) |
| 157 | + |
| 158 | + const handleClick = ({ x, y }) => e => { |
| 159 | + const i = get(state, [y, x].join('.'), 0) |
| 160 | + const n = (i + 1) % colors.length |
| 161 | + setState(s => |
| 162 | + merge(s, { |
| 163 | + [y]: { |
| 164 | + [x]: n, |
| 165 | + }, |
| 166 | + }) |
| 167 | + ) |
| 168 | + } |
| 169 | + |
| 170 | + const logo = {} |
| 171 | + logo.key = get(colors, get(state, '2.0')) |
| 172 | + if (logo.key !== 'background') { |
| 173 | + logo.color = get(theme.colors, logo.key) |
| 174 | + } else { |
| 175 | + logo.color = get(theme.colors, 'text') |
| 176 | + } |
| 177 | + |
| 178 | + return ( |
| 179 | + <svg |
| 180 | + xmlns="http://www.w3.org/2000/svg" |
| 181 | + viewBox={`0 0 ${width} ${height}`} |
| 182 | + width={width * scale} |
| 183 | + height={height * scale} |
| 184 | + sx={{ |
| 185 | + maxWidth: '100%', |
| 186 | + width: '100%', |
| 187 | + height: 'auto', |
| 188 | + overflow: 'visible', |
| 189 | + userSelect: 'none', |
| 190 | + }}> |
| 191 | + <rect width={width} height={height} fill="none" /> |
| 192 | + {rows.map(row => |
| 193 | + row.map(({ x, y }) => ( |
| 194 | + <g key={x + y}> |
| 195 | + <Edges x={x} y={y} state={state} /> |
| 196 | + <Node |
| 197 | + x={x} |
| 198 | + y={y} |
| 199 | + color={get(state, [y, x].join('.'), 0)} |
| 200 | + onClick={handleClick({ x, y })} |
| 201 | + /> |
| 202 | + </g> |
| 203 | + )) |
| 204 | + )} |
| 205 | + <g transform="translate(0 6)"> |
| 206 | + <Logo size={2} color={logo.color} /> |
| 207 | + </g> |
| 208 | + </svg> |
| 209 | + ) |
| 210 | +} |
0 commit comments