From 132556da8106263b5ce441c38983f57e923dfdde Mon Sep 17 00:00:00 2001 From: Willem Dantuma Date: Wed, 19 Jan 2022 13:21:44 +0100 Subject: [PATCH] AW-3412 --- .../feature-thumbnail.component.ts | 103 ++++++++++++------ 1 file changed, 69 insertions(+), 34 deletions(-) diff --git a/projects/common-map/src/fm-map/components/feature-thumbnail/feature-thumbnail.component.ts b/projects/common-map/src/fm-map/components/feature-thumbnail/feature-thumbnail.component.ts index ec01356..d4b0116 100644 --- a/projects/common-map/src/fm-map/components/feature-thumbnail/feature-thumbnail.component.ts +++ b/projects/common-map/src/fm-map/components/feature-thumbnail/feature-thumbnail.component.ts @@ -1,4 +1,4 @@ -import { Component, Input, OnInit,ViewChild } from '@angular/core'; +import { Component, Input, AfterViewInit, ViewChild } from '@angular/core'; import { Feature} from 'ol'; import { Geometry } from 'ol/geom'; import * as extent from 'ol/extent'; @@ -10,51 +10,86 @@ import * as style from 'ol/style'; templateUrl: './feature-thumbnail.component.html', styleUrls: ['./feature-thumbnail.component.scss'] }) -export class GeometryThumbnailComponent implements OnInit { +export class GeometryThumbnailComponent implements AfterViewInit { + constructor() { } @ViewChild('canvas') canvas; @ViewChild('container') container; - @Input('feature') feature:Feature; - ngOnInit(): void { + private geometry:Geometry = null; + @Input() set feature(value:Feature) { + if(value) { + this.geometry = value.getGeometry(); + } else { + this.geometry = null; + } + this.render(this.canvas, + this.geometryStyle, + this.geometry, + this.width, + this.height); + }; + + private defaultStyle:style.Style = new style.Style({ + stroke: new style.Stroke({ color: 'black',width:1.5 }) + }); + private geometryStyle:style.Style = this.defaultStyle; + @Input() set fillColor(value:string) { + if(style) { + this.geometryStyle = new style.Style({ + stroke: new style.Stroke({ color: 'black',width:1.5 }), + fill: new style.Fill({color: value}) + }); + } else { + this.geometryStyle = this.defaultStyle + } + this.render(this.canvas, + this.geometryStyle, + this.geometry, + this.width, + this.height); } - render(canvas,width,height,geometry:Geometry) { - let renderContext = render.toContext(canvas.getContext( '2d'),{ size: [width, height] }); - - let strokeStyle = new style.Style({ - stroke: new style.Stroke({ color: 'black',width:1.5 }) - }); + private width:number = 0; + private height:number = 0; - let geom = geometry.clone(), - line = geom.getCoordinates()[0], - e = extent.boundingExtent( line ); + render(canvas,style:style.Style,geometry:Geometry,width:number,height:number) { + if(canvas && canvas.nativeElement && geometry && style) { + let renderContext = render.toContext(canvas.nativeElement.getContext( '2d'),{ size: [width, height] }); - let dxy = extent.getCenter(e), - sxy = [ - (width - 2 ) / extent.getWidth(e), - (height - 2 ) / extent.getHeight(e) - ]; - - let dx = dxy[0], - dy = dxy[1], - sx = sxy[0], - sy = sxy[1]; - - geom.translate( -dx, -dy ); - geom.scale( Math.min(sx, sy), -Math.min(sx, sy)); - geom.translate( width / 2, height / 2 ); - - renderContext.setStyle( strokeStyle ); - renderContext.drawGeometry( geom ); + let geom = geometry.clone(), + line = geom.getCoordinates()[0], + e = extent.boundingExtent( line ); + + let dxy = extent.getCenter(e), + sxy = [ + (width - 2 ) / extent.getWidth(e), + (height - 2 ) / extent.getHeight(e) + ]; + + let dx = dxy[0], + dy = dxy[1], + sx = sxy[0], + sy = sxy[1]; + + geom.translate( -dx, -dy ); + geom.scale( Math.min(sx, sy), -Math.min(sx, sy)); + geom.translate(width / 2,height / 2 ); + renderContext.setStyle( style ); + renderContext.drawGeometry( geom ); + } } ngAfterViewInit() { - this.render(this.canvas.nativeElement, - this.container.nativeElement.offsetWidth, - this.container.nativeElement.offsetHeight, - this.feature.getGeometry()); + this.width = this.container.nativeElement.offsetWidth; + this.height = this.container.nativeElement.offsetHeight; + this.render(this.canvas, + this.geometryStyle, + this.geometry, + this.width, + this.height); } + }