Skip to content

Commit 3179557

Browse files
authored
Add support for numeric direction option (#45)
* Add support for numeric direction (angle) for option * Add changesets
1 parent e16c19e commit 3179557

4 files changed

Lines changed: 31 additions & 7 deletions

File tree

.changeset/many-horses-vanish.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@imgproxy/imgproxy-js-core": minor
3+
---
4+
5+
Add support for numeric direction value in [gradient](https://docs.imgproxy.net/usage/processing#gradient) option

src/options/gradient.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ const build = (options: GradientOptionsPartial): string => {
3232

3333
if (color) guardIsNotStr(color, "gradient.color", true);
3434
if (direction) {
35-
guardIsNotStr(direction, "gradient.direction");
36-
guardIsValidVal(currentDirection, direction, "gradient.direction");
35+
if (typeof direction === "number") {
36+
guardIsNotNum(direction, "gradient.direction");
37+
} else {
38+
guardIsNotStr(direction, "gradient.direction");
39+
guardIsValidVal(currentDirection, direction, "gradient.direction");
40+
}
3741
}
3842
if (start)
3943
guardIsNotNum(start, "gradient.start", { addParam: { min: 0, max: 1 } });

src/types/gradient.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
*
66
* @param {number} opacity - specifies gradient opacity. When set to 0, gradient is not applied. Value range: `0` - `1`.
77
* @param {string} [color="000"] - (optional) specifies gradient color. If omitted, the gradient is black. Default `"000"`.
8-
* @param {"down" | "up" | "right" | "left"} [direction="down"] - (optional) specifies gradient direction. Default `"down"`.
8+
* @param {"down" | "up" | "right" | "left" | number} [direction="down"] - (optional) specifies gradient direction. Default `"down"`.
99
*
1010
* Available values:
1111
* - `down` - the top side of the gradient is transparrent, the bottom side is opaque
1212
* - `up` - the bottom side of the gradient is transparrent, the top side is opaque
1313
* - `right` - the left side of the gradient is transparrent, the right side is opaque
1414
* - `left` - the right side of the gradient is transparrent, the left side is opaque
15+
* - `number` - angle in degrees (clockwise). `0` creates a gradient from top to bottom; `90` creates a gradient from right to left.
1516
* @param {number} [start=0.0] - (optional) specifies the start point of the gradient. Value range: `0.0` - `1.0`. Default `0.0`.
1617
* @param {number} [stop=1.0] - (optional) specifies the end point of the gradient. Value range: `0.0` - `1.0`. Default `1.0`.
1718
*
@@ -25,12 +26,15 @@
2526
* // Apply a gray gradient with 0.3 opacity from the left to the right of the image with the start point at 0.2 and the end point at 0.8
2627
* {gradient: {opacity: 0.3, direction: "right", color: "808080", start: 0.2, stop: 0.8}}
2728
*
29+
* // Apply a gray gradient with 0.3 opacity at a 45 degree angle
30+
* {gradient: {opacity: 0.3, direction: 45, color: "808080"}}
31+
*
2832
* @see https://docs.imgproxy.net/generating_the_url?id=gradient
2933
*/
3034
interface Gradient {
3135
opacity: number;
3236
color?: string;
33-
direction?: "down" | "up" | "right" | "left";
37+
direction?: "down" | "up" | "right" | "left" | number;
3438
start?: number;
3539
stop?: number;
3640
}

tests/optionsBasic/gradient.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ describe("gradient", () => {
6868
);
6969
});
7070

71-
it("should throw an error if direction is not a string", () => {
71+
it("should throw an error if direction is not a string or number", () => {
7272
expect(() =>
7373
// @ts-expect-error: Let's ignore an error (check for users with vanilla js).
74-
build({ gradient: { opacity: 0.5, direction: 250 } })
74+
build({ gradient: { opacity: 0.5, direction: true } })
7575
).toThrow("gradient.direction is not a string");
7676
});
7777

78-
it("should throw an error if direction is not one of: down, up, right, left", () => {
78+
it("should throw an error if direction is a string not among: down, up, right, left", () => {
7979
expect(() =>
8080
// @ts-expect-error: Let's ignore an error (check for users with vanilla js).
8181
build({ gradient: { opacity: 0.5, direction: "top" } })
@@ -84,6 +84,17 @@ describe("gradient", () => {
8484
);
8585
});
8686

87+
it("should accept numeric angle for direction", () => {
88+
expect(
89+
build({
90+
gradient: {
91+
opacity: 0.5,
92+
direction: 45,
93+
},
94+
})
95+
).toEqual("gr:0.5::45");
96+
});
97+
8798
it("should throw an error if start is not a number", () => {
8899
expect(() =>
89100
// @ts-expect-error: Let's ignore an error (check for users with vanilla js).

0 commit comments

Comments
 (0)