Skip to content

Commit 213717c

Browse files
Merge pull request #27 from CA-Visualizer-for-Education/feat/svg-arrows
feat: created ManhattanArrow component
2 parents 5b77650 + 927f30f commit 213717c

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use client'
2+
3+
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
4+
import PlayArrowOutlinedIcon from '@mui/icons-material/PlayArrowOutlined';
5+
6+
type Direction = 'left' | 'right' | 'up' | 'down';
7+
8+
type PathSegment = {
9+
direction: Direction;
10+
length: number;
11+
};
12+
13+
interface ManhattanArrowProps {
14+
start: {
15+
x: number;
16+
y: number;
17+
};
18+
path: PathSegment[];
19+
type: 'dependency' | 'implements'
20+
rotation: 0 | 90 | 180 | 270;
21+
}
22+
23+
export default function ManhattanArrow({ start, path, type, rotation }: ManhattanArrowProps) {
24+
let x = start.x;
25+
let y = start.y;
26+
let d = '';
27+
28+
d += `M ${x} ${y} `;
29+
30+
for (const { direction, length } of path) {
31+
if (direction === 'left') {
32+
x -= length;
33+
} else if (direction === 'right') {
34+
x += length;
35+
} else if (direction === 'up') {
36+
y -= length;
37+
} else if (direction === 'down') {
38+
y += length;
39+
}
40+
41+
d+= `L ${x} ${y} `;
42+
}
43+
44+
console.log(d);
45+
46+
return (
47+
<svg
48+
width={1000}
49+
height={700}
50+
viewBox="0 0 1000 700"
51+
style={{
52+
overflow: 'visible',
53+
position: 'absolute',
54+
top: 0,
55+
left: 0,
56+
zIndex: 9999,
57+
pointerEvents: 'none',
58+
border: '1px solid transparent', // for debugging
59+
}}
60+
>
61+
<path
62+
d={d}
63+
stroke="black"
64+
strokeWidth={2}
65+
fill="none"
66+
/>
67+
<foreignObject
68+
x={x - 10}
69+
y={y - 10}
70+
width={20}
71+
height={20}
72+
>
73+
{ type === "dependency" ?
74+
<PlayArrowIcon
75+
style={{
76+
width: '20px',
77+
height: '20px',
78+
color: 'black',
79+
transform: `rotate(${rotation}deg)`,
80+
}}
81+
/>
82+
:
83+
<PlayArrowOutlinedIcon
84+
style={{
85+
width: '20px',
86+
height: '20px',
87+
color: 'black',
88+
transform: `rotate(${rotation}deg)`,
89+
}}
90+
/>
91+
}
92+
</foreignObject>
93+
</svg>
94+
)
95+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"contributors": [
3636
"Masa Abu Arja",
3737
"Matthew Dahlgren",
38-
"Seokjin Yoo"
38+
"Seokjin Yoo",
39+
"Annie Wang"
3940
]
4041
}

0 commit comments

Comments
 (0)