src/create_text_marker.js   A
last analyzed

Complexity

Total Complexity 4
Complexity/F 2

Size

Lines of Code 123
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
eloc 85
nc 1
dl 0
loc 123
rs 10
c 0
b 0
f 0
wmc 4
mnd 1
bc 3
fnc 2
bpm 1.5
cpm 2
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A create_text_marker.js ➔ createTextMarker 0 22 1
B create_text_marker.js ➔ generateCanvas 0 98 3
1
/** global: google */
2
import { IconObject } from "./icon_object.js";
3
4
import { getColors } from "./parsers.js";
5
const generateCanvas = function(options) {
6
    let canvas = document.createElement("canvas");
7
    let ancho = 30,
8
        alto = 40;
9
    canvas.width = ancho + 18;
10
    canvas.height = alto;
11
    let x = canvas.width / 2,
12
        y = canvas.height - 2,
13
        radius = ancho / 2,
14
        angulo = 0.6;
15
16
    let font = "'" + options.font + "'" || "Arial";
17
    let fontsize = options.fontsize || 11;
18
19
    let context = canvas.getContext("2d");
20
21
    context.clearRect(0, 0, canvas.width, canvas.height);
22
23
    let radius0 = 2 * radius,
24
        cx = x + 0.95 * radius0,
25
        cy = y + 0.45 * radius0;
26
27
    let grad = context.createLinearGradient(0, 0, 0, canvas.height),
28
        colors = getColors(options),
29
        color0 = colors[0],
30
        color1 = colors[1];
31
32
    grad.addColorStop(0, color0);
33
    grad.addColorStop(1, color1);
34
35
    context.fillStyle = grad;
36
    context.strokeStyle = "rgba(200,200,200,0.7)";
37
38
    context.beginPath();
39
40
    //arco izquierdo
41
    context.arc(
42
        cx - 1,
43
        cy,
44
        radius0,
45
        (9 * Math.PI) / 8,
46
        (-6 * Math.PI) / 8,
47
        false
48
    );
49
50
    // arco superior
51
    context.arc(x, (y - 7) / 2, radius, angulo, Math.PI - angulo, true);
52
53
    //arco derecho
54
    context.arc(
55
        2 * x - cx + 1,
56
        cy,
57
        radius0,
58
        (-0.95 * Math.PI) / 3,
59
        -Math.PI / 8,
60
        false
61
    );
62
    context.fill();
63
    context.stroke();
64
65
    context.beginPath();
66
    context.arc(x, 0.4 * y, (2 * radius) / 3, 0, 2 * Math.PI, false);
67
    context.fillStyle = "white";
68
    context.fill();
69
70
    context.beginPath();
71
72
    // Render Label
73
    //context.font = "11pt Arial";
74
    context.font = fontsize + "pt " + font;
75
    context.textBaseline = "top";
76
77
    let textWidth = context.measureText(options.label);
78
79
    if (textWidth.width > ancho || String(options.label).length > 3) {
80
        context.rect(
81
            x - 2 - textWidth.width / 2,
82
            y - 30,
83
            x - 2 + textWidth.width / 2,
84
            y - 23
85
        );
86
        context.fillStyle = "#F7F0F0";
87
        context.fill();
88
        context.stroke();
89
    }
90
91
    context.fillStyle = "black";
92
    context.strokeStyle = "black";
93
    // centre the text.
94
    context.fillText(
95
        options.label,
96
        1 + Math.floor(canvas.width / 2 - textWidth.width / 2),
97
        8
98
    );
99
    canvas.fillColor = color0;
100
101
    return canvas;
102
};
103
export function createTextMarker(theoptions) {
104
    let scale = theoptions.scale || 0.75,
105
        markerCanvas = generateCanvas(theoptions),
106
        markerOpts = {};
107
108
    theoptions.type = "textmarker";
109
110
    Object.assign(markerOpts, theoptions);
111
112
    Object.assign(markerOpts, {
113
        origin: { x: 0, y: 0 },
114
        anchor: { x: 24 * scale, y: 40 * scale },
115
        size: { width: 48, height: 40 },
116
        scaledSize: { width: 48 * scale, height: 40 * scale }
117
    });
118
119
    let url = markerCanvas.toDataURL(),
120
        fillColor = markerCanvas.fillColor,
121
        iconObj = new IconObject(url, fillColor, markerOpts);
122
123
    return iconObj;
124
}
125