GhostOverlays.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const { UserColors } = require("./UserColors.js");
  2. class GhostOverlays {
  3. constructor(graph, userColors) {
  4. // this.userNames = userNames;
  5. this.userColors = userColors;
  6. this.canvas = graph.view.canvas;
  7. this.svg = this.canvas.parentElement;
  8. this.map = new Map();
  9. }
  10. // Update or create ghost cursor. Idempotent.
  11. put(userId, name, x, y) {
  12. let state = this.map.get(userId);
  13. if (state === undefined) {
  14. const g = document.createElementNS("http://www.w3.org/2000/svg", 'g');
  15. const image = document.createElementNS("http://www.w3.org/2000/svg", 'image');
  16. const text = document.createElementNS("http://www.w3.org/2000/svg", 'text');
  17. text.style.fontSize = "10px";
  18. text.style.fill = this.userColors.getColor(userId);
  19. text.setAttribute('y', 30);
  20. const textNode = document.createTextNode("");
  21. text.appendChild(textNode);
  22. image.setAttribute('href', "/lib/versioning/resources/cursor.svg");
  23. image.setAttribute('width', 11.6);
  24. image.setAttribute('height', 18.2);
  25. g.appendChild(image);
  26. g.appendChild(text);
  27. this.canvas.appendChild(g);
  28. const transform = this.svg.createSVGTransform();
  29. g.transform.baseVal.appendItem(transform);
  30. state = {transform, textNode, timeout: null};
  31. this.map.set(userId, state);
  32. }
  33. state.transform.setTranslate(
  34. (x + graph.view.translate.x) * graph.view.scale,
  35. (y + graph.view.translate.y) * graph.view.scale,
  36. );
  37. state.textNode.data = name;
  38. if (state.timeout) {
  39. clearTimeout(state.timeout);
  40. }
  41. state.timeout = setTimeout(() => state.transform.setTranslate(-50, -50), 5000);
  42. }
  43. }
  44. module.exports = { GhostOverlays };