smoothretro82

spawns a auto-updating user list. Update 1: (now with colored names)

Nov 23rd, 2025 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. let lastDraw = [];
  2. let lastHeight = 0;
  3. let lastWidth = 0;
  4.  
  5. let spawnX = cursor.x;
  6. let spawnY = cursor.y;
  7.  
  8. // Draw only changed characters + colors
  9. function drawDiffLines(lines, x0, y0) {
  10. for (let y = 0; y < lines.length; y++) {
  11. const newLine = lines[y];
  12. const oldLine = lastDraw[y] || [];
  13.  
  14. const maxLen = Math.max(newLine.length, oldLine.length);
  15.  
  16. for (let x = 0; x < maxLen; x++) {
  17. const n = newLine[x] || { char: " ", color: 0 };
  18. const o = oldLine[x] || { char: " ", color: 0 };
  19.  
  20. if (n.char !== o.char || n.color !== o.color) {
  21. writeCharAt(n.char, n.color, x0 + x, y0 + y);
  22. }
  23. }
  24. }
  25.  
  26. lastDraw = lines.map(r => [...r]);
  27. lastHeight = lines.length;
  28. lastWidth = Math.max(...lines.map(r => r.length));
  29. }
  30.  
  31. // Colored username
  32. function makeColoredText(text, color) {
  33. return [...text].map(ch => ({ char: ch, color }));
  34. }
  35.  
  36. // Normal text
  37. function makeText(text) {
  38. return [...text].map(ch => ({ char: ch, color: 0 }));
  39. }
  40.  
  41. function ut() {
  42. let rows = [];
  43.  
  44. // Build new content
  45. for (const [id, cur] of w.cursors) {
  46. if (!cur) continue;
  47.  
  48. const cname = cur.n;
  49. const ccolor = cur.c || 0;
  50. const rest = ` | ${cur.id} → (${cur.l[0]},${cur.l[1]})`;
  51.  
  52. rows.push([
  53. ...makeColoredText(cname, ccolor),
  54. ...makeText(rest)
  55. ]);
  56. }
  57.  
  58. rows.push(makeText(""));
  59. rows.push(makeText("Client: " + w.clientId));
  60. rows.push(makeText("Color: " + (localStorage.col || "?")));
  61.  
  62. // ---- CLEAR PREVIOUS AREA IF NEW ONE IS SMALLER ----
  63. const newHeight = rows.length;
  64. const newWidth = Math.max(...rows.map(r => r.length));
  65.  
  66. if (newHeight < lastHeight || newWidth < lastWidth) {
  67. const clearRows = [];
  68.  
  69. for (let y = 0; y < lastHeight; y++) {
  70. clearRows.push(
  71. [...Array(lastWidth)].map(() => ({ char: " ", color: 0 }))
  72. );
  73. }
  74.  
  75. drawDiffLines(clearRows, spawnX, spawnY);
  76. }
  77.  
  78. // ---- DRAW ACTUAL CONTENT ----
  79. drawDiffLines(rows, spawnX, spawnY);
  80.  
  81. requestAnimationFrame(ut);
  82. }
  83.  
  84. ut();
  85.  
Advertisement
Add Comment
Please, Sign In to add comment