北京经济技术开发区经开区虚拟城市项目-【前端】-移动端Web
11
少年
2024-02-06 0261ec79dc29b9ff0e1068b060c7414234028e23
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
/*
    Copyright (c) 2015 Jean-Marc VIGLINO, 
    released under the CeCILL-B license (http://www.cecill.info/).
    github: https://github.com/Viglino/OL3-AnimatedCluster
    ol.layer.AnimatedCluster is a vector layer tha animate cluster 
    
    olx.layer.AnimatedClusterOptions: extend olx.layer.Options
    {    animationDuration {Number} animation duration in ms, default is 700ms 
        animationMethod {function} easing method to use, default ol.easing.easeOut
    }
*/
 
/**
* @constructor AnimatedCluster
* @extends {ol.layer.Vector}
* @param {olx.layer.AnimatedClusterOptions=} options
* @todo 
*/
ol.layer.AnimatedCluster = function(opt_options)
{    var options = opt_options || {};
 
    ol.layer.Vector.call (this, options);
    
    this.oldcluster = new ol.source.Vector();
    this.clusters = [];
    this.animation={start:false};
    this.set('animationDuration', typeof(options.animationDuration)=='number' ? options.animationDuration : 700);
    this.set('animationMethod', options.animationMethod || ol.easing.easeOut);
 
    // Save cluster before change
    this.getSource().on('change', this.saveCluster, this);
    // Animate the cluster
    this.on('precompose', this.animate, this);
    this.on('postcompose', this.postanimate, this);
};
ol.inherits (ol.layer.AnimatedCluster, ol.layer.Vector);
 
/** @private save cluster features before change
*/
ol.layer.AnimatedCluster.prototype.saveCluster = function()
{    this.oldcluster.clear();
    if (!this.get('animationDuration')) return;
    var features = this.getSource().getFeatures();
    if (features.length && features[0].get('features'))
    {    this.oldcluster.addFeatures (this.clusters);
        this.clusters = features.slice(0);
        this.sourceChanged = true;
    }
};
 
/** @private Get the cluster that contains a feature
*/
ol.layer.AnimatedCluster.prototype.getClusterForFeature = function(f, cluster)
{    for (var j=0, c; c=cluster[j]; j++)
    {    var features = cluster[j].get('features');
        if (features && features.length) 
        {    for (var k=0, f2; f2=features[k]; k++)
            {    if (f===f2) 
                {    return cluster[j];
                }
            }
        }
    }
    return false;
};
 
/** @private 
*/
ol.layer.AnimatedCluster.prototype.stopAnimation = function()
{    this.animation.start = false;
    this.animation.cA = [];
    this.animation.cB = [];
};
 
/** @private animate the cluster
*/
ol.layer.AnimatedCluster.prototype.animate = function(e)
{    var duration = this.get('animationDuration');
    if (!duration) return;
    var resolution = e.frameState.viewState.resolution;
    var a = this.animation;
    var time = e.frameState.time;
 
    // Start a new animation, if change resolution and source has changed
    if (a.resolution != resolution && this.sourceChanged)
    {    var extent = e.frameState.extent;
        if (a.resolution < resolution)
        {    extent = ol.extent.buffer(extent, 100*resolution);
            a.cA = this.oldcluster.getFeaturesInExtent(extent);
            a.cB = this.getSource().getFeaturesInExtent(extent);
            a.revers = false;
        }
        else
        {    extent = ol.extent.buffer(extent, 100*resolution);
            a.cA = this.getSource().getFeaturesInExtent(extent);
            a.cB = this.oldcluster.getFeaturesInExtent(extent);
            a.revers = true;
        }
        a.clusters = [];
        for (var i=0, c0; c0=a.cA[i]; i++)
        {    var f = c0.get('features');
            if (f && f.length) 
            {    var c = this.getClusterForFeature (f[0], a.cB);
                if (c) a.clusters.push({ f:c0, pt:c.getGeometry().getCoordinates() });
            }
        }
        // Save state
        a.resolution = resolution;
        this.sourceChanged = false;
 
        // No cluster or too much to animate
        if (!a.clusters.length || a.clusters.length>1000) 
        {    this.stopAnimation();
            return;
        }
        // Start animation from now
        time = a.start = (new Date()).getTime();
    }
 
    // Run animation
    if (a.start)
    {    var vectorContext = e.vectorContext;
        var d = (time - a.start) / duration;
        // Animation ends
        if (d > 1.0) 
        {    this.stopAnimation();
            d = 1;
        }
        d = this.get('animationMethod')(d);
        // Animate
        var style = this.getStyle();
        var stylefn = (typeof(style) == 'function') ? style : style.length ? function(){ return style; } : function(){ return [style]; } ;
        // Layer opacity
        e.context.save();
        e.context.globalAlpha = this.getOpacity();
        // Retina device
        var ratio = e.frameState.pixelRatio;
        for (var i=0, c; c=a.clusters[i]; i++)
        {    var pt = c.f.getGeometry().getCoordinates();
            if (a.revers)
            {    pt[0] = c.pt[0] + d * (pt[0]-c.pt[0]);
                pt[1] = c.pt[1] + d * (pt[1]-c.pt[1]);
            }
            else
            {    pt[0] = pt[0] + d * (c.pt[0]-pt[0]);
                pt[1] = pt[1] + d * (c.pt[1]-pt[1]);
            }
            // Draw feature
            var st = stylefn(c.f, resolution);
            /* Preserve pixel ration on retina */
            var geo = new ol.geom.Point(pt);
            for (var k=0; s=st[k]; k++)
            {    var sc;
                // OL < v4.3 : setImageStyle doesn't check retina
                var imgs = ol.Map.prototype.getFeaturesAtPixel ? false : s.getImage();
                if (imgs)
                {    sc = imgs.getScale(); 
                    imgs.setScale(sc*ratio); 
                }
                // OL3 > v3.14
                if (vectorContext.setStyle)
                {    vectorContext.setStyle(s);
                    vectorContext.drawGeometry(geo);
                }
                // older version
                else
                {    vectorContext.setImageStyle(imgs);
                    vectorContext.setTextStyle(s.getText());
                    vectorContext.drawPointGeometry(geo);
                }
                if (imgs) imgs.setScale(sc);
            }
            /*/
            var f = new ol.Feature(new ol.geom.Point(pt));
            for (var k=0; s=st[k]; k++)
            {    var imgs = s.getImage();
                var sc = imgs.getScale(); 
                imgs.setScale(sc*ratio); // drawFeature don't check retina
                vectorContext.drawFeature(f, s);
                imgs.setScale(sc);
            }
            /**/
        }
        e.context.restore();
        // tell OL3 to continue postcompose animation
        e.frameState.animate = true;
 
        // Prevent layer drawing (clip with null rect)
        e.context.save();
        e.context.beginPath();
        e.context.rect(0,0,0,0);
        e.context.clip();
        this.clip_ = true;
    }
 
    return;
};
 
/** @private remove clipping after the layer is drawn
*/
ol.layer.AnimatedCluster.prototype.postanimate = function(e)
{    if (this.clip_)
    {    e.context.restore();
        this.clip_ = false;
    }
};