Skip to content

Commit 3785add

Browse files
committed
Create component for partial line
1 parent 07daf51 commit 3785add

1 file changed

Lines changed: 32 additions & 21 deletions

File tree

src/preview.tsx

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import { getBoundariesFromPattern } from './functions'
3-
import { State } from './types'
3+
import { NumberPair, State } from './types'
44

55
function getViewBox(state: State): string {
66
let xMin = 0
@@ -27,6 +27,34 @@ function getViewBox(state: State): string {
2727
return `${xMin} ${yMin} ${xMax - xMin} ${yMax - yMin}`
2828
}
2929

30+
function lerp(a: number, b: number, k: number): number {
31+
return a + (b - a) * k
32+
}
33+
34+
const PartialLine: React.FC<{ anchor: NumberPair; target: NumberPair }> = ({ anchor, target }) => {
35+
const [anchorX, anchorY] = anchor
36+
const [targetX, targetY] = target
37+
38+
const k = 0.2
39+
40+
const x1 = anchorX
41+
const y1 = anchorY
42+
const x2 = lerp(anchorX, targetX, k)
43+
const y2 = lerp(anchorY, targetY, k)
44+
45+
return (
46+
<line
47+
className='stroke-amber-100'
48+
vectorEffect='non-scaling-stroke'
49+
strokeWidth='1px'
50+
x1={x1.toFixed(3)}
51+
y1={y1.toFixed(3)}
52+
x2={x2.toFixed(3)}
53+
y2={y2.toFixed(3)}
54+
/>
55+
)
56+
}
57+
3058
export const Preview: React.FC<{ state: State }> = ({ state }) => {
3159
return (
3260
<div className='border-b-1 border-amber-300 aspect-square'>
@@ -41,6 +69,8 @@ export const Preview: React.FC<{ state: State }> = ({ state }) => {
4169
width={1}
4270
height={1}
4371
/>
72+
<PartialLine anchor={[0, 0]} target={[1, 1]} />
73+
4474
{state.patterns.map((p, i) => {
4575
const { xMin, xMax, yMin, yMax } = getBoundariesFromPattern(p)
4676

@@ -60,27 +90,8 @@ export const Preview: React.FC<{ state: State }> = ({ state }) => {
6090
height={height.toFixed(3)}
6191
/>
6292
{/* Partial line from anchor to target in order to show the direction */}
63-
{(() => {
64-
const [anchorX, anchorY] = p.anchor
65-
const [targetX, targetY] = p.target
93+
<PartialLine anchor={p.anchor} target={p.target} />
6694

67-
const x1 = anchorX
68-
const y1 = anchorY
69-
const x2 = targetX - (targetX - anchorX) * 0.5
70-
const y2 = targetY - (targetY - anchorY) * 0.5
71-
72-
return (
73-
<line
74-
className='stroke-amber-100'
75-
vectorEffect='non-scaling-stroke'
76-
strokeWidth='1px'
77-
x1={x1.toFixed(3)}
78-
y1={y1.toFixed(3)}
79-
x2={x2.toFixed(3)}
80-
y2={y2.toFixed(3)}
81-
/>
82-
)
83-
})()}
8495
{/* Click surfaces for rotation and resizing actions */}
8596
{[
8697
// top left

0 commit comments

Comments
 (0)