smoothretro82

Live chat box on canvas (Update 2)

Nov 18th, 2025 (edited)
83
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. // === Single-Line Chat Display (with full clear + username fix + double-line border) ===
  2.  
  3. // --- Constants ---
  4. const CHAT_START_X = cursor.x;
  5. const CHAT_START_Y = cursor.y;
  6. const CHAT_LINE_LENGTH = 180;
  7.  
  8. const BORDER_TOP_Y = CHAT_START_Y - 1;
  9. const BORDER_MID_Y = CHAT_START_Y;
  10. const BORDER_BOTTOM_Y = CHAT_START_Y + 1;
  11.  
  12. const INNER_WIDTH = CHAT_LINE_LENGTH;
  13. const BLANK_LINE = " ".repeat(INNER_WIDTH);
  14.  
  15. let lastDrawn = "";
  16. let scheduledFrame = false;
  17. let nextText = "";
  18.  
  19. // Cursor helper
  20. function setCursor(x, y) {
  21. cursor.x = x;
  22. cursor.y = y;
  23. }
  24.  
  25. // Write string via writeChar()
  26. function writeString(str) {
  27. for (let i = 0; i < str.length; i++) {
  28. writeChar(str[i], 1); // keep attribute
  29. }
  30. }
  31.  
  32. // === Draw static DOUBLE-LINE border ===
  33. function drawBorder() {
  34. // Top: ╔══════════╗
  35. setCursor(CHAT_START_X, BORDER_TOP_Y);
  36. writeString("╔" + "═".repeat(INNER_WIDTH) + "╗");
  37.  
  38. // Middle empty line: ║ ║
  39. setCursor(CHAT_START_X, BORDER_MID_Y);
  40. writeString("║" + " ".repeat(INNER_WIDTH) + "║");
  41.  
  42. // Bottom: ╚══════════╝
  43. setCursor(CHAT_START_X, BORDER_BOTTOM_Y);
  44. writeString("╚" + "═".repeat(INNER_WIDTH) + "╝");
  45. }
  46.  
  47. // Clear inside the double-line border
  48. function clearInner() {
  49. setCursor(CHAT_START_X + 1, BORDER_MID_Y);
  50. writeString(BLANK_LINE);
  51. }
  52.  
  53. // Draw updated chat text
  54. function drawSingleLine() {
  55. const newText = nextText.slice(0, INNER_WIDTH);
  56.  
  57. clearInner();
  58.  
  59. setCursor(CHAT_START_X + 1, BORDER_MID_Y);
  60. writeString(newText);
  61.  
  62. lastDrawn = newText;
  63. scheduledFrame = false;
  64. }
  65.  
  66. // Schedule a single-frame redraw
  67. function scheduleDraw(text) {
  68. nextText = text;
  69. if (!scheduledFrame) {
  70. scheduledFrame = true;
  71. requestAnimationFrame(drawSingleLine);
  72. }
  73. }
  74.  
  75. // === Chat listener ===
  76. w.on("msg", (data) => {
  77. const nick =
  78. data?.nick?.trim() ||
  79. data?.user?.name?.trim() ||
  80. data?.username?.trim() ||
  81. "Unknown";
  82.  
  83. const line = `${nick}: ${data.msg}`;
  84.  
  85. scheduleDraw(line);
  86.  
  87. // Original save logic
  88. if (data.msg.startsWith("save ") && nick === "CalculatorBot") {
  89. save = Number(data.msg.substring(5)) - b;
  90. }
  91. });
  92.  
  93. // Draw border once at startup
  94. drawBorder();
  95.  
  96. // Startup message
  97.  
  98.  
Advertisement
Comments
  • smoothretro82
    255 days (edited)
    # text 0.23 KB | 0 0
    1. If you don't want to have the chatbox always spawn at your cursor's position,
    2. replace cursor.x and cursor.y with regular coords in the following line:
    3.  
    4. // --- Constants ---
    5. const CHAT_START_X = cursor.x;
    6. const CHAT_START_Y = cursor.y;
Add Comment
Please, Sign In to add comment