summaryrefslogtreecommitdiff
path: root/scraper/reports/leaflet.bezier.js
blob: 387e0717c82ba57cb0409d0393f0238336a2b751 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
L.SVG.include({
    _updatecurve: function (layer) {
        let svg_path = this._curvePointsToPath(layer._points);
        this._setPath(layer, svg_path);

        if (layer.options.animate) {
            let path = layer._path;
            let length = path.getTotalLength();

            if (!layer.options.dashArray) {
                path.style.strokeDasharray = length + ' ' + length;
            }

            if (layer._initialUpdate) {
                path.animate([
                    {strokeDashoffset: length},
                    {strokeDashoffset: 0}
                ], layer.options.animate);
                layer._initialUpdate = false;
            }
        }

        return svg_path;
    },


    _curvePointsToPath: function (points) {
        let point, curCommand, str = '';
        for (let i = 0; i < points.length; i++) {
            point = points[i];
            if (typeof point === 'string' || point instanceof String) {
                curCommand = point;
                str += curCommand;
            } else
                str += point.x + ',' + point.y + ' ';


        }
        return str || 'M0 0';
    },

});

let Bezier = L.Path.extend({
    options: {},
    initialize: function (path, icon, options) {

        if (!path.mid || path.mid[0] === undefined) {
            path.mid = this.getMidPoint(path.from, path.to, (path.from.deep ? path.from.deep : 4), path.from.slide);
        }

        L.setOptions(this, options);
        this._initialUpdate = true;
        this.setPath(path);
        this.icon = icon;

    },
    //Juast after path is added
    onAdd: function (map) {
        this._renderer._initPath(this);
        this._reset();
        this._renderer._addPath(this);

        // TODO ajust plane acording to zoom
        map.on('zoom', function(){

        });

    },
    // setAnimatePlane: function(path) {

    //     if (this.spaceship_img)
    //         this.spaceship_img.remove();

    //     let SnapSvg = Snap('.leaflet-overlay-pane>svg');

    //     let spaceship_img = this.spaceship_img = SnapSvg.image(this.icon.path).attr({
    //         visibility: "hidden"
    //     });


    //     let spaceship = SnapSvg.group(spaceship_img);
    //     let flight_path = SnapSvg.path(path).attr({
    //         'fill': 'none',
    //         'stroke': 'none'
    //     });

    //     let full_path_length = Snap.path.getTotalLength(flight_path);
    //     let half_path_length = full_path_length / 2;
    //     let third_path_length = full_path_length / 3;
    //     let forth_path_length = full_path_length / 4;


    //     let width = forth_path_length / this._map.getZoom();
    //     let height = forth_path_length / this._map.getZoom();

    //     width = Math.min(Math.max(width, 30), 64);
    //     height = Math.min(Math.max(height, 30), 64);


    //     let last_step = 0;


    //     Snap.animate(0, forth_path_length, function (step) {

    //         //show image when plane start to animate
    //         spaceship_img.attr({
    //             visibility: "visible"
    //         });

    //         spaceship_img.attr({width: width, height: height});

    //         last_step = step;

    //         let moveToPoint = Snap.path.getPointAtLength(flight_path, step);

    //         let x = moveToPoint.x - (width / 2);
    //         let y = moveToPoint.y - (height / 2);


    //         spaceship.transform('translate(' + x + ',' + y + ') rotate(' + (moveToPoint.alpha - 90) + ', ' + width / 2 + ', ' + height / 2 + ')');

    //     }, 2500, mina.easeout, function () {

    //         Snap.animate(forth_path_length, half_path_length, function (step) {

    //             last_step = step;
    //             let moveToPoint = Snap.path.getPointAtLength(flight_path, step);

    //             let x = moveToPoint.x - width / 2;
    //             let y = moveToPoint.y - height / 2;
    //             spaceship.transform('translate(' + x + ',' + y + ') rotate(' + (moveToPoint.alpha - 90) + ', ' + width / 2 + ', ' + height / 2 + ')');
    //         }, 7000, mina.easein, function () {
    //             //done

    //         });

    //     });


    // },
    getPath: function () {
        return this._coords;
    },
    setPath: function (path) {
        this._setPath(path);
        return this.redraw();
    },
    getBounds: function () {
        return this._bounds;
    },
    getMidPoint: function (from, to, deep, round_side = 'LEFT_ROUND') {

        let offset = 3.14;

        if (round_side === 'RIGHT_ROUND')
            offset = offset * -1;

        let latlngs = [];

        let latlng1 = from,
            latlng2 = to;

        let offsetX = latlng2.lng - latlng1.lng,
            offsetY = latlng2.lat - latlng1.lat;

        let r = Math.sqrt(Math.pow(offsetX, 2) + Math.pow(offsetY, 2)),
            theta = Math.atan2(offsetY, offsetX);

        let thetaOffset = (offset / (deep ? deep : 4));

        let r2 = (r / 2) / (Math.cos(thetaOffset)),
            theta2 = theta + thetaOffset;

        let midpointX = (r2 * Math.cos(theta2)) + latlng1.lng,
            midpointY = (r2 * Math.sin(theta2)) + latlng1.lat;

        let midpointLatLng = [midpointY, midpointX];

        latlngs.push(latlng1, midpointLatLng, latlng2);

        return midpointLatLng;
    },
    _setPath: function (path) {
        this._coords = path;
        this._bounds = this._computeBounds();
    },
    _computeBounds: function () {

        let bound = new L.LatLngBounds();

        bound.extend(this._coords.from);
        bound.extend(this._coords.to);//for single destination
        bound.extend(this._coords.mid);

        return bound;
    },
    getCenter: function () {
        return this._bounds.getCenter();
    },
    _update: function () {
        if (!this._map) {
            return;
        }
        this._updatePath();
    },
    _updatePath: function () {
        //animated plane
        let path = this._renderer._updatecurve(this);
        // this.setAnimatePlane(path);
    },
    _project: function () {

        this._points = [];

        this._points.push('M');

        let curPoint = this._map.latLngToLayerPoint(this._coords.from);
        this._points.push(curPoint);

        if (this._coords.mid) {
            this._points.push('Q');
            curPoint = this._map.latLngToLayerPoint(this._coords.mid);
            this._points.push(curPoint);
        }
        curPoint = this._map.latLngToLayerPoint(this._coords.to);
        this._points.push(curPoint);


    },


});

L.bezier = function (config, options) {
    let paths = [];
    for (let i = 0; config.path.length > i; i++) {
        let last_destination = false;
        for (let c = 0; config.path[i].length > c; c++) {

            let current_destination = config.path[i][c];
            if (last_destination) {
                let path_pair = {from: last_destination, to: current_destination};
                paths.push(new Bezier(path_pair, config.icon, options));
            }

            last_destination = config.path[i][c];
        }
    }
    return L.layerGroup(paths);

};