This commit is contained in:
Willem Dantuma 2022-01-19 13:21:44 +01:00
parent c2350eec52
commit 132556da81

View File

@ -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 { Feature} from 'ol';
import { Geometry } from 'ol/geom'; import { Geometry } from 'ol/geom';
import * as extent from 'ol/extent'; import * as extent from 'ol/extent';
@ -10,23 +10,54 @@ import * as style from 'ol/style';
templateUrl: './feature-thumbnail.component.html', templateUrl: './feature-thumbnail.component.html',
styleUrls: ['./feature-thumbnail.component.scss'] styleUrls: ['./feature-thumbnail.component.scss']
}) })
export class GeometryThumbnailComponent implements OnInit { export class GeometryThumbnailComponent implements AfterViewInit {
constructor() { } constructor() { }
@ViewChild('canvas') canvas; @ViewChild('canvas') canvas;
@ViewChild('container') container; @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);
};
render(canvas,width,height,geometry:Geometry) { private defaultStyle:style.Style = new style.Style({
let renderContext = render.toContext(canvas.getContext( '2d'),{ size: [width, height] });
let strokeStyle = new style.Style({
stroke: new style.Stroke({ color: 'black',width:1.5 }) 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);
}
private width:number = 0;
private height:number = 0;
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 geom = geometry.clone(), let geom = geometry.clone(),
line = geom.getCoordinates()[0], line = geom.getCoordinates()[0],
@ -45,16 +76,20 @@ export class GeometryThumbnailComponent implements OnInit {
geom.translate( -dx, -dy ); geom.translate( -dx, -dy );
geom.scale( Math.min(sx, sy), -Math.min(sx, sy)); geom.scale( Math.min(sx, sy), -Math.min(sx, sy));
geom.translate( width / 2, height / 2 ); geom.translate(width / 2,height / 2 );
renderContext.setStyle( style );
renderContext.setStyle( strokeStyle );
renderContext.drawGeometry( geom ); renderContext.drawGeometry( geom );
} }
}
ngAfterViewInit() { ngAfterViewInit() {
this.render(this.canvas.nativeElement, this.width = this.container.nativeElement.offsetWidth;
this.container.nativeElement.offsetWidth, this.height = this.container.nativeElement.offsetHeight;
this.container.nativeElement.offsetHeight, this.render(this.canvas,
this.feature.getGeometry()); this.geometryStyle,
this.geometry,
this.width,
this.height);
} }
} }