arrow.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. !(function (t) {
  2. 'function' == typeof define && define.amd ? define(t) : t()
  3. }(function () {
  4. 'use strict'; function t (t, i) {
  5. this._tmpRender = i; this._canvas = t = 'string' == typeof t ? document.getElementById(t) : t, this._ctx = t.getContext('2d'), this._width = t.width, this._height = t.height, this._weight = 5, this._outlineWidth = 1, this._outlineColor = 'black', this._min = 0, this._max = 1, this._data = [], this._tmpImage = {}, this.gradient({ 0: 'green', 0.5: 'yellow', 1: 'red' })
  6. } const l = { clipSegment (t, i, e, n, o) {
  7. let h, s, r, a = n ? this._lastCode : BM.LineUtil._getBitCode(t, e), l = BM.LineUtil._getBitCode(i, e); for (this._lastCode = l; ;) {
  8. if (!(a | l)) {
  9. return [t, i]
  10. } if (a & l) {
  11. return !1
  12. } h = a || l, s = BM.LineUtil._getEdgeIntersection(t, i, h, e, o), r = BM.LineUtil._getBitCode(s, e), h === a ? (s.z = t.z, t = s, a = r) : (s.z = i.z, i = s, l = r)
  13. }
  14. } }; t.prototype = { width (t) {
  15. return this._width = t, this
  16. }, url (t) {
  17. return this._url = t, this
  18. }, offset (t) {
  19. return this._offset = t, this
  20. }, height (t) {
  21. return this._height = t, this
  22. }, weight (t) {
  23. return this._weight = t, this
  24. }, outlineWidth (t) {
  25. return this._outlineWidth = t, this
  26. }, outlineColor (t) {
  27. return this._outlineColor = t, this
  28. }, gradient (t) {
  29. let i, e = document.createElement('canvas'), n = e.getContext('2d'), o = n.createLinearGradient(0, 0, 0, 256); for (i in e.width = 1, e.height = 256, t) {
  30. o.addColorStop(i, t[i])
  31. } return n.fillStyle = o, n.fillRect(0, 0, 1, 256), this._palette = n.getImageData(0, 0, 1, 256).data, this
  32. }, min (t) {
  33. return this._min = t, this
  34. }, max (t) {
  35. return this._max = t, this
  36. }, data (t) {
  37. return this._data = t, this
  38. }, add (t) {
  39. return this._data.push(t), this
  40. }, draw () {
  41. const t = this._ctx; return t.globalCompositeOperation = 'source-over', t.lineCap = 'round', this._drawOutline(t), this._drawHotline(t), this
  42. }, getRGBForValue (t) {
  43. t = Math.min(Math.max((t - this._min) / (this._max - this._min), 0), 0.999), t = 4 * Math.floor(256 * t); return [this._palette[t], this._palette[1 + t], this._palette[2 + t]]
  44. }, _drawOutline (t) {
  45. let i, e, n, o, h, s, r; if (this._outlineWidth) {
  46. for (i = 0, n = this._data.length; i < n; i++) {
  47. for (o = this._data[i], t.lineWidth = this._weight + 2 * this._outlineWidth, e = 1, h = o.length; e < h; e++) {
  48. s = o[e - 1], r = o[e], t.strokeStyle = this._outlineColor, t.beginPath(), t.moveTo(s.x, s.y), t.lineTo(r.x, r.y), t.stroke()
  49. }
  50. }
  51. }
  52. }, _calcImagePath (i, e, t, n) {
  53. const o = []; n = n || 0; const h = Math.sqrt(Math.pow(i.x - e.x, 2) + Math.pow(i.y - e.y, 2)), s = this._offset || 60, r = Math.floor(h / s); for (let t = n = 0; t <= r; t++) {
  54. o.push([i.x + t / r * (e.x - i.x), i.y + t / r * (e.y - i.y)])
  55. } return n = h % s, { path: o, offset: n }
  56. }, _drawHotline (e) {
  57. let t, n, i, o, h, s, r, a, l, u, _ = 0; e.lineWidth = this._weight; const d = this._url; if (d && !this._tmpImage[d]) {
  58. const t = new Image(); t.src = d, this._tmpImage[d] = [t, !1, []], t.onload = () => {
  59. this._tmpImage[d][1] = !0, this._tmpRender._update()
  60. }
  61. } for (t = 0, i = this._data.length; t < i; t++) {
  62. for (n = 1, h = (o = this._data[t]).length; n < h; n++) {
  63. s = o[n - 1], r = o[n], a = e.createLinearGradient(s.x, s.y, r.x, r.y), l = this.getRGBForValue(s.z), u = this.getRGBForValue(r.z), a.addColorStop(0, 'rgb(' + l.join(',') + ')'), a.addColorStop(1, 'rgb(' + u.join(',') + ')'), e.strokeStyle = a, e.beginPath(), e.moveTo(s.x, s.y), e.lineTo(r.x, r.y), e.stroke(); let i = Math.atan((r.y - s.y) / (r.x - s.x)); if (r.x - s.x < 0 && (i += Math.PI), this._tmpImage[d][1]) {
  64. const t = this._calcImagePath(s, r, i, _); _ = t.offset, t.path.map(t => {
  65. e.save(), e.translate(t[0], t[1]), e.rotate(i), e.drawImage(this._tmpImage[d][0], -this._weight / 2, -this._weight / 2, this._weight, this._weight), e.restore()
  66. })
  67. }
  68. }
  69. }
  70. } }; function i (t) {
  71. return BM.Browser.canvas ? new e(t) : null
  72. } var e = BM.Canvas.extend({ _initContainer () {
  73. BM.Canvas.prototype._initContainer.call(this), this._hotline = new t(this._container, this)
  74. }, _update () {
  75. BM.Canvas.prototype._update.call(this), this._hotline.width(this._container.width), this._hotline.height(this._container.height)
  76. }, _updatePoly (t) {
  77. let i; !this._drawing || (i = t._parts).length && (this._updateOptions(t), this._hotline.data(i).draw())
  78. }, _updateOptions (t) {
  79. null != t.options.min && this._hotline.min(t.options.min), null != t.options.max && this._hotline.max(t.options.max), null != t.options.url && this._hotline.url(t.options.url), null != t.options.weight && this._hotline.weight(t.options.weight), null != t.options.outlineWidth && this._hotline.outlineWidth(t.options.outlineWidth), null != t.options.outlineColor && this._hotline.outlineColor(t.options.outlineColor), t.options.gradient && this._hotline.gradient(t.options.gradient), t.options.offset && this._hotline.offset(t.options.offset)
  80. } }); BM.Hotline = BM.Polyline.extend({ statics: { Renderer: e, renderer: i }, options: { renderer: i(), min: 0, max: 1, gradient: { 0: 'green', 0.5: 'yellow', 1: 'red' }, weight: 5, outlineColor: 'black', outlineWidth: 1 }, _projectLatlngs (t, i, e) {
  81. let n, o, h = t[0] instanceof BM.LatLng, s = t.length; if (h) {
  82. for (o = [], n = 0; n < s; n++) {
  83. o[n] = this._map.latLngToLayerPoint(t[n]), o[n].z = t[n].alt || n, e.extend(o[n])
  84. }i.push(o)
  85. } else {
  86. for (n = 0; n < s; n++) {
  87. this._projectLatlngs(t[n], i, e)
  88. }
  89. }
  90. }, _clipPoints () {
  91. if (this.options.noClip) {
  92. this._parts = this._rings
  93. } else {
  94. this._parts = []; for (var t, i, e, n, o = this._parts, h = this._renderer._bounds, s = 0, r = 0, a = this._rings.length; s < a; s++) {
  95. for (t = 0, i = (n = this._rings[s]).length; t < i - 1; t++) {
  96. (e = l.clipSegment(n[t], n[t + 1], h, t, !0)) && (o[r] = o[r] || [], o[r].push(e[0]), e[1] === n[t + 1] && t !== i - 2 || (o[r].push(e[1]), r++))
  97. }
  98. }
  99. }
  100. }, _clickTolerance () {
  101. return this.options.weight / 2 + this.options.outlineWidth + (BM.Browser.touch ? 10 : 0)
  102. } }), BM.hotline = function (t, i) {
  103. return new BM.Hotline(t, i)
  104. }
  105. }))