Skip to content

Commit 831b904

Browse files
committed
draft swipe position
1 parent b397127 commit 831b904

File tree

3 files changed

+228
-16
lines changed

3 files changed

+228
-16
lines changed

examples/RasterLayer.ipynb

+73-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
{
1111
"cell_type": "code",
12-
"execution_count": 1,
12+
"execution_count": 8,
1313
"metadata": {},
1414
"outputs": [],
1515
"source": [
@@ -18,7 +18,7 @@
1818
},
1919
{
2020
"cell_type": "code",
21-
"execution_count": 2,
21+
"execution_count": 9,
2222
"metadata": {},
2323
"outputs": [],
2424
"source": [
@@ -30,23 +30,23 @@
3030
},
3131
{
3232
"cell_type": "code",
33-
"execution_count": 3,
33+
"execution_count": 10,
3434
"metadata": {
3535
"scrolled": true
3636
},
3737
"outputs": [
3838
{
3939
"data": {
4040
"application/vnd.jupyter.widget-view+json": {
41-
"model_id": "bf54f7b270eb4c12ba2fbcaeea2583da",
41+
"model_id": "77010bf2973f41d9a93c400f283857bd",
4242
"version_major": 2,
4343
"version_minor": 0
4444
},
4545
"text/plain": [
4646
"Map(center=[4.299875503991089, 46.85012303279379], zoom=0.0)"
4747
]
4848
},
49-
"execution_count": 3,
49+
"execution_count": 10,
5050
"metadata": {},
5151
"output_type": "execute_result"
5252
}
@@ -65,7 +65,7 @@
6565
},
6666
{
6767
"cell_type": "code",
68-
"execution_count": 4,
68+
"execution_count": 11,
6969
"metadata": {},
7070
"outputs": [],
7171
"source": [
@@ -74,7 +74,7 @@
7474
},
7575
{
7676
"cell_type": "code",
77-
"execution_count": 5,
77+
"execution_count": 12,
7878
"metadata": {},
7979
"outputs": [],
8080
"source": [
@@ -83,7 +83,7 @@
8383
},
8484
{
8585
"cell_type": "code",
86-
"execution_count": 26,
86+
"execution_count": 13,
8787
"metadata": {},
8888
"outputs": [],
8989
"source": [
@@ -94,6 +94,64 @@
9494
"m.add_layer(raster) "
9595
]
9696
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": 14,
100+
"metadata": {},
101+
"outputs": [
102+
{
103+
"data": {
104+
"application/vnd.jupyter.widget-view+json": {
105+
"model_id": "f2b5931a98ee4e40b8b23f7e5f3ee8a1",
106+
"version_major": 2,
107+
"version_minor": 0
108+
},
109+
"text/plain": [
110+
"IntSlider(value=0, description='Swipe Position:')"
111+
]
112+
},
113+
"metadata": {},
114+
"output_type": "display_data"
115+
},
116+
{
117+
"data": {
118+
"application/vnd.jupyter.widget-view+json": {
119+
"model_id": "77010bf2973f41d9a93c400f283857bd",
120+
"version_major": 2,
121+
"version_minor": 0
122+
},
123+
"text/plain": [
124+
"Map(center=[4.299875503991089, 46.85012303279379], layers=[RasterTileLayer(), RasterTileLayer(url='https://api…"
125+
]
126+
},
127+
"metadata": {},
128+
"output_type": "display_data"
129+
}
130+
],
131+
"source": [
132+
"import ipywidgets as widgets\n",
133+
"# Création du slider\n",
134+
"slider = widgets.IntSlider(\n",
135+
" value=0,\n",
136+
" min=0,\n",
137+
" max=100,\n",
138+
" step=1,\n",
139+
" description='Swipe Position:',\n",
140+
" continuous_update=True\n",
141+
")\n",
142+
"\n",
143+
"# Créez une fonction pour mettre à jour le modèle lorsque le slider change\n",
144+
"def on_slider_change(change):\n",
145+
" m.swipe_position=change['new']\n",
146+
"\n",
147+
"# Lier la fonction de mise à jour au changement de valeur du slider\n",
148+
"slider.observe(on_slider_change, names='value')\n",
149+
"\n",
150+
"# Affichez le slider dans le notebook\n",
151+
"display(slider)\n",
152+
"display(m)\n"
153+
]
154+
},
97155
{
98156
"cell_type": "code",
99157
"execution_count": 8,
@@ -132,6 +190,13 @@
132190
"source": [
133191
"m.remove_layer(rasterlay)"
134192
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": null,
197+
"metadata": {},
198+
"outputs": [],
199+
"source": []
135200
}
136201
],
137202
"metadata": {

src/rastertilelayer.ts

+51-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
// Copyright (c) QuantStack
2-
// Distributed under the terms of the Modified BSD License.
31
import { DOMWidgetModel, ISerializers } from '@jupyter-widgets/base';
4-
import TileLayer from 'ol/layer/WebGLTile.js';
2+
import WebGLTileLayer from 'ol/layer/WebGLTile.js';
53
import XYZ from 'ol/source/XYZ.js';
64
import { MODULE_NAME, MODULE_VERSION } from './version';
75
import { MapView } from './widget';
86
import { LayerModel, LayerView } from './layer';
97

8+
type WebGLEvent = {
9+
context: WebGLRenderingContext;
10+
};
11+
1012
export class RasterTileLayerModel extends LayerModel {
1113
defaults() {
1214
return {
@@ -28,7 +30,7 @@ export class RasterTileLayerModel extends LayerModel {
2830

2931
static serializers: ISerializers = {
3032
...DOMWidgetModel.serializers,
31-
// Add any extra serializers here
33+
// Add any extra serializers ici
3234
};
3335

3436
static model_name = 'RasterTileLayerModel';
@@ -41,15 +43,36 @@ export class RasterTileLayerModel extends LayerModel {
4143

4244
export class RasterTileLayerView extends LayerView {
4345
map_view: MapView;
46+
tileLayer: WebGLTileLayer;
47+
48+
private prerenderListener: (event: WebGLEvent) => void;
49+
private postrenderListener: (event: WebGLEvent) => void;
50+
private previousSwipePosition: number | undefined;
51+
52+
constructor(options: any) {
53+
super(options);
54+
this.map_view = options.options.map_view;
55+
this.prerenderListener = this.map_view.handlePrerender.bind(this.map_view);
56+
this.postrenderListener = this.map_view.handlePostrender.bind(
57+
this.map_view,
58+
);
59+
this.previousSwipePosition = undefined;
60+
}
4461

4562
render() {
4663
super.render();
4764
this.urlChanged();
4865
this.model.on('change:url', this.urlChanged, this);
66+
this.model.on(
67+
'change:swipe_position',
68+
this.handleSwipePositionChanged,
69+
this,
70+
);
71+
this.updateEventListeners();
4972
}
5073

5174
create_obj() {
52-
this.obj = this.tileLayer = new TileLayer({
75+
this.obj = this.tileLayer = new WebGLTileLayer({
5376
source: new XYZ({
5477
url: this.model.get('url'),
5578
attributions: this.model.get('attributions'),
@@ -74,5 +97,27 @@ export class RasterTileLayerView extends LayerView {
7497
}
7598
}
7699

77-
tileLayer: TileLayer;
100+
handleSwipePositionChanged() {
101+
const swipePosition = this.model.get('swipe_position');
102+
console.log('Swipe Position Changed:', swipePosition);
103+
104+
if (this.previousSwipePosition !== swipePosition) {
105+
this.previousSwipePosition = swipePosition;
106+
this.updateEventListeners();
107+
this.map_view.map.render();
108+
}
109+
}
110+
111+
updateEventListeners() {
112+
console.log('Updating event listeners');
113+
const swipePosition = this.model.get('swipe_position');
114+
(this.tileLayer as any).un('precompose', this.prerenderListener);
115+
(this.tileLayer as any).un('postcompose', this.postrenderListener);
116+
117+
if (swipePosition >= 0) {
118+
(this.tileLayer as any).on('precompose', this.prerenderListener);
119+
(this.tileLayer as any).on('postcompose', this.postrenderListener);
120+
}
121+
console.log('Event listeners updated');
122+
}
78123
}

0 commit comments

Comments
 (0)