1- // Copyright (c) QuantStack
2- // Distributed under the terms of the Modified BSD License.
31import Control from 'ol/control/Control' ;
42import { getRenderPixel } from 'ol/render' ;
53import 'ol/ol.css' ;
@@ -10,19 +8,16 @@ import { MapView } from './widget';
108interface SplitMapControlOptions {
119 target ?: string ;
1210 map_view ?: MapView ;
13- swipe_position ?: number ; // Ajoutez swipe_position en tant que nombre
11+ swipe_position ?: number ;
1412}
13+
1514export default class SplitMapControl extends Control {
1615 swipe : HTMLInputElement ;
1716 leftLayer : any ;
18- rightLayer : any ;
1917 map_view : MapView ;
20- swipe_position : any ;
21- constructor (
22- leftLayer : any ,
23- rightLayer : any ,
24- options : SplitMapControlOptions = { } ,
25- ) {
18+ private _swipe_position : number ;
19+
20+ constructor ( leftLayer : any , options : SplitMapControlOptions = { } ) {
2621 const element = document . createElement ( 'div' ) ;
2722 element . className = 'ol-unselectable ol-control split-map-control' ;
2823
@@ -32,98 +27,63 @@ export default class SplitMapControl extends Control {
3227 } ) ;
3328
3429 this . leftLayer = leftLayer ;
35- this . rightLayer = rightLayer ;
3630
37- console . log ( 'Initializing SplitMapControl...' ) ;
38- console . log ( options ) ;
3931 if ( options . map_view ) {
4032 this . map_view = options . map_view ;
41- console . log ( 'MapView initialized:' , this . map_view ) ;
4233 } else {
4334 throw new Error ( 'MapView is required for SplitMapControl.' ) ;
4435 }
45- if ( options . swipe_position ) {
46- this . swipe_position = options . swipe_position ;
47- console . log ( 'swipe_position initialized:' , this . swipe_position ) ;
48- } else {
49- throw new Error ( 'swipe_position is required for SplitMapControl.' ) ;
50- }
36+
37+ this . _swipe_position = options . swipe_position ?? 0 ;
5138
5239 const swiperContainer = document . createElement ( 'div' ) ;
5340 swiperContainer . className = 'swiper-container' ;
41+ swiperContainer . style . width = '100%' ;
5442
5543 this . swipe = document . createElement ( 'input' ) ;
5644 this . swipe . type = 'range' ;
5745 this . swipe . className = 'swipe' ;
58- ( this . swipe . value = this . swipe_position ) ,
59- swiperContainer . appendChild ( this . swipe ) ;
60- console . log ( 'this.map_view.map_container' , this . map_view . map_container ) ;
61- this . map_view . map_container . appendChild ( swiperContainer ) ;
62- this . handleSwipe ( ) ;
63- }
64-
65- handleSwipe ( event ?: Event ) {
66- console . log ( 'Handling swipe event...' ) ;
67- console . log ( this . swipe . value ) ;
68- if ( ! this . leftLayer . hasListener ( 'prerender' ) ) {
69- console . log ( '1' ) ;
70- this . leftLayer . on ( 'prerender' , this . prerender . bind ( this ) ) ;
71- }
72-
73- if ( ! this . leftLayer . hasListener ( 'postrender' ) ) {
74- console . log ( '2' ) ;
75- //this.leftLayer.on('postrender', this.postrender.bind(this));
76- }
77-
78- if ( ! this . rightLayer . hasListener ( 'prerender' ) ) {
79- console . log ( '3' ) ;
46+ this . swipe . style . width = '100%' ;
47+ this . updateSwipeValue ( ) ;
48+ swiperContainer . appendChild ( this . swipe ) ;
8049
81- //this.rightLayer.on('prerender', this.prerender.bind(this));
82- }
83-
84- if ( ! this . rightLayer . hasListener ( 'postrender' ) ) {
85- console . log ( '4' ) ;
86-
87- this . rightLayer . on ( 'postrender' , this . postrender . bind ( this ) ) ;
88- }
50+ this . map_view . map_container . style . position = 'relative' ;
51+ this . map_view . map_container . appendChild ( swiperContainer ) ;
8952
90- console . log ( 'ok' ) ;
91- }
53+ const map_view = this . map_view ;
9254
93- prerender ( event : any ) {
94- console . log ( 'Prerender event triggered.' ) ;
55+ this . leftLayer . on ( 'prerender' , ( event : any ) => {
56+ const gl = event . context ;
57+ gl . enable ( gl . SCISSOR_TEST ) ;
9558
96- const gl = event . context ; // Get WebGL context
97- gl . enable ( gl . SCISSOR_TEST ) ; // Enable scissor test
59+ const mapSize = map_view . getSize ( ) ;
9860
99- const mapSize = this . map_view . getSize ( ) ; // Get the size of the map
100- if ( ! mapSize ) {
101- console . warn ( 'Map size is undefined.' ) ;
102- return ;
103- }
61+ if ( mapSize ) {
62+ const bottomLeft = getRenderPixel ( event , [ 0 , mapSize [ 1 ] ] ) ;
63+ const topRight = getRenderPixel ( event , [ mapSize [ 0 ] , 0 ] ) ;
10464
105- // Get render pixels for the bottom left and top right corners
106- const bottomLeft = getRenderPixel ( event , [ 0 , mapSize [ 1 ] ] ) ;
107- const topRight = getRenderPixel ( event , [ mapSize [ 0 ] , 0 ] ) ;
65+ const width = Math . round ( ( topRight [ 0 ] - bottomLeft [ 0 ] ) * ( this . _swipe_position / 100 ) ) ;
66+ const height = topRight [ 1 ] - bottomLeft [ 1 ] ;
10867
109- // Get the swipe value from the input element
110- const swipeValue = this . swipe . value ;
111- console . log ( 'swipeValue' , swipeValue ) ;
68+ gl . scissor ( bottomLeft [ 0 ] , bottomLeft [ 1 ] , width , height ) ;
69+ }
70+ } ) ;
11271
113- // Calculate the width and height for the scissor test
114- const width = Math . round (
115- ( topRight [ 0 ] - bottomLeft [ 0 ] ) * ( Number ( swipeValue ) / 100 ) ,
116- ) ;
117- const height = topRight [ 1 ] - bottomLeft [ 1 ] ;
72+ this . leftLayer . on ( 'postrender' , ( event : any ) => {
73+ const gl = event . context ;
74+ gl . disable ( gl . SCISSOR_TEST ) ;
75+ } ) ;
11876
119- // Define the scissor box
120- gl . scissor ( bottomLeft [ 0 ] , bottomLeft [ 1 ] , width , height ) ;
77+ this . swipe . addEventListener ( 'input' , ( ) => {
78+ this . _swipe_position = parseInt ( this . swipe . value , 10 ) ;
79+ this . updateSwipeValue ( ) ;
80+ map_view . map . render ( ) ;
81+ } ) ;
12182 }
12283
123- postrender ( event : any ) {
124- console . log ( 'Postrender event triggered.' ) ;
125-
126- const gl = event . context ; // Get WebGL context
127- gl . disable ( gl . SCISSOR_TEST ) ; // Disable scissor test
84+ private updateSwipeValue ( ) {
85+ if ( this . swipe ) {
86+ this . swipe . value = this . _swipe_position . toString ( ) ;
87+ }
12888 }
12989}
0 commit comments