派生自 wuyushui/SewerAndRainNetwork

陈泽平
2021-05-21 bb588bddeaee7ea617f8ad019ba9f5e1825d9e27
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
/* eslint-disable */
// Adds a fade-out animation to grid layers when they're removed from the map
 
(function () {
  var gridProto = L.GridLayer.prototype
  var onRemoveProto = gridProto.onRemove
  var onAddProto = gridProto.onAdd
  var fadeDuration = 200
 
  L.GridLayer.include({
 
    onAdd: function (map) {
      if (this._fadeOutTime) {
        var now = performance.now() || (+new Date())
        L.Util.cancelAnimFrame(this._fadeOutFrame)
        this._fadeOutTime = now + fadeDuration - this._fadeOutTime + now
        L.Util.requestAnimFrame(this._fadeIn, this)
      } else {
        onAddProto.call(this, map)
      }
    },
 
    onRemove: function (map) {
      if (this._fadeOutTime) {
        // We're removing this *again* quickly after removing and re-adding
        var now = performance.now() || (+new Date())
 
        this._fadeOutTime = now + fadeDuration - this._fadeOutTime + now
      }
      this._fadeOutTime = (performance.now() || (+new Date())) + fadeDuration * 2
      this._fadeOutMap = this._map
 
      L.Util.requestAnimFrame(this._fadeOut, this)
    },
 
    _fadeOut: function () {
      if (!this._fadeOutTime || !this._container) { return }
 
      var now = performance.now() || (+new Date())
 
      var opacity = Math.min((this._fadeOutTime - now) / fadeDuration, 1)
      // console.log('fadeout:', opacity);
      if (opacity < 0) {
        this._fadeOutTime = false
 
        onRemoveProto.call(this, this._fadeOutMap)
 
        return
      }
 
      L.DomUtil.setOpacity(this._container, opacity * this.options.opacity)
 
      this._fadeOutFrame = L.Util.requestAnimFrame(this._fadeOut, this)
    },
 
    // Only runs when the gridlayer is quickly re-added while it's being faded out
    _fadeIn: function _fadeIn () {
      if (!this._fadeOutTime || !this._container) { return }
 
      var now = performance.now() || (+new Date())
 
      var opacity = (now - this._fadeOutTime) / fadeDuration
      // console.log('fadein:', opacity);
 
      if (opacity > 1) {
        this._fadeOutTime = false
        return
      }
 
      L.DomUtil.setOpacity(this._container, opacity * this.options.opacity)
 
      L.Util.requestAnimFrame(this._fadeIn, this)
    }
 
  })
})()