4
0

labelslayer.js 2.2 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. if (!this.data)
  10. return
  11. var tileSize = this.options.tileSize
  12. var s = tilePoint.multiplyBy(tileSize)
  13. var map = this._map
  14. function project(coords) {
  15. var p = map.project(new L.LatLng(coords[0], coords[1]))
  16. return {x: p.x - s.x, y: p.y - s.y}
  17. }
  18. function projectNodes(d) {
  19. return { p: project([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude]),
  20. o: d
  21. }
  22. }
  23. var margin = 150
  24. function onTile(d) {
  25. return d.p.x + margin > 0 ||
  26. d.p.y + margin > 0 ||
  27. d.p.x - tileSize - margin < 0 ||
  28. d.p.y - tileSize - margin < 0
  29. }
  30. var ctx = canvas.getContext("2d")
  31. var nodesOnline = this.data.online.map(projectNodes).filter(onTile)
  32. var nodesOffline = this.data.offline.map(projectNodes).filter(onTile)
  33. var nodesNew = this.data.new.map(projectNodes).filter(onTile)
  34. var nodesLost = this.data.lost.map(projectNodes).filter(onTile)
  35. var distance = 10
  36. ctx.font = "12px Roboto"
  37. ctx.textBaseline = "middle"
  38. ctx.textAlign = "left"
  39. ctx.lineWidth = 2.5
  40. function drawLabel(d) {
  41. ctx.strokeText(d.o.nodeinfo.hostname, d.p.x + distance, d.p.y)
  42. ctx.fillText(d.o.nodeinfo.hostname, d.p.x + distance, d.p.y)
  43. }
  44. ctx.fillStyle = "rgba(212, 62, 42, 0.6)"
  45. ctx.strokeStyle = "rgba(255, 255, 255, 0.6)"
  46. nodesOffline.forEach(drawLabel)
  47. ctx.fillStyle = "rgba(0, 0, 0, 0.6)"
  48. ctx.strokeStyle = "rgba(255, 255, 255, 0.9)"
  49. nodesOnline.forEach(drawLabel)
  50. ctx.fillStyle = "rgba(212, 62, 42, 0.6)"
  51. ctx.strokeStyle = "rgba(255, 255, 255, 0.9)"
  52. nodesLost.forEach(drawLabel)
  53. ctx.fillStyle = "rgba(0, 0, 0, 0.6)"
  54. ctx.strokeStyle = "rgba(255, 255, 255, 1.0)"
  55. nodesNew.forEach(drawLabel)
  56. }
  57. })
  58. })