@@ -260,6 +260,15 @@ function defineStyleProperty(style, category, name, value, defaultValue) {
260
260
* @property {number|function } text.haloBlur - The blur value of the halo, in pixels.
261
261
* Default is `0`.
262
262
*
263
+ * @property {Object } icon - Defines the appearance of icons attached to label.
264
+ * @property {String } icon.source - The url of the icons' image file.
265
+ * @property {String } icon.key - The key of the icons' image in a vector tile data set.
266
+ * @property {string } [icon.anchor='center'] - The anchor of the icon compared to the label position.
267
+ * Can be `left`, `bottom`, `right`, `center`, `top-left`, `top-right`, `bottom-left`
268
+ * or `bottom-right`.
269
+ * @property {number } icon.size - If the icon's image is passed with `icon.source` or
270
+ * `icon.key`, it size when displayed on screen is multiplied by `icon.size`. Default is `1`.
271
+ *
263
272
* @example
264
273
* const style = new itowns.Style({
265
274
* stroke: { color: 'red' },
@@ -295,13 +304,15 @@ class Style {
295
304
stroke : { } ,
296
305
point : { } ,
297
306
text : { } ,
307
+ icon : { } ,
298
308
} ;
299
309
300
310
params . zoom = params . zoom || { } ;
301
311
params . fill = params . fill || { } ;
302
312
params . stroke = params . stroke || { } ;
303
313
params . point = params . point || { } ;
304
314
params . text = params . text || { } ;
315
+ params . icon = params . icon || { } ;
305
316
306
317
this . zoom = { } ;
307
318
defineStyleProperty ( this , 'zoom' , 'min' , params . zoom . min ) ;
@@ -354,6 +365,12 @@ class Style {
354
365
defineStyleProperty ( this , 'text' , 'haloColor' , params . text . haloColor , '#000000' ) ;
355
366
defineStyleProperty ( this , 'text' , 'haloWidth' , params . text . haloWidth , 0 ) ;
356
367
defineStyleProperty ( this , 'text' , 'haloBlur' , params . text . haloBlur , 0 ) ;
368
+
369
+ this . icon = { } ;
370
+ defineStyleProperty ( this , 'icon' , 'source' , params . icon . source ) ;
371
+ defineStyleProperty ( this , 'icon' , 'key' , params . icon . key ) ;
372
+ defineStyleProperty ( this , 'icon' , 'anchor' , params . icon . anchor , 'center' ) ;
373
+ defineStyleProperty ( this , 'icon' , 'size' , params . icon . size , 1 ) ;
357
374
}
358
375
359
376
/**
@@ -407,6 +424,7 @@ class Style {
407
424
Object . assign ( this . stroke , style . stroke ) ;
408
425
Object . assign ( this . point , style . point ) ;
409
426
Object . assign ( this . text , style . text ) ;
427
+ Object . assign ( this . icon , style . icon ) ;
410
428
return this ;
411
429
}
412
430
@@ -438,7 +456,7 @@ class Style {
438
456
this . text . size = properties [ 'label-size' ] ;
439
457
440
458
if ( properties . icon ) {
441
- this . icon = { image : properties . icon , size : 1 } ;
459
+ this . icon . source = properties . icon ;
442
460
}
443
461
} else {
444
462
this . stroke . color = properties . stroke ;
@@ -537,7 +555,7 @@ class Style {
537
555
// additional icon
538
556
const key = readVectorProperty ( layer . layout [ 'icon-image' ] ) ;
539
557
if ( key ) {
540
- this . icon = { key } ;
558
+ this . icon . key = key ;
541
559
this . icon . size = readVectorProperty ( layer . layout [ 'icon-size' ] ) || 1 ;
542
560
}
543
561
}
@@ -573,14 +591,12 @@ class Style {
573
591
domElement . setAttribute ( 'data-before' , domElement . textContent ) ;
574
592
}
575
593
576
- if ( ! this . icon ) {
594
+ if ( ! this . icon . source && ! this . icon . key ) {
577
595
return ;
578
596
}
579
597
580
- const image = this . icon . image ;
581
-
598
+ const image = this . icon . source ;
582
599
const size = this . icon . size ;
583
-
584
600
const key = this . icon . key ;
585
601
586
602
let icon = cacheStyle . get ( image || key , size ) ;
@@ -591,45 +607,51 @@ class Style {
591
607
} else {
592
608
icon = getImage ( image ) ;
593
609
}
594
- icon . style . position = 'absolute' ;
595
610
cacheStyle . set ( icon , image || key , size ) ;
596
611
}
597
612
598
613
const addIcon = ( ) => {
599
614
const cIcon = icon . cloneNode ( ) ;
600
- cIcon . width *= size ;
601
- cIcon . height *= size ;
602
- switch ( this . text . anchor ) {
615
+
616
+ cIcon . setAttribute ( 'class' , 'itowns-icon' ) ;
617
+
618
+ cIcon . width = icon . width * this . icon . size ;
619
+ cIcon . height = icon . height * this . icon . size ;
620
+ cIcon . style . position = 'absolute' ;
621
+ cIcon . style . top = '0' ;
622
+ cIcon . style . left = '0' ;
623
+
624
+ switch ( this . icon . anchor ) { // center by default
603
625
case 'left' :
604
- cIcon . style . right = `calc(100% - ${ cIcon . width * 0.5 } px)` ;
605
- cIcon . style . top = `calc(50% - ${ cIcon . height * 0.5 } px)` ;
626
+ cIcon . style . top = `${ - 0.5 * cIcon . height } px` ;
606
627
break ;
607
628
case 'right' :
608
- cIcon . style . top = `calc(50% - ${ cIcon . height * 0.5 } px)` ;
629
+ cIcon . style . top = `${ - 0.5 * cIcon . height } px` ;
630
+ cIcon . style . left = `${ - cIcon . width } px` ;
609
631
break ;
610
632
case 'top' :
611
- cIcon . style . right = `calc(50% - ${ cIcon . width * 0.5 } px) ` ;
633
+ cIcon . style . left = `${ - 0.5 * cIcon . width } px` ;
612
634
break ;
613
635
case 'bottom' :
614
- cIcon . style . top = `calc(100% - ${ cIcon . height * 0.5 } px) ` ;
615
- cIcon . style . right = `calc(50% - ${ cIcon . width * 0.5 } px) ` ;
636
+ cIcon . style . top = `${ - cIcon . height } px` ;
637
+ cIcon . style . left = `${ - 0.5 * cIcon . width } px` ;
616
638
break ;
617
639
case 'bottom-left' :
618
- cIcon . style . top = `calc(100% - ${ cIcon . height * 0.5 } px)` ;
619
- cIcon . style . right = `calc(100% - ${ cIcon . width * 0.5 } px)` ;
640
+ cIcon . style . top = `${ - cIcon . height } px` ;
620
641
break ;
621
642
case 'bottom-right' :
622
- cIcon . style . top = `calc(100% - ${ cIcon . height * 0.5 } px)` ;
643
+ cIcon . style . top = `${ - cIcon . height } px` ;
644
+ cIcon . style . left = `${ - cIcon . width } px` ;
623
645
break ;
624
646
case 'top-left' :
625
- cIcon . style . right = `calc(100% - ${ cIcon . width * 0.5 } px)` ;
626
647
break ;
627
648
case 'top-right' :
649
+ cIcon . style . left = `${ - cIcon . width } px` ;
628
650
break ;
629
651
case 'center' :
630
652
default :
631
- cIcon . style . top = `calc(50% - ${ cIcon . height * 0.5 } px) ` ;
632
- cIcon . style . right = `calc(50% - ${ cIcon . width * 0.5 } px) ` ;
653
+ cIcon . style . top = `${ - 0.5 * cIcon . height } px` ;
654
+ cIcon . style . left = `${ - 0.5 * cIcon . width } px` ;
633
655
break ;
634
656
}
635
657
@@ -700,6 +722,6 @@ style_properties.fill = Object.keys(style.fill);
700
722
style_properties . stroke = Object . keys ( style . stroke ) ;
701
723
style_properties . point = Object . keys ( style . point ) ;
702
724
style_properties . text = Object . keys ( style . text ) ;
703
- style_properties . icon = [ 'image' , 'size' , 'key' ] ;
725
+ style_properties . icon = Object . keys ( style . icon ) ;
704
726
705
727
export default Style ;
0 commit comments