|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { Rect, Line, Text, Group } from 'react-konva'; |
| 3 | +import onecolor from 'onecolor'; |
| 4 | + |
| 5 | +const GRID_CELL_COUNT = 5; |
| 6 | +const GRID_LINES = [...new Array(GRID_CELL_COUNT + 1).keys()].map( |
| 7 | + (i) => (i - GRID_CELL_COUNT * 0.5) / (GRID_CELL_COUNT * 0.5) |
| 8 | +); |
| 9 | + |
| 10 | +const TestScroll = React.memo(({ width, height }) => { |
| 11 | + const [rows, setRows] = useState([0]); |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + const intervalId = window.setInterval(() => { |
| 15 | + setRows((prevRows) => { |
| 16 | + const newRows = |
| 17 | + Math.random() < 0.3 |
| 18 | + ? [...prevRows, Math.random() * 0.3].slice(-25) |
| 19 | + : [...prevRows]; |
| 20 | + |
| 21 | + newRows[newRows.length - 1] = Math.min( |
| 22 | + 1, |
| 23 | + newRows[newRows.length - 1] + Math.random() * 0.15 |
| 24 | + ); |
| 25 | + |
| 26 | + return newRows; |
| 27 | + }); |
| 28 | + }, 50); |
| 29 | + |
| 30 | + return () => { |
| 31 | + window.clearInterval(intervalId); |
| 32 | + }; |
| 33 | + }, []); |
| 34 | + |
| 35 | + return ( |
| 36 | + <Group x={15} y={height - 15 - rows.length * 3}> |
| 37 | + {rows.map((amount, index) => { |
| 38 | + return ( |
| 39 | + <Rect |
| 40 | + key={index} |
| 41 | + x={0} |
| 42 | + y={index * 3} |
| 43 | + width={Math.round(amount * 100)} |
| 44 | + height={2} |
| 45 | + fill="#80ff80" |
| 46 | + /> |
| 47 | + ); |
| 48 | + })} |
| 49 | + </Group> |
| 50 | + ); |
| 51 | +}); |
| 52 | + |
| 53 | +export const TestKonvaContent = ({ width, height }) => { |
| 54 | + const [blink, setBlink] = useState(false); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + const intervalId = window.setInterval(() => { |
| 58 | + setBlink((prev) => !prev); |
| 59 | + }, 800); |
| 60 | + |
| 61 | + return () => { |
| 62 | + window.clearInterval(intervalId); |
| 63 | + }; |
| 64 | + }, []); |
| 65 | + |
| 66 | + const [angleDeg, setAngleDeg] = useState(0); |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + const intervalId = window.setInterval(() => { |
| 70 | + setAngleDeg((prevAngleDeg) => (prevAngleDeg + 1.5) % 360); |
| 71 | + }, 50); |
| 72 | + |
| 73 | + return () => { |
| 74 | + window.clearInterval(intervalId); |
| 75 | + }; |
| 76 | + }, []); |
| 77 | + |
| 78 | + const cx = width / 2, |
| 79 | + cy = height / 2; |
| 80 | + const currentColor = onecolor(['HSL', angleDeg / 360, 1, 0.5]).hex(); |
| 81 | + const angle = (Math.PI * angleDeg) / 180; |
| 82 | + const acos = Math.cos(angle); |
| 83 | + const asin = Math.sin(angle); |
| 84 | + |
| 85 | + const outA = [0, 0]; |
| 86 | + const outB = [0, 0]; |
| 87 | + function computeXYZ(gx, gy, out) { |
| 88 | + const wx = gx * acos - gy * asin; |
| 89 | + const wy = gx * asin + gy * acos; |
| 90 | + |
| 91 | + const dz = wy + 6; |
| 92 | + const w = 1 / dz; |
| 93 | + out[0] = wx * (width / 2) * w; |
| 94 | + out[1] = -(-2.5 * (width / 2) * w) - 30; |
| 95 | + } |
| 96 | + |
| 97 | + return ( |
| 98 | + <> |
| 99 | + {GRID_LINES.map((gx) => { |
| 100 | + computeXYZ(2 * gx, -2, outA); |
| 101 | + computeXYZ(2 * gx, 2, outB); |
| 102 | + |
| 103 | + return ( |
| 104 | + <Line |
| 105 | + key={gx} |
| 106 | + points={[outA[0], outA[1], outB[0], outB[1]]} |
| 107 | + strokeWidth={1} |
| 108 | + x={width * 0.65} |
| 109 | + y={height * 0.6} |
| 110 | + stroke={currentColor} |
| 111 | + /> |
| 112 | + ); |
| 113 | + })} |
| 114 | + |
| 115 | + {GRID_LINES.map((gy) => { |
| 116 | + computeXYZ(-2, 2 * gy, outA); |
| 117 | + computeXYZ(2, 2 * gy, outB); |
| 118 | + |
| 119 | + return ( |
| 120 | + <Line |
| 121 | + key={gy} |
| 122 | + points={[outA[0], outA[1], outB[0], outB[1]]} |
| 123 | + strokeWidth={1} |
| 124 | + x={width * 0.65} |
| 125 | + y={height * 0.6} |
| 126 | + stroke={currentColor} |
| 127 | + /> |
| 128 | + ); |
| 129 | + })} |
| 130 | + |
| 131 | + <Rect |
| 132 | + x={2.5} |
| 133 | + y={22.5} |
| 134 | + width={width - 5} |
| 135 | + height={height - 25} |
| 136 | + stroke="#00ffff" |
| 137 | + strokeWidth={1} |
| 138 | + /> |
| 139 | + <Rect |
| 140 | + x={4.5} |
| 141 | + y={24.5} |
| 142 | + width={width - 9} |
| 143 | + height={height - 29} |
| 144 | + stroke="#00ffff" |
| 145 | + strokeWidth={1} |
| 146 | + /> |
| 147 | + |
| 148 | + {blink && ( |
| 149 | + <Text |
| 150 | + x={2} |
| 151 | + y={2} |
| 152 | + text="SYNTHESIS LEVEL 1" |
| 153 | + fontSize={16} |
| 154 | + fontFamily="Inconsolata" |
| 155 | + fill="#ffff00" |
| 156 | + /> |
| 157 | + )} |
| 158 | + |
| 159 | + <Text |
| 160 | + x={15} |
| 161 | + y={36} |
| 162 | + text={`${Math.round(acos * 1000) / 1000}`} |
| 163 | + fontSize={10} |
| 164 | + fontFamily="Inconsolata" |
| 165 | + fill="#ff0000" |
| 166 | + /> |
| 167 | + |
| 168 | + <Text |
| 169 | + x={15} |
| 170 | + y={44} |
| 171 | + text={`${Math.round(asin * 1000) / 1000}`} |
| 172 | + fontSize={10} |
| 173 | + fontFamily="Inconsolata" |
| 174 | + fill="#ff0000" |
| 175 | + /> |
| 176 | + |
| 177 | + <TestScroll width={width} height={height} /> |
| 178 | + </> |
| 179 | + ); |
| 180 | +}; |
0 commit comments