labelslayer.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. define(["leaflet"],
  2. function (L) {
  3. return L.TileLayer.Canvas.extend({
  4. setData: function (d) {
  5. this.data = d
  6. this.redraw()
  7. },
  8. drawTile: function (canvas, tilePoint) {
  9. function getTileBBox(s, map, tileSize, margin) {
  10. var tl = map.unproject([s.x - margin, s.y - margin])
  11. var br = map.unproject([s.x + margin + tileSize, s.y + margin + tileSize])
  12. return [br.lat, tl.lng, tl.lat, br.lng]
  13. }
  14. if (!this.data)
  15. return
  16. var tileSize = this.options.tileSize
  17. var s = tilePoint.multiplyBy(tileSize)
  18. var map = this._map
  19. function projectNodes(d) {
  20. var p = map.project([d.node.nodeinfo.location.latitude, d.node.nodeinfo.location.longitude])
  21. p.x -= s.x
  22. p.y -= s.y
  23. return {p: p, o: d.node}
  24. }
  25. var margin = 256
  26. var bbox = getTileBBox(s, map, tileSize, margin)
  27. var nodesOnline = this.data.online.search(bbox).map(projectNodes)
  28. var nodesOffline = this.data.offline.search(bbox).map(projectNodes)
  29. var nodesNew = this.data.new.search(bbox).map(projectNodes)
  30. var nodesLost = this.data.lost.search(bbox).map(projectNodes)
  31. var ctx = canvas.getContext("2d")
  32. ctx.font = "12px Roboto"
  33. ctx.textBaseline = "middle"
  34. ctx.textAlign = "left"
  35. ctx.lineWidth = 2.5
  36. var distance = 10
  37. function drawLabel(d) {
  38. ctx.strokeText(d.o.nodeinfo.hostname, d.p.x + distance, d.p.y)
  39. ctx.fillText(d.o.nodeinfo.hostname, d.p.x + distance, d.p.y)
  40. }
  41. ctx.fillStyle = "rgba(212, 62, 42, 0.6)"
  42. ctx.strokeStyle = "rgba(255, 255, 255, 0.6)"
  43. nodesOffline.forEach(drawLabel)
  44. ctx.fillStyle = "rgba(0, 0, 0, 0.6)"
  45. ctx.strokeStyle = "rgba(255, 255, 255, 0.9)"
  46. nodesOnline.forEach(drawLabel)
  47. ctx.fillStyle = "rgba(212, 62, 42, 0.6)"
  48. ctx.strokeStyle = "rgba(255, 255, 255, 0.9)"
  49. nodesLost.forEach(drawLabel)
  50. ctx.fillStyle = "rgba(0, 0, 0, 0.6)"
  51. ctx.strokeStyle = "rgba(255, 255, 255, 1.0)"
  52. nodesNew.forEach(drawLabel)
  53. }
  54. })
  55. })