1
|
|
|
function compact(array) { |
|
|
|
|
2
|
|
|
var index = -1, |
|
|
|
|
3
|
|
|
length = array ? array.length : 0, |
4
|
|
|
resIndex = 0, |
5
|
|
|
result = []; |
6
|
|
|
|
7
|
|
|
while (++index < length) { |
8
|
|
|
var value = array[index]; |
|
|
|
|
9
|
|
|
if (value) { |
10
|
|
|
result[resIndex++] = value; |
11
|
|
|
} |
12
|
|
|
} |
13
|
|
|
return result; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
function padHex(str_in) { |
17
|
|
|
if (('' + str_in).length === 1) { |
18
|
|
|
return '0' + String(str_in); |
19
|
|
|
} else { |
20
|
|
|
return String(str_in); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
var defaults = { |
|
|
|
|
25
|
|
|
h: 1, |
26
|
|
|
s: 78, // constant saturation |
27
|
|
|
l: 63, // constant luminance |
28
|
|
|
a: 1 |
29
|
|
|
}; |
30
|
|
|
|
31
|
|
|
function getColor(val, range) { |
32
|
|
|
defaults.h = Math.floor((360 / range) * val); |
33
|
|
|
return "hsla(" + defaults.h + "," + defaults.s + "%," + defaults.l + "%," + defaults.a + ")"; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
function getColor1() { |
37
|
|
|
return "hsla(" + defaults.h + "," + defaults.s + "%," + (defaults.l - 30) + "%," + defaults.a + ")"; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function parseHalf(foo) { |
41
|
|
|
return parseInt(foo / 2, 10); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
function darken(stringcolor, factor) { |
47
|
|
|
var darkercolor = {}; |
|
|
|
|
48
|
|
|
if (!factor) { |
49
|
|
|
factor = 1; |
50
|
|
|
} |
51
|
|
|
if (stringcolor.fillColor.indexOf('rgb') !== -1) { |
52
|
|
|
darkercolor.r = factor * parseHalf(stringcolor.r); |
53
|
|
|
darkercolor.g = factor * parseHalf(stringcolor.g); |
54
|
|
|
darkercolor.b = factor * parseHalf(stringcolor.b); |
55
|
|
|
darkercolor.fillColor = 'rgba(' + darkercolor.r + ',' + darkercolor.g + ',' + darkercolor.b + ',0.99)'; |
56
|
|
|
} else if (stringcolor.fillColor.indexOf('hsl') !== -1) { |
57
|
|
|
darkercolor.h = stringcolor.h; |
58
|
|
|
darkercolor.s = stringcolor.s; |
59
|
|
|
darkercolor.l = factor * stringcolor.l - 30; |
60
|
|
|
darkercolor.fillColor = 'hsl(' + darkercolor.h + ',' + darkercolor.s + '%,' + darkercolor.l + '%)'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return darkercolor; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
function getColors(options) { |
67
|
|
|
var color0, color1; |
|
|
|
|
68
|
|
|
if (options.index !== undefined && options.count > 0) { |
69
|
|
|
color0 = getColor(options.index, options.count); |
70
|
|
|
color1 = getColor1(); |
71
|
|
|
} else { |
72
|
|
|
var deccolor = toDecColor(options.color); |
|
|
|
|
73
|
|
|
color0 = deccolor.fillColor; |
74
|
|
|
color1 = darken(deccolor).fillColor; |
75
|
|
|
} |
76
|
|
|
return [color0, color1]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
function parseHex(hexstring, opacity, darkenfactor) { |
80
|
|
|
var hexcolor = { |
|
|
|
|
81
|
|
|
hex: hexstring |
82
|
|
|
}; |
83
|
|
|
darkenfactor = darkenfactor || 1; |
84
|
|
|
|
85
|
|
|
hexstring = hexstring.replace('#', ''); |
86
|
|
|
if (hexstring.length === 3) { |
87
|
|
|
hexstring = hexstring[0] + hexstring[0] + hexstring[1] + hexstring[1] + hexstring[2] + hexstring[2]; |
88
|
|
|
} |
89
|
|
|
if (isNaN(parseFloat(opacity, 10))) { |
90
|
|
|
opacity = 1; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
hexcolor.r = parseInt(darkenfactor * (parseInt(hexstring.substring(0, 2), 16)), 10); |
94
|
|
|
hexcolor.g = parseInt(darkenfactor * (parseInt(hexstring.substring(2, 4), 16)), 10); |
95
|
|
|
hexcolor.b = parseInt(darkenfactor * (parseInt(hexstring.substring(4, 6), 16)), 10); |
96
|
|
|
hexcolor.a = opacity; |
97
|
|
|
hexcolor.fillColor = 'rgba(' + hexcolor.r + ',' + hexcolor.g + ',' + hexcolor.b + ',' + hexcolor.a + ')'; |
98
|
|
|
hexcolor.strokeColor = ['rgba(' + parseHalf(hexcolor.r), parseHalf(hexcolor.g), parseHalf(hexcolor.b), hexcolor.a + ')'].join(','); |
99
|
|
|
hexcolor.rgb = hexcolor.fillColor; |
100
|
|
|
return hexcolor; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
function parseHSL(hslstring, opacity) { |
104
|
|
|
var hslcolor = {}, |
|
|
|
|
105
|
|
|
hslparts = compact(hslstring.split(/hsla?\(|\,|\)|\%/)); |
106
|
|
|
|
107
|
|
|
if (hslparts[3] === undefined) { |
108
|
|
|
hslparts[3] = 1; |
109
|
|
|
} |
110
|
|
|
if (isNaN(parseFloat(opacity, 10))) { |
111
|
|
|
opacity = 1; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
hslcolor.h = parseFloat(hslparts[0], 10); |
115
|
|
|
hslcolor.s = parseFloat(hslparts[1], 10); |
116
|
|
|
hslcolor.l = parseFloat(hslparts[2], 10); |
117
|
|
|
hslcolor.a = parseFloat(opacity * hslparts[3], 10); |
118
|
|
|
|
119
|
|
|
hslcolor.fillColor = 'hsla(' + hslcolor.h + ',' + hslcolor.s + '%,' + hslcolor.l + '%,' + hslcolor.a + ')'; |
120
|
|
|
hslcolor.strokeColor = 'hsla(' + hslcolor.h + ',' + hslcolor.s + '%,' + parseInt(hslcolor.l / 2, 10) + '%,' + hslcolor.a + ')'; |
121
|
|
|
hslcolor.hsl = hslcolor.fillColor; |
122
|
|
|
return hslcolor; |
123
|
|
|
}; |
|
|
|
|
124
|
|
|
|
125
|
|
|
function parseRGB(rgbstring, opacity, darkenfactor) { |
126
|
|
|
var rgbcolor = {}, |
|
|
|
|
127
|
|
|
rgbparts = compact(rgbstring.split(/rgba?\(|\,|\)/)); |
128
|
|
|
|
129
|
|
|
darkenfactor = darkenfactor || 1; |
130
|
|
|
|
131
|
|
|
if (rgbparts[3] === undefined) { |
132
|
|
|
rgbparts[3] = 1; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (isNaN(parseFloat(opacity, 10))) { |
136
|
|
|
opacity = 1; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
rgbcolor.r = parseInt(darkenfactor * (parseInt(rgbparts[0], 10) % 256), 10); |
140
|
|
|
rgbcolor.g = parseInt(darkenfactor * (parseInt(rgbparts[1], 10) % 256), 10); |
141
|
|
|
rgbcolor.b = parseInt(darkenfactor * (parseInt(rgbparts[2], 10) % 256), 10); |
142
|
|
|
rgbcolor.a = parseFloat(opacity * rgbparts[3], 10); |
143
|
|
|
rgbcolor.fillColor = 'rgba(' + rgbcolor.r + ',' + rgbcolor.g + ',' + rgbcolor.b + ',' + rgbcolor.a + ')'; |
144
|
|
|
rgbcolor.strokeColor = 'rgba(' + rgbcolor.r / 2 + ',' + rgbcolor.g / 2 + ',' + rgbcolor.b / 2 + ',' + rgbcolor.a + ')'; |
145
|
|
|
rgbcolor.rgb = rgbcolor.fillColor; |
146
|
|
|
return rgbcolor; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
function rgbToHSL(r, g, b, a) { |
150
|
|
|
r = (r % 256) / 255; |
151
|
|
|
g = (g % 256) / 255; |
152
|
|
|
b = (b % 256) / 255; |
153
|
|
|
if (a === undefined) { |
154
|
|
|
a = 1; |
155
|
|
|
} |
156
|
|
|
var max = Math.max(r, g, b), |
|
|
|
|
157
|
|
|
min = Math.min(r, g, b); |
158
|
|
|
var h, s, l = (max + min) / 2; |
|
|
|
|
159
|
|
|
|
160
|
|
|
if (max === min) { |
161
|
|
|
h = s = 0; // achromatic |
162
|
|
|
} else { |
163
|
|
|
var d = max - min; |
|
|
|
|
164
|
|
|
s = l > 0.5 ? d / (2 - max - min) : d / (max + min); |
165
|
|
|
switch (max) { |
166
|
|
|
case r: |
|
|
|
|
167
|
|
|
h = (g - b) / d + (g < b ? 6 : 0); |
168
|
|
|
break; |
169
|
|
|
case g: |
|
|
|
|
170
|
|
|
h = (b - r) / d + 2; |
171
|
|
|
break; |
172
|
|
|
case b: |
|
|
|
|
173
|
|
|
h = (r - g) / d + 4; |
174
|
|
|
break; |
175
|
|
|
default: |
176
|
|
|
h = 0; |
177
|
|
|
break; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
h /= 6; |
181
|
|
|
} |
182
|
|
|
var hsl = { |
|
|
|
|
183
|
|
|
h: Math.round(360 * h), |
184
|
|
|
s: Math.round(100 * s), |
185
|
|
|
l: Math.round(100 * l), |
186
|
|
|
a: Math.round(100 * a) / 100 |
187
|
|
|
}; |
188
|
|
|
|
189
|
|
|
hsl.fillColor = 'hsla(' + hsl.h + ',' + hsl.s + '%,' + hsl.l + '%,' + hsl.a + ')'; |
190
|
|
|
|
191
|
|
|
return hsl; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
function hslToRGB(h, s, l, a, darkenfactor) { |
195
|
|
|
var r, g, b; |
|
|
|
|
196
|
|
|
|
197
|
|
|
darkenfactor = darkenfactor || 1; |
198
|
|
|
h = parseFloat(h, 10) / 360; |
199
|
|
|
s = parseFloat(s, 10) / 100; |
200
|
|
|
l = parseFloat(l, 10) / 100; |
201
|
|
|
if (a === undefined) { |
202
|
|
|
a = 1; |
203
|
|
|
} |
204
|
|
|
if (s === 0) { |
205
|
|
|
r = g = b = l; // achromatic |
206
|
|
|
} else { |
207
|
|
|
var hue2rgb = function (p, q, t) { |
|
|
|
|
208
|
|
|
if (t < 0) { |
209
|
|
|
t += 1; |
210
|
|
|
} |
211
|
|
|
if (t > 1) { |
212
|
|
|
t -= 1; |
213
|
|
|
} |
214
|
|
|
if (t < 1 / 6) { |
215
|
|
|
return p + (q - p) * 6 * t; |
216
|
|
|
} |
217
|
|
|
if (t < 1 / 2) { |
218
|
|
|
return q; |
219
|
|
|
} |
220
|
|
|
if (t < 2 / 3) { |
221
|
|
|
return p + (q - p) * (2 / 3 - t) * 6; |
222
|
|
|
} |
223
|
|
|
return p; |
224
|
|
|
}; |
225
|
|
|
|
226
|
|
|
var q = l < 0.5 ? l * (1 + s) : l + s - l * s; |
|
|
|
|
227
|
|
|
var p = 2 * l - q; |
|
|
|
|
228
|
|
|
r = hue2rgb(p, q, h + 1 / 3); |
229
|
|
|
g = hue2rgb(p, q, h); |
230
|
|
|
b = hue2rgb(p, q, h - 1 / 3); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if (a === undefined) { |
234
|
|
|
a = 1; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
var rgb = { |
|
|
|
|
238
|
|
|
r: Math.round(r * 255 * darkenfactor), |
239
|
|
|
g: Math.round(g * 255 * darkenfactor), |
240
|
|
|
b: Math.round(b * 255 * darkenfactor), |
241
|
|
|
a: parseFloat(a, 10) |
242
|
|
|
}; |
243
|
|
|
|
244
|
|
|
rgb.fillColor = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + rgb.a + ')'; |
245
|
|
|
|
246
|
|
|
return rgb; |
247
|
|
|
|
248
|
|
|
}; |
|
|
|
|
249
|
|
|
|
250
|
|
|
function toDecColor(stringcolor) { |
251
|
|
|
var parsedcolor = {}; |
|
|
|
|
252
|
|
|
if (!stringcolor) { |
253
|
|
|
parsedcolor.fillColor = 'rgba(100,250,50,0.99)'; |
254
|
|
|
} else if (stringcolor.indexOf('rgb') !== -1) { |
255
|
|
|
parsedcolor = parseRGB(stringcolor); |
256
|
|
|
} else if (stringcolor.indexOf('hsl') !== -1) { |
257
|
|
|
parsedcolor = parseHSL(stringcolor); |
258
|
|
|
} else { |
259
|
|
|
parsedcolor = parseHex(stringcolor); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return parsedcolor; |
263
|
|
|
}; |
|
|
|
|
264
|
|
|
|
265
|
|
|
var IconObject = function (canvas, markerOpts) { |
|
|
|
|
266
|
|
|
this.url = canvas.toDataURL(); |
267
|
|
|
this.fillColor = canvas.fillColor; |
268
|
|
|
this.markerOpts = markerOpts; |
269
|
|
|
Object.assign(this, markerOpts); |
270
|
|
|
return this; |
271
|
|
|
}; |
272
|
|
|
IconObject.prototype.toJSON = function () { |
273
|
|
|
return { |
274
|
|
|
url: null, |
275
|
|
|
markerOpts: this.markerOpts |
276
|
|
|
}; |
277
|
|
|
}; |
278
|
|
|
|
279
|
|
|
var createTextMarker = function (theoptions) { |
|
|
|
|
280
|
|
|
|
281
|
|
|
var generateCanvas = function (options) { |
|
|
|
|
282
|
|
|
var canvas = document.createElement("canvas"); |
|
|
|
|
283
|
|
|
var ancho = 30, |
|
|
|
|
284
|
|
|
alto = 40; |
285
|
|
|
canvas.width = ancho + 18; |
286
|
|
|
canvas.height = alto; |
287
|
|
|
var x = canvas.width / 2, |
|
|
|
|
288
|
|
|
y = canvas.height - 2, |
289
|
|
|
radius = ancho / 2, |
290
|
|
|
angulo = 0.6; |
291
|
|
|
|
292
|
|
|
var font = "'" + options.font + "'" || 'Arial'; |
|
|
|
|
293
|
|
|
var fontsize = options.fontsize || 11; |
|
|
|
|
294
|
|
|
|
295
|
|
|
var context = canvas.getContext("2d"); |
|
|
|
|
296
|
|
|
|
297
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height); |
298
|
|
|
|
299
|
|
|
var radius0 = 2 * radius, |
|
|
|
|
300
|
|
|
cx = x + 0.95 * radius0, |
301
|
|
|
cy = y + 0.45 * radius0; |
302
|
|
|
|
303
|
|
|
var grad = context.createLinearGradient(0, 0, 0, canvas.height), |
|
|
|
|
304
|
|
|
colors = getColors(options), |
305
|
|
|
color0 = colors[0], |
306
|
|
|
color1 = colors[1]; |
307
|
|
|
|
308
|
|
|
grad.addColorStop(0, color0); |
309
|
|
|
grad.addColorStop(1, color1); |
310
|
|
|
|
311
|
|
|
context.fillStyle = grad; |
312
|
|
|
context.strokeStyle = 'rgba(200,200,200,0.7)'; |
313
|
|
|
|
314
|
|
|
context.beginPath(); |
315
|
|
|
|
316
|
|
|
//arco izquierdo |
317
|
|
|
context.arc(cx - 1, cy, radius0, 9 * Math.PI / 8, -6 * Math.PI / 8, false); |
318
|
|
|
|
319
|
|
|
// arco superior |
320
|
|
|
context.arc(x, (y - 7) / 2, radius, angulo, Math.PI - angulo, true); |
321
|
|
|
|
322
|
|
|
//arco derecho |
323
|
|
|
context.arc(2 * x - cx + 1, cy, radius0, -0.95 * Math.PI / 3, -Math.PI / 8, false); |
324
|
|
|
context.fill(); |
325
|
|
|
context.stroke(); |
326
|
|
|
|
327
|
|
|
context.beginPath(); |
328
|
|
|
context.arc(x, 0.40 * y, 2 * radius / 3, 0, 2 * Math.PI, false); |
329
|
|
|
context.fillStyle = 'white'; |
330
|
|
|
context.fill(); |
331
|
|
|
|
332
|
|
|
context.beginPath(); |
333
|
|
|
|
334
|
|
|
// Render Label |
335
|
|
|
//context.font = "11pt Arial"; |
336
|
|
|
context.font = fontsize + "pt " + font; |
337
|
|
|
context.textBaseline = "top"; |
338
|
|
|
|
339
|
|
|
var textWidth = context.measureText(options.label); |
|
|
|
|
340
|
|
|
|
341
|
|
|
if (textWidth.width > ancho || String(options.label).length > 3) { |
342
|
|
|
context.rect(x - 2 - textWidth.width / 2, y - 30, x - 2 + textWidth.width / 2, y - 23); |
343
|
|
|
context.fillStyle = '#F7F0F0'; |
344
|
|
|
context.fill(); |
345
|
|
|
context.stroke(); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
context.fillStyle = "black"; |
349
|
|
|
context.strokeStyle = "black"; |
350
|
|
|
// centre the text. |
351
|
|
|
context.fillText(options.label, 1 + Math.floor((canvas.width / 2) - (textWidth.width / 2)), 8); |
352
|
|
|
|
353
|
|
|
return canvas; |
354
|
|
|
|
355
|
|
|
}; |
356
|
|
|
theoptions.scale = theoptions.scale || 0.75; |
357
|
|
|
var markerCanvas = generateCanvas(theoptions), |
|
|
|
|
358
|
|
|
markerOpts = {}; |
359
|
|
|
|
360
|
|
|
theoptions.type = 'textmarker'; |
361
|
|
|
|
362
|
|
|
Object.assign(markerOpts, theoptions); |
363
|
|
|
|
364
|
|
|
if (window && window.google && window.google.maps) { |
365
|
|
|
Object.assign(markerOpts, { |
366
|
|
|
size: new google.maps.Size(48, 40), |
|
|
|
|
367
|
|
|
origin: new google.maps.Point(0, 0), |
368
|
|
|
anchor: new google.maps.Point(24 * theoptions.scale, 40 * theoptions.scale), |
369
|
|
|
scaledSize: new google.maps.Size(48 * theoptions.scale, 40 * theoptions.scale) |
370
|
|
|
}); |
371
|
|
|
} |
372
|
|
|
var iconObj = new IconObject(markerCanvas, markerOpts); |
|
|
|
|
373
|
|
|
|
374
|
|
|
return iconObj; |
375
|
|
|
}; |
376
|
|
|
|
377
|
|
|
|
378
|
|
|
|
|
|
|
|
379
|
|
|
var createClusterIcon = function (theoptions) { |
|
|
|
|
380
|
|
|
|
381
|
|
|
var generateClusterCanvas = function (options) { |
|
|
|
|
382
|
|
|
var canvas = options.canvas || document.createElement("canvas"), |
|
|
|
|
383
|
|
|
anchorX = 27, |
384
|
|
|
anchorY = 53, |
385
|
|
|
radius = (anchorX - 9), |
386
|
|
|
angulo = 1.1, |
|
|
|
|
387
|
|
|
font = options.font || 'fontello', |
388
|
|
|
fontsize = options.fontsize || 14, |
|
|
|
|
389
|
|
|
context = canvas.getContext("2d"), |
390
|
|
|
grad = context.createLinearGradient(0, 0, 0, anchorY); |
|
|
|
|
391
|
|
|
|
392
|
|
|
canvas.width = anchorX * 2; |
393
|
|
|
canvas.height = anchorY + 1; |
394
|
|
|
|
395
|
|
|
var colors = getColors(options), |
|
|
|
|
396
|
|
|
color0 = colors[0], |
|
|
|
|
397
|
|
|
color1 = colors[1]; |
|
|
|
|
398
|
|
|
|
399
|
|
|
|
400
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height); |
401
|
|
|
context.moveTo(anchorX, anchorY); |
402
|
|
|
|
403
|
|
|
var labelvalue = parseInt(options.label); |
|
|
|
|
404
|
|
|
if (labelvalue < 10) { |
405
|
|
|
color1 = 'orange'; |
406
|
|
|
fontsize = 14; |
407
|
|
|
} else if (labelvalue < 30) { |
408
|
|
|
color1 = 'red'; |
409
|
|
|
fontsize = 15; |
410
|
|
|
} else { |
411
|
|
|
color1 = 'purple'; |
412
|
|
|
fontsize = 16; |
413
|
|
|
} |
414
|
|
|
if (labelvalue > 99) { |
415
|
|
|
radius = radius + 3; |
416
|
|
|
context.setLineDash([5, 5]) |
417
|
|
|
context.beginPath(); |
418
|
|
|
context.arc(anchorX, 2 + (0.50 * anchorY), (radius + 7), 0, 2 * Math.PI, false); |
419
|
|
|
context.fillStyle = 'transparent'; |
420
|
|
|
context.strokeStyle = color1; |
421
|
|
|
context.lineWidth = 2; |
422
|
|
|
context.fill(); |
423
|
|
|
context.stroke(); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
context.setLineDash([5, 5]) |
427
|
|
|
context.beginPath(); |
428
|
|
|
context.arc(anchorX, 2 + (0.50 * anchorY), (radius + 2), 0, 2 * Math.PI, false); |
429
|
|
|
context.fillStyle = 'transparent'; |
430
|
|
|
context.strokeStyle = color1; |
431
|
|
|
context.lineWidth = 2; |
432
|
|
|
context.fill(); |
433
|
|
|
context.stroke(); |
434
|
|
|
|
435
|
|
|
// Círculo blanco |
436
|
|
|
context.setLineDash([5, 0]) |
437
|
|
|
context.beginPath(); |
438
|
|
|
context.arc(anchorX, 2 + (0.50 * anchorY), (radius - 3), 0, 2 * Math.PI, false); |
439
|
|
|
context.fillStyle = 'white'; |
440
|
|
|
context.strokeStyle = color1; |
441
|
|
|
context.lineWidth = 4; |
442
|
|
|
context.fill(); |
443
|
|
|
context.stroke(); |
444
|
|
|
|
445
|
|
|
context.beginPath(); |
446
|
|
|
|
447
|
|
|
context.font = 'normal normal normal ' + fontsize + 'px ' + font; |
448
|
|
|
console.log('context font', context.font); |
|
|
|
|
449
|
|
|
context.fillStyle = '#333'; |
450
|
|
|
context.textBaseline = "top"; |
451
|
|
|
var textWidth = context.measureText(options.label); |
|
|
|
|
452
|
|
|
|
453
|
|
|
// centre the text. |
454
|
|
|
context.fillText(options.label, Math.floor((canvas.width / 2) - (textWidth.width / 2)), 1 + Math.floor(canvas.height / 2 - fontsize / 2)); |
455
|
|
|
|
456
|
|
|
return canvas; |
457
|
|
|
|
458
|
|
|
}; |
459
|
|
|
theoptions.scale = theoptions.scale || 1; |
460
|
|
|
var markerCanvas = generateClusterCanvas(theoptions), |
|
|
|
|
461
|
|
|
markerOpts = {}, |
462
|
|
|
scale = theoptions.scale; |
463
|
|
|
|
464
|
|
|
Object.assign(markerOpts, theoptions); |
465
|
|
|
|
466
|
|
|
if (window && window.google && window.google.maps) { |
467
|
|
|
Object.assign(markerOpts, { |
468
|
|
|
size: new google.maps.Size(54, 48), |
|
|
|
|
469
|
|
|
origin: new google.maps.Point(0, 0), |
470
|
|
|
anchor: new google.maps.Point(27 * scale, 24 * scale), |
471
|
|
|
scaledSize: new google.maps.Size(54 * scale, 48 * scale) |
472
|
|
|
}); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
var iconObj = new IconObject(markerCanvas, markerOpts); |
|
|
|
|
476
|
|
|
|
477
|
|
|
return iconObj; |
478
|
|
|
}; |
479
|
|
|
|
480
|
|
|
var createFatMarkerIcon = function (theoptions) { |
|
|
|
|
481
|
|
|
|
482
|
|
|
var generateFatCanvas = function (options) { |
|
|
|
|
483
|
|
|
var canvas = options.canvas || document.createElement("canvas"), |
|
|
|
|
484
|
|
|
anchorX = 27, |
485
|
|
|
anchorY = 53, |
486
|
|
|
radius = (anchorX - 9), |
487
|
|
|
angulo = 1.1, |
488
|
|
|
font = options.font || 'fontello', |
489
|
|
|
fontsize = options.fontsize || 14, |
490
|
|
|
context = canvas.getContext("2d"), |
491
|
|
|
grad = context.createLinearGradient(0, 0, 0, anchorY); |
492
|
|
|
|
493
|
|
|
canvas.width = anchorX * 2; |
494
|
|
|
canvas.height = anchorY + 1; |
495
|
|
|
|
496
|
|
|
var colors = getColors(options), |
|
|
|
|
497
|
|
|
color0 = colors[0], |
498
|
|
|
color1 = colors[1]; |
499
|
|
|
|
500
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height); |
501
|
|
|
|
502
|
|
|
grad.addColorStop(0, color0); |
503
|
|
|
grad.addColorStop(1, color1); |
504
|
|
|
|
505
|
|
|
context.fillStyle = grad; |
506
|
|
|
context.strokeStyle = color1; |
507
|
|
|
context.beginPath(); |
508
|
|
|
|
509
|
|
|
context.moveTo(anchorX, anchorY); |
510
|
|
|
|
511
|
|
|
// arco superior |
512
|
|
|
context.arc(anchorX, 2 + (0.50 * anchorY), radius, angulo, Math.PI - angulo, true); |
513
|
|
|
|
514
|
|
|
//punta inferior |
515
|
|
|
context.lineTo(anchorX, anchorY); |
516
|
|
|
|
517
|
|
|
context.fill(); |
518
|
|
|
context.stroke(); |
519
|
|
|
|
520
|
|
|
// Círculo blanco |
521
|
|
|
context.beginPath(); |
522
|
|
|
context.arc(anchorX, 2 + (0.50 * anchorY), (radius - 3), 0, 2 * Math.PI, false); |
523
|
|
|
context.fillStyle = 'white'; |
524
|
|
|
context.fill(); |
525
|
|
|
|
526
|
|
|
context.beginPath(); |
527
|
|
|
|
528
|
|
|
context.font = 'normal normal normal ' + fontsize + 'px ' + font; |
529
|
|
|
//console.log('context font', context.font); |
530
|
|
|
context.fillStyle = color1; |
531
|
|
|
context.textBaseline = "top"; |
532
|
|
|
var textWidth = context.measureText(options.unicodelabel); |
|
|
|
|
533
|
|
|
|
534
|
|
|
// centre the text. |
535
|
|
|
context.fillText(options.unicodelabel, Math.floor((canvas.width / 2) - (textWidth.width / 2)), 1 + Math.floor(canvas.height / 2 - fontsize / 2)); |
|
|
|
|
536
|
|
|
canvas.fillColor = color0; |
537
|
|
|
return canvas; |
538
|
|
|
|
539
|
|
|
}; |
540
|
|
|
var scale = theoptions.scale || 1, |
|
|
|
|
541
|
|
|
markerCanvas = generateFatCanvas(theoptions), |
542
|
|
|
markerOpts = {}; |
543
|
|
|
|
544
|
|
|
theoptions.type = 'fatmarker'; |
545
|
|
|
|
546
|
|
|
Object.assign(markerOpts, theoptions); |
547
|
|
|
|
548
|
|
|
if (window && window.google && window.google.maps) { |
549
|
|
|
Object.assign(markerOpts, { |
550
|
|
|
size: new google.maps.Size(54, 48), |
|
|
|
|
551
|
|
|
origin: new google.maps.Point(0, 0), |
552
|
|
|
anchor: new google.maps.Point(21 * scale, 36 * scale), |
553
|
|
|
scaledSize: new google.maps.Size(42 * scale, 36 * scale), |
554
|
|
|
scale: scale |
555
|
|
|
}); |
556
|
|
|
} |
557
|
|
|
var iconObj = new IconObject(markerCanvas, markerOpts); |
|
|
|
|
558
|
|
|
return iconObj; |
559
|
|
|
}; |
560
|
|
|
|
561
|
|
|
var createTransparentMarkerIcon = function (theoptions) { |
|
|
|
|
562
|
|
|
|
563
|
|
|
var generateTransparentCanvas = function (options) { |
|
|
|
|
564
|
|
|
var canvas = options.canvas || document.createElement("canvas"), |
|
|
|
|
565
|
|
|
context = canvas.getContext("2d"), |
566
|
|
|
font = options.font || 'fontello', |
567
|
|
|
fontsize = options.fontsize || 26; |
568
|
|
|
|
569
|
|
|
canvas.width = 54; |
570
|
|
|
canvas.height = 48; |
571
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height); |
572
|
|
|
|
573
|
|
|
/*context.rect(1, 1, canvas.width - 2, canvas.height - 2); |
574
|
|
|
context.lineWidth = 1; |
575
|
|
|
context.strokeStyle = 'black'; |
576
|
|
|
context.stroke();*/ |
577
|
|
|
|
578
|
|
|
var colors = getColors(options), |
|
|
|
|
579
|
|
|
color0 = colors[0], |
580
|
|
|
color1 = colors[1]; |
581
|
|
|
context.beginPath(); |
582
|
|
|
|
583
|
|
|
if (options.shadow) { |
584
|
|
|
|
585
|
|
|
context.font = 'normal normal normal ' + fontsize + 'px ' + font; |
586
|
|
|
|
587
|
|
|
context.textBaseline = "top"; |
588
|
|
|
var textWidth = context.measureText(options.unicodelabel), |
|
|
|
|
589
|
|
|
text_x = Math.floor((canvas.width / 2) - (textWidth.width / 2)); |
590
|
|
|
|
591
|
|
|
context.shadowOffsetX = -2; |
592
|
|
|
context.shadowOffsetY = -2; |
593
|
|
|
context.shadowBlur = 0; |
594
|
|
|
|
595
|
|
|
context.fillStyle = '#FFFFFF'; |
596
|
|
|
context.shadowColor = '#666666'; |
597
|
|
|
|
598
|
|
|
context.fillText(options.unicodelabel, text_x - 4, 2); |
599
|
|
|
context.fillText(options.unicodelabel, text_x, 5); |
600
|
|
|
context.fillStyle = color0; |
601
|
|
|
context.fillText(options.unicodelabel, text_x + 4, 8); |
602
|
|
|
|
603
|
|
|
context.strokeStyle = '#FFFFFF'; |
604
|
|
|
context.strokeText(options.unicodelabel, text_x + 4, 8); |
605
|
|
|
|
606
|
|
|
} else { |
607
|
|
|
|
608
|
|
|
context.font = 'normal normal normal ' + (fontsize - 3) + 'px ' + font; |
609
|
|
|
|
610
|
|
|
context.textBaseline = "top"; |
611
|
|
|
var textmetric = context.measureText(options.unicodelabel), |
|
|
|
|
612
|
|
|
text_x = Math.floor((canvas.width / 2) - (textmetric.width / 2)); |
|
|
|
|
613
|
|
|
|
614
|
|
|
//console.debug('textmetric', textmetric); |
615
|
|
|
|
616
|
|
|
context.shadowOffsetX = 2; |
617
|
|
|
context.shadowOffsetY = 2; |
618
|
|
|
context.shadowBlur = 0; |
619
|
|
|
context.shadowColor = '#FFFFFF'; |
620
|
|
|
context.fillStyle = color0; |
621
|
|
|
context.fillText(options.unicodelabel, text_x + 1, 6); |
622
|
|
|
|
623
|
|
|
context.shadowOffsetX = 2; |
624
|
|
|
context.shadowOffsetY = 2; |
625
|
|
|
context.shadowBlur = 1; |
626
|
|
|
context.shadowColor = '#FFFFFF'; |
627
|
|
|
context.strokeStyle = color1; |
628
|
|
|
context.strokeText(options.unicodelabel, text_x + 1, 6); |
629
|
|
|
|
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
canvas.fillColor = color0; |
633
|
|
|
|
634
|
|
|
return canvas; |
635
|
|
|
|
636
|
|
|
}; |
637
|
|
|
|
638
|
|
|
theoptions.scale = theoptions.scale || 1; |
639
|
|
|
theoptions.fontsize = theoptions.fontsize || 26; |
640
|
|
|
|
641
|
|
|
var markerCanvas = generateTransparentCanvas(theoptions), |
|
|
|
|
642
|
|
|
markerOpts = {}; |
643
|
|
|
|
644
|
|
|
var scale = theoptions.scale; |
|
|
|
|
645
|
|
|
/*if (theoptions.shadow) { |
646
|
|
|
scale = 0.9 * scale; |
647
|
|
|
}*/ |
648
|
|
|
theoptions.type = 'transparent'; |
649
|
|
|
|
650
|
|
|
Object.assign(markerOpts, theoptions); |
651
|
|
|
|
652
|
|
|
if (window.google && window.google.maps) { |
653
|
|
|
Object.assign(markerOpts, { |
654
|
|
|
size: new google.maps.Size(54 * scale, 48 * scale), |
|
|
|
|
655
|
|
|
origin: new google.maps.Point(0, 0), |
656
|
|
|
anchor: new google.maps.Point(27 * scale, 24 * scale), |
657
|
|
|
scaledSize: new google.maps.Size(54 * scale, 48 * scale) |
658
|
|
|
}); |
659
|
|
|
} |
660
|
|
|
var iconObj = new IconObject(markerCanvas, markerOpts); |
|
|
|
|
661
|
|
|
|
662
|
|
|
return iconObj; |
663
|
|
|
}; |
664
|
|
|
|
665
|
|
|
|
666
|
|
|
|
667
|
|
|
|
|
|
|
|
668
|
|
|
var MarkerFactory = { |
|
|
|
|
669
|
|
|
createTransparentMarkerIcon: createTransparentMarkerIcon, |
670
|
|
|
createFatMarkerIcon: createFatMarkerIcon, |
671
|
|
|
createTextMarker: createTextMarker, |
672
|
|
|
/** |
673
|
|
|
* Receives a color string rgb(a), hsl(a) or hex, returns its components |
674
|
|
|
* in rgba and hsla, with optional transparency |
675
|
|
|
* plus a darkened version (default is half of each RGB component) and a |
|
|
|
|
676
|
|
|
* |
677
|
|
|
* @param {string} somecolor - A color string in rgb(a), hsl(a) or hex format |
678
|
|
|
* @param {Number} [opacity=1] - Opacity to apply to the color |
|
|
|
|
679
|
|
|
* @param {Number} [darkenfactor=1] - How much darker should the resulting color be |
|
|
|
|
680
|
|
|
* |
|
|
|
|
681
|
|
|
* @return {Object} input color parsed and modified as requested |
682
|
|
|
*/ |
683
|
|
|
parseColorString: function (somecolor, opacity, darkenfactor) { |
684
|
|
|
var parsedcolor = { |
|
|
|
|
685
|
|
|
original: somecolor |
686
|
|
|
}, |
687
|
|
|
hsl, rgb; |
688
|
|
|
|
689
|
|
|
darkenfactor = darkenfactor || 1; |
690
|
|
|
opacity = opacity || 1; |
691
|
|
|
|
692
|
|
|
if (somecolor.indexOf('hsl') !== -1) { |
693
|
|
|
hsl = parseHSL(somecolor, opacity); |
694
|
|
|
rgb = hslToRGB(hsl.h, hsl.s, hsl.l, hsl.a, darkenfactor); |
695
|
|
|
|
696
|
|
|
} else { |
697
|
|
|
if (somecolor.indexOf('rgb') !== -1) { |
|
|
|
|
698
|
|
|
rgb = parseRGB(somecolor, opacity, darkenfactor); |
699
|
|
|
} else { |
700
|
|
|
rgb = parseHex(somecolor, opacity, darkenfactor); |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
|
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
|
707
|
|
|
hsl = rgbToHSL(rgb.r, rgb.g, rgb.b, rgb.a); |
708
|
|
|
|
709
|
|
|
|
710
|
|
|
parsedcolor.hsl = { |
711
|
|
|
h: hsl.h, |
712
|
|
|
s: hsl.s, |
713
|
|
|
l: hsl.l, |
714
|
|
|
a: hsl.a |
715
|
|
|
}; |
716
|
|
|
parsedcolor.rgb = { |
717
|
|
|
r: rgb.r, |
718
|
|
|
g: rgb.g, |
719
|
|
|
b: rgb.b, |
720
|
|
|
a: rgb.a |
721
|
|
|
}; |
722
|
|
|
|
723
|
|
|
|
724
|
|
|
|
|
|
|
|
725
|
|
|
parsedcolor.fillColor = rgb.fillColor; |
726
|
|
|
parsedcolor.rgba = rgb.fillColor; |
727
|
|
|
parsedcolor.hsla = hsl.fillColor; |
728
|
|
|
parsedcolor.strokeColor = rgb.strokeColor; |
729
|
|
|
parsedcolor.hex = ['#', padHex(rgb.r.toString(16)), padHex(rgb.g.toString(16)), padHex(rgb.b.toString(16))].join(''); |
730
|
|
|
return parsedcolor; |
731
|
|
|
}, |
732
|
|
|
/** |
733
|
|
|
* Generates an google maps marker (or an image as dataurl from the given options) |
734
|
|
|
* |
735
|
|
|
* @param {Object} options The options |
736
|
|
|
* @return {Object} { description_of_the_return_value } |
737
|
|
|
*/ |
738
|
|
|
autoIcon: function (options) { |
739
|
|
|
|
740
|
|
|
if (typeof (options) !== 'object') { |
741
|
|
|
console.warn('autoIcon expects an object as its only parameter'); |
742
|
|
|
return null; |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
options.label = String(options.label || 'A'); |
746
|
|
|
options.color = options.color || '#FF0000'; |
747
|
|
|
|
748
|
|
|
// unless explicitly set to false, the icon doesn't have a marker-like wrapper |
749
|
|
|
if (options.transparent_background === undefined) { |
750
|
|
|
options.transparent_background = true; |
751
|
|
|
} |
752
|
|
|
|
753
|
|
|
if (options.label.length === 4 || options.label.substring(0, 2) === '0x') { |
754
|
|
|
|
755
|
|
|
|
756
|
|
|
options.font = options.font || 'fontello'; |
757
|
|
|
options.label = (options.label || 'e836').slice(-4); |
758
|
|
|
options.unicodelabel = String.fromCharCode('0x' + options.label); |
759
|
|
|
options.scale = options.scale || 1; |
760
|
|
|
|
761
|
|
|
if (options.transparent_background) { |
762
|
|
|
console.log('createTransparentMarkerIcon', options.font); |
|
|
|
|
763
|
|
|
return MarkerFactory.createTransparentMarkerIcon(options); |
764
|
|
|
} else { |
765
|
|
|
console.log('createFatMarkerIcon', options.font); |
766
|
|
|
return MarkerFactory.createFatMarkerIcon(options); |
767
|
|
|
} |
768
|
|
|
} else if (options.shadow) { |
769
|
|
|
return createClusterIcon(options); |
770
|
|
|
} else { |
771
|
|
|
options.scale = options.scale || 0.75; |
772
|
|
|
options.label = String(options.label || 'A'); |
773
|
|
|
options.fontsize = options.fontsize || 11; |
774
|
|
|
options.font = options.font || 'Arial'; |
775
|
|
|
// This is text I should print literally |
776
|
|
|
return MarkerFactory.createTextMarker(options); |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
} |
780
|
|
|
}; |
781
|
|
|
|
782
|
|
|
|
783
|
|
|
export { |
784
|
|
|
MarkerFactory |
785
|
|
|
}; |
786
|
|
|
export default MarkerFactory; |
|
|
|
|