-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimated_Line.tsx
More file actions
157 lines (149 loc) · 4.84 KB
/
Animated_Line.tsx
File metadata and controls
157 lines (149 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { useEffect, useRef, useState } from "react"
import { motion } from "framer-motion"
import { addPropertyControls, ControlType } from "framer"
export function AnimatedLine({
direction = "horizontal", // "horizontal" or "vertical"
duration = 2, // Time in seconds
delay = 0, // Delay in seconds
lineColor = "#000000", // Default line color
triggerOnVisibility = false, // Whether to trigger animation only when in viewport
replay = true, // Whether the animation should replay every time in view
transitionType = "ease", // Type of transition ("ease", "linear", etc.)
customCurve = "cubic-bezier(0.25, 0.8, 0.25, 1)", // Custom cubic-bezier curve
}) {
const [animate, setAnimate] = useState("start")
const [hasAnimated, setHasAnimated] = useState(false)
const ref = useRef(null)
const handleVisibility = (entries) => {
const [entry] = entries
if (entry.isIntersecting) {
if (!triggerOnVisibility || replay || !hasAnimated) {
setTimeout(() => setAnimate("end"), delay * 1000) // Apply delay
setHasAnimated(true)
}
} else if (replay && triggerOnVisibility) {
setAnimate("start") // Reset animation state
}
}
useEffect(() => {
const observer = new IntersectionObserver(handleVisibility, {
threshold: 0.1, // Trigger when 10% of the element is in view
})
if (ref.current) {
observer.observe(ref.current)
}
return () => observer.disconnect()
}, [triggerOnVisibility, replay, delay])
useEffect(() => {
if (!triggerOnVisibility) {
setTimeout(() => setAnimate("end"), delay * 1000) // Apply delay for non-visibility-triggered animations
}
}, [triggerOnVisibility, delay])
const variants = {
start: {
width: direction === "horizontal" ? "0%" : "100%", // Start at 0% width for horizontal
height: direction === "vertical" ? "0%" : "100%", // Start at 0% height for vertical
transition: {
duration,
ease:
transitionType === "custom" ? customCurve : transitionType,
},
},
end: {
width: direction === "horizontal" ? "100%" : "100%", // Expand to full width for horizontal
height: direction === "vertical" ? "100%" : "100%", // Expand to full height for vertical
transition: {
duration,
ease:
transitionType === "custom" ? customCurve : transitionType,
},
},
}
return (
<motion.div
ref={ref}
className="animated-line"
style={{
position: "relative",
width: "100%",
height: "100%",
overflow: "hidden",
}}
>
<motion.div
variants={variants}
initial="start"
animate={animate}
style={{
position: "absolute",
top: 0,
left: 0,
backgroundColor: lineColor,
width: direction === "horizontal" ? "0%" : "100%", // Default dimensions
height: direction === "vertical" ? "0%" : "100%",
}}
/>
</motion.div>
)
}
// Exposing properties for the property panel
addPropertyControls(AnimatedLine, {
direction: {
type: ControlType.SegmentedEnum,
title: "Direction",
options: ["horizontal", "vertical"],
defaultValue: "horizontal",
},
duration: {
type: ControlType.Number,
title: "Duration (s)",
min: 0.1,
max: 10,
step: 0.1,
defaultValue: 2,
},
delay: {
type: ControlType.Number,
title: "Delay (s)",
min: 0,
max: 10,
step: 0.1,
defaultValue: 0,
},
lineColor: {
type: ControlType.Color,
title: "Line Color",
defaultValue: "#000000",
},
triggerOnVisibility: {
type: ControlType.Boolean,
title: "Trigger on Visibility",
defaultValue: false,
},
replay: {
type: ControlType.Boolean,
title: "Replay Animation",
defaultValue: true,
},
transitionType: {
type: ControlType.Enum,
title: "Transition Type",
options: [
"ease",
"linear",
"ease-in",
"ease-out",
"ease-in-out",
"custom",
],
defaultValue: "ease",
},
customCurve: {
type: ControlType.String,
title: "Custom Curve",
defaultValue: "cubic-bezier(0.25, 0.8, 0.25, 1)",
hidden(props) {
return props.transitionType !== "custom"
},
},
})