Skip to content

Commit 580591b

Browse files
committed
feat(ol-style-fill): correctly setup conic gradient (#375)
1 parent f2a740e commit 580591b

File tree

5 files changed

+95
-22
lines changed

5 files changed

+95
-22
lines changed

docs/componentsguide/styles/fill/index.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Use it inside ol-style, ol-style-circle, ol-style-text along with ol-style-strok
99
## Demo
1010

1111
<script setup>
12-
import MultiPoint from "@demos/MultiPoint.vue"
12+
import PolygonDemo from "@demos/PolygonDemo.vue"
1313
</script>
1414
<ClientOnly>
15-
<MultiPoint />
15+
<PolygonDemo />
1616
</ClientOnly>
1717

1818
## Setup
@@ -29,23 +29,31 @@ Styling a feature
2929

3030
::: code-group
3131

32-
<<< ../../../../src/demos/MultiPoint.vue
32+
<<< ../../../../src/demos/PolygonDemo.vue
3333

3434
:::
3535

3636
## Properties
3737

38+
You need to pass either a `color` or a `gradient`.
39+
3840
### color
3941

4042
- **Type**: `Number`
4143

4244
The color either in hexadecimal or as RGB array with red, green, and blue values between 0 and 255 and alpha between 0 and 1 inclusive.
4345

4446
### gradient
45-
- **Type**: `Object`
46-
```
47+
48+
- **Type**: `LinearGradient | RadialGradient | ConicGradient`
49+
50+
::: details Details for the gradient configuration
51+
52+
#### `LinearGradient`
53+
54+
```jsonc
4755
{
48-
"type": "linear", // Type of gradient. Here, it's a linear gradient.
56+
"type": "linear",
4957
"x0": 0, // x-coordinate of the starting point. Indicates the horizontal position where the gradient starts.
5058
"y0": 0, // y-coordinate of the starting point. Indicates the vertical position where the gradient starts.
5159
"x1": 0, // x-coordinate of the ending point. This is the horizontal position where the gradient ends. It’s the same as x0, meaning the gradient is vertical.
@@ -56,9 +64,13 @@ The color either in hexadecimal or as RGB array with red, green, and blue values
5664
[1, "green"] // End color at position 1. The color at the end of the gradient (y = 256) is green.
5765
]
5866
}
67+
```
68+
69+
#### `RadialGradient`
5970

71+
```jsonc
6072
{
61-
"type": "radial", // Type of gradient. Here, it's a radial gradient.
73+
"type": "radial",
6274
"x0": 128, // x-coordinate of the starting circle's center. Specifies the horizontal position of the starting circle.
6375
"y0": 128, // y-coordinate of the starting circle's center. Specifies the vertical position of the starting circle.
6476
"r0": 0, // Radius of the starting circle. Here, it's 0, meaning the gradient starts at a single point.
@@ -71,5 +83,22 @@ The color either in hexadecimal or as RGB array with red, green, and blue values
7183
[1, "white"] // Color at the edge of the gradient (r = 128). The color at the outer edge of the radial gradient is white.
7284
]
7385
}
86+
```
87+
88+
#### `ConicGradient`
89+
90+
```jsonc
91+
{
92+
"type": "conic",
93+
"x": 0, // x-axis coordinate of the center of the gradient.
94+
"y": 0, // y-axis coordinate of the center of the gradient.
95+
"startAngle": 0, // The angle at which to begin the gradient, in radians. The angle starts from a line going horizontally right from the center, and proceeds clockwise.
96+
"colorStops": [
97+
[0, "red"], // Start color at position 0. The color at the beginning of the gradient is red.
98+
[0.5, "yellow"], // Middle color at position 0.5. The color at the midpoint of the gradient (y = 128) is yellow.
99+
[1, "green"] // End color at position 1. The color at the end of the gradient (y = 256) is green.
100+
]
101+
}
102+
```
74103

75-
```
104+
:::

