派生自 wuyushui/SewerAndRainNetwork

徐旺旺
2021-03-30 f305077e82b1eb1edc45da2d206e976fdb89f66b
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
// @class PolyLine
const L = window.L
L.Path.mergeOptions({
  // @option dashSpeed: Number
  // The speed of the dash array, in pixels per second
  dashSpeed: 0
})
 
var _originalBeforeAdd = L.Path.prototype.beforeAdd
 
L.Path.include({
 
  beforeAdd: function (map) {
    _originalBeforeAdd.bind(this)(map)
 
    if (this.options.dashSpeed) {
      this._lastDashFrame = performance.now()
      this._dashFrame = L.Util.requestAnimFrame(this._onDashFrame.bind(this))
    }
  },
 
  _onDashFrame: function () {
    if (!this._renderer) {
      return
    }
 
    var now = performance.now()
    var dashOffsetDelta = (now - this._lastDashFrame) * this.options.dashSpeed / 1000
 
    this.options.dashOffset = Number(this.options.dashOffset || 0) + dashOffsetDelta
    this._renderer._updateStyle(this)
 
    this._lastDashFrame = performance.now()
 
    this._dashFrame = L.Util.requestAnimFrame(this._onDashFrame.bind(this))
  },
 
  _offDashFrame: function () {
    L.Util.cancelAnimFrame(this._dashFrame)
  },
 
  onRemove: function () {
    this._renderer._removePath(this)
    this._offDashFrame()
  }
})
 
// 针对Canvas添加dashOffset参数,解决Canvas下无法实现动态线问题
L.Canvas.include({
  _updateDashArray: function (layer) {
    if (typeof layer.options.dashArray === 'string') {
      var parts = layer.options.dashArray.split(/[, ]+/)
      var dashArray = []
      var dashValue
      var i
      for (i = 0; i < parts.length; i++) {
        dashValue = Number(parts[i])
        // Ignore dash array containing invalid lengths
        if (isNaN(dashValue)) {
          return
        }
        dashArray.push(dashValue)
      }
      layer.options._dashArray = dashArray
    } else {
      layer.options._dashArray = layer.options.dashArray
    }
 
    if (layer.options.dashOffset) {
      layer.options._dashOffset = layer.options.dashOffset
    }
  },
  _fillStroke: function (ctx, layer) {
    var options = layer.options
 
    if (options.fill) {
      ctx.globalAlpha = options.fillOpacity
      ctx.fillStyle = options.fillColor || options.color
      ctx.fill(options.fillRule || 'evenodd')
    }
 
    if (options.stroke && options.weight !== 0) {
      if (ctx.setLineDash) {
        ctx.setLineDash((layer.options && layer.options._dashArray) || [])
      }
      if (layer.options._dashOffset) {
        ctx.lineDashOffset = layer.options._dashOffset
      }
      ctx.globalAlpha = options.opacity
      ctx.lineWidth = options.weight
      ctx.strokeStyle = options.color
      ctx.lineCap = options.lineCap
      ctx.lineJoin = options.lineJoin
      ctx.stroke()
    }
  }
})