FarmMapsLib/projects/common-map/src/fm-map/services/feature-icon.service.ts

41 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-02-12 19:38:14 +00:00
import { Injectable} from '@angular/core';
import { Feature } from 'ol';
import { Point } from 'ol/geom';
import * as extent from 'ol/extent';
@Injectable()
export class FeatureIconService {
2023-03-06 13:04:14 +00:00
getIconImageDataUrl(iconClass:string, backgroundColor = "#c80a6e",color = "#ffffff"): string {
const canvas = document.createElement('canvas');
2020-02-12 19:38:14 +00:00
canvas.width = 365;
canvas.height = 560;
2023-03-06 13:04:14 +00:00
const ctx = canvas.getContext('2d');
2020-02-12 19:38:14 +00:00
ctx.lineWidth = 6;
ctx.fillStyle = backgroundColor;
ctx.strokeStyle = "#000000";
2023-03-06 13:04:14 +00:00
const path = new Path2D("m182.9 551.7c0 0.1 0.2 0.3 0.2 0.3s175.2-269 175.2-357.4c0-130.1-88.8-186.7-175.4-186.9-86.6 0.2-175.4 56.8-175.4 186.9 0 88.4 175.3 357.4 175.3 357.4z");
2020-02-12 19:38:14 +00:00
ctx.fill(path)
2023-03-06 13:04:14 +00:00
let iconCharacter = "";
2020-02-12 19:38:14 +00:00
if (iconClass != null) {
2023-03-06 13:04:14 +00:00
const element = document.createElement("i");
2020-02-12 19:38:14 +00:00
element.style.display = "none";
element.className = iconClass;
document.body.appendChild(element);
iconCharacter = getComputedStyle(element, "::before").content.replace(/"/g, '');
2023-03-06 13:04:14 +00:00
const iconFont = "200px " +getComputedStyle(element, "::before").fontFamily
2020-02-12 19:38:14 +00:00
document.body.removeChild(element);
ctx.strokeStyle = color;
ctx.fillStyle = color;
ctx.lineWidth = 15;
ctx.font = iconFont;
2023-03-06 13:04:14 +00:00
const ts = ctx.measureText(iconCharacter);
2020-02-12 19:38:14 +00:00
ctx.fillText(iconCharacter, 182.9 - (ts.width / 2), 250);
ctx.strokeText(iconCharacter, 182.9 - (ts.width / 2), 250);
}
return canvas.toDataURL();
}
}