src/components/styles/OlStyleFill.vue

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties
1313
type GradientColorStop = [number, string];
1414
1515
// Define the type for linear gradients
16-
type LinearGradient = {
16+
export type LinearGradient = {
1717
type: "linear";
1818
x0: number;
1919
y0: number;
@@ -23,7 +23,7 @@ type LinearGradient = {
2323
};
2424
2525
// Define the type for radial gradients
26-
type RadialGradient = {
26+
export type RadialGradient = {
2727
type: "radial";
2828
x0: number;
2929
y0: number;
@@ -35,18 +35,16 @@ type RadialGradient = {
3535
};
3636
3737
// Define the type for conic gradients
38-
type ConicGradient = {
38+
export type ConicGradient = {
3939
type: "conic";
40-
cx: number;
41-
cy: number;
42-
radius: number;
40+
x: number;
41+
y: number;
4342
startAngle: number;
44-
endAngle: number;
4543
colorStops: GradientColorStop[];
4644
};
4745
4846
// Define a union type for all gradient types
49-
type Gradient = LinearGradient | RadialGradient | ConicGradient;
47+
export type Gradient = LinearGradient | RadialGradient | ConicGradient;
5048
5149
// Define the props type for the component
5250
const props = defineProps<{
@@ -100,8 +98,11 @@ const createGradientFill = (
10098
);
10199
break;
102100
case "conic":
103-
// Conic gradients are not directly supported by the Canvas API, use a linear gradient as a fallback
104-
grad = ctx.createLinearGradient(0, 0, width, height);
101+
grad = ctx.createConicGradient(
102+
gradient.x,
103+
gradient.y,
104+
gradient.startAngle,
105+
);
105106
break;
106107
default:
107108
throw new Error("Unsupported gradient type");

src/components/styles/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import OlStyleFlowline from "./OlStyleFlowline.vue";
99
import type { FeatureLike } from "ol/Feature";
1010
import type { Style } from "ol/style";
1111
import type { Vue3OpenlayersGlobalOptions } from "@/types";
12+
import type {
13+
Gradient,
14+
ConicGradient,
15+
RadialGradient,
16+
LinearGradient,
17+
} from "./OlStyleFill.vue";
1218

1319
type OverrideStyleFunction = (
1420
feature: FeatureLike,
@@ -46,4 +52,8 @@ export {
4652
OlStyleFlowline,
4753
OlStyleCircle,
4854
type OverrideStyleFunction,
55+
type Gradient,
56+
type ConicGradient,
57+
type RadialGradient,
58+
type LinearGradient,
4959
};

src/demos/PolygonDemo.vue

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,37 @@
8989
<ol-style-fill :gradient="radialGradient" />
9090
</ol-style>
9191
</ol-feature>
92+
<ol-feature>
93+
<ol-geom-polygon
94+
:coordinates="[
95+
[
96+
[-98.8435, 19.680586],
97+
[-98.838, 19.68098],
98+
[-98.835, 19.68522],
99+
[-98.843, 19.685667],
100+
[-98.835, 19.683586],
101+
],
102+
]"
103+
></ol-geom-polygon>
104+
<ol-style>
105+
<ol-style-fill :gradient="conicGradient" />
106+
</ol-style>
107+
</ol-feature>
92108
</ol-source-vector>
93109
</ol-vector-layer>
94110
</ol-map>
95111
</template>
96112

97-
<script setup>
113+
<script lang="ts" setup>
98114
import { ref } from "vue";
115+
import type { LinearGradient, RadialGradient } from "vue3-openlayers";
116+
import type { ConicGradient } from "@components/styles";
99117
100118
const center = ref([-98.8449, 19.6869]);
101119
const projection = ref("EPSG:4326");
102120
const zoom = ref(15);
103121
104-
const linearGradient = {
122+
const linearGradient: LinearGradient = {
105123
type: "linear",
106124
x0: 0,
107125
y0: 0,
@@ -114,7 +132,7 @@ const linearGradient = {
114132
],
115133
};
116134
117-
const radialGradient = {
135+
const radialGradient: RadialGradient = {
118136
type: "radial",
119137
x0: 128,
120138
y0: 128,
@@ -128,4 +146,18 @@ const radialGradient = {
128146
[1, "white"], // Edge color
129147
],
130148
};
149+
150+
const conicGradient: ConicGradient = {
151+
type: "conic",
152+
x: 100,
153+
y: 100,
154+
startAngle: 100,
155+
colorStops: [
156+
[0, "red"],
157+
[0.25, "orange"],
158+
[0.5, "yellow"],
159+
[0.75, "green"],
160+
[1, "blue"],
161+
],
162+
};
131163
</script>

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"paths": {
2929
"@/*": ["./src/*"],
3030
"@components/*": ["./src/components/*"],
31-
"@demos/*": ["./src/demos/*"]
31+
"@demos/*": ["./src/demos/*"],
32+
"vue3-openlayers/*": ["src/*"]
3233
},
3334
"checkJs": false
3435
},

0 commit comments

Comments
 (0)