smoothretro1982

text spawner 0.2

Mar 13th, 2026 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. // --- SAFETY CHECK AND DUMMY CURSOR ---
  2. if (typeof w === "undefined") w = { cursors: new Map() }
  3. if (!w.cursors) w.cursors = new Map()
  4. if (w.cursors.size === 0) {
  5. w.cursors.set("test123", { n: "TestCursor", rawx: 50, rawy: 10, c: 1 })
  6. }
  7.  
  8. // --- CREATE HUD ---
  9. if (!document.getElementById('cursorTextHUD')) {
  10. const hud = document.createElement('div')
  11. hud.id = 'cursorTextHUD'
  12. hud.style.cssText = `
  13. position: fixed;
  14. top: 20px;
  15. left: 20px;
  16. width: 340px;
  17. padding: 10px;
  18. background: rgba(0,0,0,0.85);
  19. color: white;
  20. font-family: monospace;
  21. border-radius: 8px;
  22. z-index: 9999;
  23. cursor: move;
  24. `
  25.  
  26. hud.innerHTML = `
  27. <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:5px;">
  28. <strong>Cursor Text HUD</strong>
  29. <button id="hudCloseBtn" style="background:red;color:white;border:none;border-radius:3px;cursor:pointer;">X</button>
  30. </div>
  31.  
  32. <div>
  33. <label>Choose Cursor (or leave blank for all):</label><br/>
  34. <select id="hudCursorSelect" style="width:100%"></select>
  35. </div>
  36.  
  37. <div style="margin-top:5px;">
  38. <label>Text (max 180 chars):</label><br/>
  39. <input type="text" id="hudCursorText" maxlength="180" style="width:100%">
  40. </div>
  41.  
  42. <div style="margin-top:5px;">
  43. <label><input type="checkbox" id="hudFollowCursor"> Follow Cursor</label>
  44. </div>
  45.  
  46. <button id="hudSpawnText" style="margin-top:5px;width:100%">Spawn Text</button>
  47. `
  48. document.body.appendChild(hud)
  49. }
  50.  
  51. // --- ELEMENT REFERENCES ---
  52. const hud = document.getElementById("cursorTextHUD")
  53. const select = document.getElementById("hudCursorSelect")
  54. const input = document.getElementById("hudCursorText")
  55. const followToggle = document.getElementById("hudFollowCursor")
  56.  
  57. // --- POPULATE CURSOR DROPDOWN (sticky selection) ---
  58. function updateOptions() {
  59. const currentSelection = select.value
  60. select.innerHTML = '<option value="">All Cursors</option>'
  61. for (const [id, c] of w.cursors) {
  62. const option = document.createElement('option')
  63. option.value = id
  64. option.textContent = `${c.n} (${id})`
  65. select.appendChild(option)
  66. }
  67. if ([...w.cursors.keys()].includes(currentSelection) || currentSelection === "") {
  68. select.value = currentSelection
  69. } else {
  70. select.value = ""
  71. }
  72. }
  73. updateOptions()
  74. setInterval(updateOptions, 2000)
  75.  
  76. // --- TRACK FOLLOWING TEXT ---
  77. const followingTexts = [] // each: {text, cursorId, offsetX, offsetY, prevPositions: []}
  78.  
  79. // --- HELPER FUNCTION TO WRITE CENTERED AND TRACK POSITIONS ---
  80. function writeCenteredFollow(followObj) {
  81. const c = w.cursors.get(followObj.cursorId)
  82. if (!c) return
  83.  
  84. const chars = [...followObj.text]
  85. const startX = c.rawx - Math.floor(chars.length / 2) + (followObj.offsetX || 0)
  86. const y = c.rawy + (followObj.offsetY || 0)
  87.  
  88. // Erase previous text
  89. if (followObj.prevPositions) {
  90. for (const pos of followObj.prevPositions) {
  91. writeCharAt(' ', c.c ?? 0, pos.x, pos.y)
  92. }
  93. }
  94.  
  95. // Write new text and store positions
  96. followObj.prevPositions = []
  97. for (let i = 0; i < chars.length; i++) {
  98. writeCharAt(chars[i], c.c ?? 0, startX + i, y)
  99. followObj.prevPositions.push({x: startX + i, y})
  100. }
  101. }
  102.  
  103. // --- LIVE TYPING ---
  104. input.addEventListener("input", () => {
  105. const text = input.value.slice(0, 180)
  106. const cursorId = select.value.trim()
  107. if (!cursorId) return
  108. if (!text) return
  109.  
  110. // Check if a follow object already exists
  111. let followObj = followingTexts.find(f => f.cursorId === cursorId)
  112. if (!followObj) {
  113. followObj = { text, cursorId, offsetX: 0, offsetY: 0, prevPositions: [] }
  114. followingTexts.push(followObj)
  115. } else {
  116. followObj.text = text
  117. }
  118.  
  119. // Only track following if toggle is on
  120. followObj.follow = followToggle.checked
  121.  
  122. writeCenteredFollow(followObj)
  123. })
  124.  
  125. // --- SPAWN TEXT BUTTON ---
  126. document.getElementById("hudSpawnText").onclick = () => {
  127. const cursorId = select.value.trim()
  128. const text = input.value.slice(0, 180)
  129.  
  130. if (!text) return alert("Enter some text!")
  131.  
  132. let cursors = []
  133. if (!cursorId) {
  134. cursors = [...w.cursors.values()]
  135. } else {
  136. const cById = w.cursors.get(cursorId)
  137. if (cById) cursors.push(cById)
  138. }
  139.  
  140. if (cursors.length === 0) return alert("No valid cursors found!")
  141.  
  142. for (const c of cursors) {
  143. writeCenteredFollow({ text, cursorId: [...w.cursors].find(([k,v]) => v===c)[0], offsetX:0, offsetY:0, prevPositions:[] })
  144. }
  145. }
  146.  
  147. // --- CLOSE BUTTON ---
  148. document.getElementById("hudCloseBtn").onclick = () => {
  149. document.getElementById("cursorTextHUD").remove()
  150. }
  151.  
  152. // --- MAKE HUD DRAGGABLE ---
  153. let isDragging = false, offsetX = 0, offsetY = 0
  154. hud.addEventListener("mousedown", e => {
  155. if (e.target.closest("input, select, button")) return
  156. isDragging = true
  157. offsetX = e.clientX - hud.offsetLeft
  158. offsetY = e.clientY - hud.offsetTop
  159. e.preventDefault()
  160. })
  161. document.addEventListener("mousemove", e => {
  162. if (!isDragging) return
  163. hud.style.left = `${e.clientX - offsetX}px`
  164. hud.style.top = `${e.clientY - offsetY}px`
  165. })
  166. document.addEventListener("mouseup", () => isDragging = false)
  167.  
  168. // --- FOCUS INPUT ON OPEN ---
  169. input.focus()
  170.  
  171. // --- UPDATE FOLLOWING TEXT EACH FRAME ---
  172. setInterval(() => {
  173. for (const f of followingTexts) {
  174. if (f.follow) writeCenteredFollow(f)
  175. }
  176. }, 50)
Advertisement
Add Comment
Please, Sign In to add comment