You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cursor.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * WHITEBOPHIR
  3. *********************************************************
  4. * @licstart The following is the entire license notice for the
  5. * JavaScript code in this page.
  6. *
  7. * Copyright (C) 2020 Ophir LOJKINE
  8. *
  9. *
  10. * The JavaScript code in this page is free software: you can
  11. * redistribute it and/or modify it under the terms of the GNU
  12. * General Public License (GNU GPL) as published by the Free Software
  13. * Foundation, either version 3 of the License, or (at your option)
  14. * any later version. The code is distributed WITHOUT ANY WARRANTY;
  15. * without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  17. *
  18. * As additional permission under GNU GPL version 3 section 7, you
  19. * may distribute non-source (e.g., minimized or compacted) forms of
  20. * that code without the copy of the GNU GPL normally required by
  21. * section 4, provided you include this license notice and a URL
  22. * through which recipients can access the Corresponding Source.
  23. *
  24. * @licend
  25. */
  26. (function () { // Code isolation
  27. // Allocate half of the maximum server updates to cursor updates
  28. var MIN_CURSOR_UPDATES_INTERVAL_MS = Tools.server_config.MAX_EMIT_COUNT_PERIOD / Tools.server_config.MAX_EMIT_COUNT * 2;
  29. var CURSOR_DELETE_AFTER_MS = 1000 * 5;
  30. var lastCursorUpdate = 0;
  31. var sending = true;
  32. var cursorTool = {
  33. "name": "Cursor",
  34. "listeners": {
  35. "press": function () { sending = false },
  36. "move": handleMarker,
  37. "release": function () { sending = true },
  38. },
  39. "onSizeChange": onSizeChange,
  40. "draw": draw,
  41. "mouseCursor": "crosshair",
  42. "icon": "tools/pencil/icon.svg",
  43. };
  44. Tools.register(cursorTool);
  45. Tools.addToolListeners(cursorTool);
  46. var message = {
  47. type: "update",
  48. x: 0,
  49. y: 0,
  50. color: Tools.getColor(),
  51. size: Tools.getSize(),
  52. };
  53. function handleMarker(x, y) {
  54. // throttle local cursor updates
  55. message.x = x;
  56. message.y = y;
  57. message.color = Tools.getColor();
  58. message.size = Tools.getSize();
  59. updateMarker();
  60. }
  61. function onSizeChange(size) {
  62. message.size = size;
  63. updateMarker();
  64. }
  65. function updateMarker() {
  66. if (!Tools.showMarker || !Tools.showMyCursor) return;
  67. var cur_time = Date.now();
  68. if (cur_time - lastCursorUpdate > MIN_CURSOR_UPDATES_INTERVAL_MS &&
  69. (sending || Tools.curTool.showMarker)) {
  70. Tools.drawAndSend(message, cursorTool);
  71. lastCursorUpdate = cur_time;
  72. } else {
  73. draw(message);
  74. }
  75. }
  76. var cursorsElem = Tools.svg.getElementById("cursors");
  77. function createCursor(id) {
  78. var cursor = document.createElementNS("http://www.w3.org/2000/svg", "circle");
  79. cursor.setAttributeNS(null, "class", "opcursor");
  80. cursor.setAttributeNS(null, "id", id);
  81. cursor.setAttributeNS(null, "cx", 0);
  82. cursor.setAttributeNS(null, "cy", 0);
  83. cursor.setAttributeNS(null, "r", 10);
  84. cursorsElem.appendChild(cursor);
  85. setTimeout(function () {
  86. cursorsElem.removeChild(cursor);
  87. }, CURSOR_DELETE_AFTER_MS);
  88. return cursor;
  89. }
  90. function getCursor(id) {
  91. return document.getElementById(id) || createCursor(id);
  92. }
  93. function draw(message) {
  94. var cursor = getCursor("cursor-" + (message.socket || 'me'));
  95. cursor.style.transform = "translate(" + message.x + "px, " + message.y + "px)";
  96. if (Tools.isIE) cursor.setAttributeNS(null, "transform", "translate(" + message.x + " " + message.y + ")");
  97. cursor.setAttributeNS(null, "fill", message.color);
  98. cursor.setAttributeNS(null, "r", message.size / 2);
  99. }
  100. })();