-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathMarkerUtils.js
134 lines (123 loc) · 4.55 KB
/
MarkerUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright 2017, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import fontawesome from './font-awesome.json';
const cssJSON = {
fontawesome
};
import baseImageUrl from '../components/mapcontrols/annotations/img/markers_default.png';
import shadowImageUrl from '../components/mapcontrols/annotations/img/markers_shadow.png';
const baseImage = new Image();
const shadowImage = new Image();
baseImage.src = baseImageUrl;
shadowImage.src = shadowImageUrl;
const glyphs = {};
const loadGlyphs = (font) => {
const fontJSON = cssJSON[font];
return Object.keys(fontJSON).reduce((acc, key) => {
return {
...acc,
[key]: eval("'\\u" + fontJSON[key] + "'") // eslint-disable-line
};
}, {});
};
const extraMarkers = {
size: [36, 46],
shadowSize: [30, 42],
margin: [3, 2],
colors: ['red', 'orange-dark', 'orange', 'yellow', 'blue-dark', 'blue', 'cyan', 'purple', 'violet',
'pink', 'green-dark', 'green', 'green-light', 'black'],
shapes: ['circle', 'square', 'star', 'penta'],
icons: [baseImageUrl, shadowImageUrl],
images: [shadowImage, baseImage]
};
const getOffsets = (color, shape) => {
return [
-extraMarkers.colors.indexOf(color) * extraMarkers.size[0],
-extraMarkers.shapes.indexOf(shape) * extraMarkers.size[1]
];
};
const getGlyphOffset = (shape) => {
return ['square', 'penta'].includes(shape) ? 6 : 5;
};
const MarkerUtils = {
extraMarkers: {
...extraMarkers,
getOffsets,
markerToDataUrl: ({ iconColor, iconShape, iconGlyph }) => {
if (MarkerUtils.extraMarkers.images) {
let canvas = document.createElement('canvas');
const margin = extraMarkers.margin;
const size = extraMarkers.size;
const width = size[0] - (margin[0] * 2);
const height = size[1] - (margin[1] * 2);
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
if (iconShape) {
const offSet = getOffsets(iconColor, iconShape);
ctx.drawImage(extraMarkers.images[0], 0, 0, width, height); // shadowImage
ctx.drawImage(extraMarkers.images[1], Math.abs(offSet[0]), Math.abs(offSet[1]), size[0], size[1], -margin[0], -margin[1], size[0], size[1]); // iconImage
}
if (iconGlyph) {
// glyph
ctx.font = "14px FontAwesome";
ctx.fillStyle = "rgb(255,255,255)";
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.fillText((MarkerUtils.getGlyphs("fontawesome"))[iconGlyph] || '', (width / 2), (height / 2) - getGlyphOffset(iconShape));
}
const data = canvas.toDataURL("image/png");
canvas = null;
return data;
}
return null;
},
matches: (style, marker) => {
return style.iconColor === marker.color && style.iconShape === marker.shape;
},
getStyle: (marker) => {
return {
iconColor: marker.color,
iconShape: marker.shape
};
},
getGrid: () => {
return extraMarkers.shapes.map((s) => ({
name: s,
markers: extraMarkers.colors.map((m) => ({
name: m,
width: extraMarkers.size[0],
height: extraMarkers.size[1],
offsets: getOffsets(m, s),
style: {
color: m,
shape: s
},
thumbnailStyle: {
backgroundImage: "url(" + extraMarkers.icons[0] + ")",
width: extraMarkers.size[0] + "px",
height: extraMarkers.size[1] + "px",
backgroundPositionX: getOffsets(m, s)[0],
backgroundPositionY: getOffsets(m, s)[1],
cursor: "pointer"
}
}))
}));
}
},
getGlyphs: (font) => {
if (!glyphs[font]) {
glyphs[font] = loadGlyphs(font);
}
return glyphs[font];
}
};
MarkerUtils.markers = {
'extra': MarkerUtils.extraMarkers
};
export default MarkerUtils;