Skip to content

Commit 12488bb

Browse files
committed
Add option to change the amount of extra color stops per transition
1 parent 4211484 commit 12488bb

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ const { colors, locations } = easeGradient({
6161
})
6262
```
6363

64+
Or the amount of extra color stops added between each transition
65+
66+
```js
67+
const { colors, locations } = easeGradient({
68+
colorStops: {
69+
0: {
70+
color: 'transparent',
71+
},
72+
1: {
73+
color: '#18181B',
74+
},
75+
},
76+
// The more color stops added, the smoother the transition is
77+
// Defaults to 12
78+
extraColorStopsPerTransition: 16
79+
})
80+
```
6481
## Credits
6582

6683
- [Easing Linear Gradients](https://css-tricks.com/easing-linear-gradients/)

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ interface ColorStops {
1010

1111
interface GradientParams {
1212
colorStops: ColorStops
13+
extraColorStopsPerTransition?: number
1314
easing?: EasingFunction
1415
}
1516

1617
const easeInOut = Easing.bezier(0.42, 0, 0.58, 1)
17-
const TOTAL_STOPS_PER_TRANSITION = 16
1818

19-
function easeGradient({ colorStops, easing = easeInOut }: GradientParams) {
19+
function easeGradient({ colorStops, easing = easeInOut, extraColorStopsPerTransition = 12 }: GradientParams) {
2020
const colors: string[] = []
2121
const locations: number[] = []
2222

@@ -45,11 +45,11 @@ function easeGradient({ colorStops, easing = easeInOut }: GradientParams) {
4545
})
4646

4747
const currentTransitionLength = endLocation - startLocation
48-
const stepSize = 1 / (TOTAL_STOPS_PER_TRANSITION - 1)
48+
const stepSize = 1 / (extraColorStopsPerTransition + 1)
4949

5050
for (
5151
let stepIndex = 0;
52-
stepIndex <= TOTAL_STOPS_PER_TRANSITION - 1;
52+
stepIndex <= extraColorStopsPerTransition + 1;
5353
stepIndex++
5454
) {
5555
const progress = stepIndex * stepSize

0 commit comments

Comments
 (0)