Skip to content

Commit bdfcab6

Browse files
committed
fix(ol-webgl-tile-layer): react on property changes
- make TileLayer a shallowRef, to not always create a new class - apply property changes using `.set('<prop>', value)` - add demo for opacity / visibility changes closes #319
1 parent 07d34f2 commit bdfcab6

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

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

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

77
<script setup lang="ts">
88
import type { Ref } from "vue";
9-
import { inject, provide, onUnmounted, onMounted, watch, computed } from "vue";
9+
import {
10+
inject,
11+
provide,
12+
onUnmounted,
13+
onMounted,
14+
watch,
15+
shallowRef,
16+
} from "vue";
1017
import TileLayer, { type Options } from "ol/layer/WebGLTile";
1118
import type Map from "ol/Map";
1219
import type { OverviewMap } from "ol/control";
@@ -23,22 +30,19 @@ const overViewMap = inject<Ref<OverviewMap | null> | null>("overviewMap", null);
2330
2431
const { properties } = usePropsAsObjectProperties(props);
2532
26-
const tileLayer = computed(() => new TileLayer(properties));
33+
const tileLayer = shallowRef(new TileLayer(properties));
2734
2835
watch(
29-
() => props.opacity,
30-
(newOpacity: number) => {
31-
tileLayer.value.setOpacity(newOpacity);
36+
() => properties,
37+
(newValue) => {
38+
for (const key in newValue) {
39+
const keyInObj = key as keyof typeof newValue;
40+
if (newValue[keyInObj]) {
41+
tileLayer.value.set(key, newValue[keyInObj]);
42+
}
43+
}
3244
},
33-
{ immediate: true },
34-
);
35-
36-
watch(
37-
() => props.visible,
38-
(newVisible: boolean) => {
39-
tileLayer.value.setVisible(newVisible);
40-
},
41-
{ immediate: true },
45+
{ deep: true },
4246
);
4347
4448
const applyTileLayer = () => {

Diff for: src/demos/GeoTIFFDemo.vue

+27-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,12 @@
1130
:projection="projection"
1231
/>
1332

14-
<ol-webgl-tile-layer :style="trueColor">
33+
<ol-webgl-tile-layer
34+
:style="trueColor"
35+
:zIndex="1001"
36+
:opacity="layerOpacity"
37+
:visible="layerVisible"
38+
>
1539
<ol-source-geo-tiff
1640
:normalize="false"
1741
:sources="[
@@ -31,6 +55,8 @@ import { ref } from "vue";
3155
const center = ref([0, 0]);
3256
const zoom = ref(2);
3357
const projection = ref("EPSG:4326");
58+
const layerOpacity = ref(1);
59+
const layerVisible = ref(true);
3460
3561
const max = 3000;
3662
function normalize(value) {

0 commit comments

Comments
 (0)