Skip to content

Commit c91cedb

Browse files
committed
Zoom out preview when patterns extend out of initial box
1 parent 506a621 commit c91cedb

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

src/preview.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,44 @@ import { getBoundariesFromPattern } from './functions'
22
import { State } from './types'
33

44
export const Preview: React.FC<{ state: State }> = ({ state }) => {
5+
const viewBox = (() => {
6+
let xMin = 0
7+
let xMax = 1
8+
let yMin = 0
9+
let yMax = 1
10+
11+
// Make sure that default viewBox contains entire pattern
12+
for (const p of state.patterns) {
13+
const b = getBoundariesFromPattern(p)
14+
15+
xMin = Math.min(xMin, b.xMin)
16+
xMax = Math.max(xMax, b.xMax)
17+
yMin = Math.min(yMin, b.yMin)
18+
yMax = Math.max(yMax, b.yMax)
19+
}
20+
21+
// Add some spacing around the bounding box
22+
xMin -= 0.1
23+
xMax += 0.1
24+
yMin -= 0.1
25+
yMax += 0.1
26+
27+
return `${xMin} ${yMin} ${xMax - xMin} ${yMax - yMin}`
28+
})()
29+
530
return (
631
<div className='border-1 border-amber-300 aspect-square'>
7-
<svg viewBox='0 0 1 1' xmlns='http://www.w3.org/2000/svg' className='w-full aspect-square'>
32+
<svg viewBox={viewBox} xmlns='http://www.w3.org/2000/svg' className='w-full aspect-square'>
33+
<rect
34+
className='stroke-amber-100'
35+
vectorEffect='non-scaling-stroke'
36+
fill='none'
37+
strokeWidth='1px'
38+
x={0}
39+
y={0}
40+
width={1}
41+
height={1}
42+
/>
843
{state.patterns.map((p, i) => {
944
const { xMin, xMax, yMin, yMax } = getBoundariesFromPattern(p)
1045

0 commit comments

Comments
 (0)