Skip to content

Commit 733fde8

Browse files
committed
fix: handle visibility and opacity changes
closes #277
1 parent 4c30138 commit 733fde8

File tree

6 files changed

+124
-9
lines changed

6 files changed

+124
-9
lines changed

Diff for: docs/.vitepress/theme/demo.css

+43
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ main form {
7575
display: flex;
7676
align-items: baseline;
7777
gap: 1rem;
78+
margin-bottom: 1rem;
7879
}
7980

8081
main form fieldset {
@@ -86,6 +87,10 @@ main form fieldset {
8687
border: none;
8788
}
8889

90+
main form label {
91+
font-weight: bold;
92+
}
93+
8994
main input[type="checkbox"] {
9095
-webkit-appearance: none;
9196
appearance: none;
@@ -123,3 +128,41 @@ main ul > li:has(> input[type="checkbox"]) {
123128
align-items: center;
124129
gap: 0.5rem;
125130
}
131+
132+
main input[type="range"] {
133+
margin: 0;
134+
padding: 0;
135+
position: relative;
136+
top: 50%;
137+
top: 0.9rem;
138+
}
139+
140+
main input[type="range"] {
141+
-webkit-appearance: none;
142+
}
143+
144+
main input[type="range"]::-webkit-slider-runnable-track {
145+
width: 300px;
146+
height: 5px;
147+
background: #ddd;
148+
border: none;
149+
border-radius: 3px;
150+
}
151+
152+
main input[type="range"]::-webkit-slider-thumb {
153+
-webkit-appearance: none;
154+
border: none;
155+
height: 16px;
156+
width: 16px;
157+
border-radius: 50%;
158+
background: var(--vp-c-brand-1);
159+
margin-top: -4px;
160+
}
161+
162+
main input[type="range"]:focus {
163+
outline: none;
164+
}
165+
166+
main input[type="range"]:focus::-webkit-slider-runnable-track {
167+
background: #ccc;
168+
}

Diff for: src/components/layers/OlImageLayer.vue

+18-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</template>
66

77
<script setup lang="ts">
8-
import { inject, provide, onUnmounted, onMounted, watch } from "vue";
8+
import { inject, provide, onUnmounted, onMounted, watch, ref } from "vue";
99
import ImageLayer from "ol/layer/Image";
1010
import type Map from "ol/Map";
1111
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
@@ -25,12 +25,27 @@ const layerGroup = inject<LayerGroup | null>("layerGroup", null);
2525
2626
const { properties } = usePropsAsObjectProperties(props);
2727
28-
const imageLayer = new ImageLayer(properties);
28+
const imageLayer = ref(new ImageLayer(properties));
2929
3030
watch(properties, () => {
31-
imageLayer.setProperties(properties);
31+
imageLayer.value.setProperties(properties);
3232
});
3333
34+
watch(
35+
() => props.opacity,
36+
(newOpacity: number) => {
37+
imageLayer.value.setOpacity(newOpacity);
38+
},
39+
{ immediate: true },
40+
);
41+
42+
watch(
43+
() => props.visible,
44+
(newVisible: boolean) => {
45+
imageLayer.value.setVisible(newVisible);
46+
},
47+
{ immediate: true },
48+
);
3449
onMounted(() => {
3550
map?.addLayer(imageLayer);
3651

Diff for: src/components/layers/OlTileLayer.vue

+18-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
onUnmounted,
1212
onMounted,
1313
watch,
14-
computed,
14+
ref,
1515
type Ref,
1616
} from "vue";
1717
import TileLayer from "ol/layer/Tile";
@@ -42,7 +42,23 @@ const overViewMap = inject<Ref<OverviewMap | null> | null>("overviewMap", null);
4242
4343
const { properties } = usePropsAsObjectProperties(props);
4444
45-
const tileLayer = computed(() => new TileLayer(properties));
45+
const tileLayer = ref(new TileLayer(properties));
46+
47+
watch(
48+
() => props.opacity,
49+
(newOpacity: number) => {
50+
tileLayer.value.setOpacity(newOpacity);
51+
},
52+
{ immediate: true },
53+
);
54+
55+
watch(
56+
() => props.visible,
57+
(newVisible: boolean) => {
58+
tileLayer.value.setVisible(newVisible);
59+
},
60+
{ immediate: true },
61+
);
4662
4763
const applyTileLayer = () => {
4864
if (layerGroup) {

Diff for: src/components/layers/OlWebglTileLayer.vue

+18-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<script setup lang="ts">
88
import type { Ref } from "vue";
9-
import { inject, provide, onUnmounted, onMounted, watch, computed } from "vue";
9+
import { inject, provide, onUnmounted, onMounted, watch, ref } from "vue";
1010
import TileLayer, { type Options } from "ol/layer/WebGLTile";
1111
import type Map from "ol/Map";
1212
import type { OverviewMap } from "ol/control";
@@ -24,7 +24,23 @@ const overViewMap = inject<Ref<OverviewMap | null> | null>("overviewMap", null);
2424
2525
const { properties } = usePropsAsObjectProperties(props);
2626
27-
const tileLayer = computed(() => new TileLayer(properties));
27+
const tileLayer = ref(new TileLayer(properties));
28+
29+
watch(
30+
() => props.opacity,
31+
(newOpacity: number) => {
32+
tileLayer.value.setOpacity(newOpacity);
33+
},
34+
{ immediate: true },
35+
);
36+
37+
watch(
38+
() => props.visible,
39+
(newVisible: boolean) => {
40+
tileLayer.value.setVisible(newVisible);
41+
},
42+
{ immediate: true },
43+
);
2844
2945
const applyTileLayer = () => {
3046
if (layerGroup) {

Diff for: src/components/styles/OlStyleText.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const createText = (properties: typeof props) => {
6666
"fill" | "stroke" | "backgroundFill" | "backgroundStroke"
6767
>;
6868
const options: Options = {
69-
...innerProperties
69+
...innerProperties,
7070
};
7171
if (properties.fill) {
7272
options.fill = new Fill({ color: properties.fill });

Diff for: src/demos/TileWMSDemo.vue

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
<template>
2+
<form>
3+
<fieldset>
4+
<label for="opacity-slider">Layer Opacity</label>
5+
<input
6+
type="range"
7+
id="opacity-slider"
8+
min="0"
9+
max="1"
10+
step="0.1"
11+
v-model.number="layerOpacity"
12+
/>
13+
<span class="description">{{ layerOpacity }}</span>
14+
</fieldset>
15+
<fieldset>
16+
<label for="visibility-toggle">Layer Visibility:</label>
17+
<input type="checkbox" id="visibility-toggle" v-model="layerVisible" />
18+
<span> {{ layerVisible ? "Visible" : "Hidden" }}</span>
19+
</fieldset>
20+
</form>
221
<ol-map
322
:loadTilesWhileAnimating="true"
423
:loadTilesWhileInteracting="true"
@@ -11,7 +30,11 @@
1130
<ol-source-osm />
1231
</ol-tile-layer>
1332

14-
<ol-tile-layer :zIndex="1001">
33+
<ol-tile-layer
34+
:zIndex="1001"
35+
:opacity="layerOpacity"
36+
:visible="layerVisible"
37+
>
1538
<ol-source-tile-wms
1639
url="https://ahocevar.com/geoserver/wms"
1740
:extent="[-13884991, 2870341, -7455066, 6338219]"
@@ -29,4 +52,6 @@ import { ref } from "vue";
2952
const zoom = ref(4);
3053
const rotation = ref(0);
3154
const center = ref([-10997148, 4569099]);
55+
const layerOpacity = ref(0.4);
56+
const layerVisible = ref(true);
3257
</script>

0 commit comments

Comments
 (0)