Drawaria Transitions

Adds transition effects to the drawing canvas in Drawaria.Online with an improved menu

  1. // ==UserScript==
  2. // @name Drawaria Transitions
  3. // @namespace Transitions using drawing commands and a draggable menu
  4. // @version 2024.10.27
  5. // @description Adds transition effects to the drawing canvas in Drawaria.Online with an improved menu
  6. // @author YouTubeDrawaria
  7. // @match *://drawaria.online/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=drawaria.online
  9. // @license MIT
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Function to send the draw command (covering the entire screen)
  17. function sendDrawCommand(x1, y1, x2, y2, color, size = 100000) { // Extremely large default size
  18. let message = `42["drawcmd",0,[${x1},${y1},${x2},${y2},false,${size},"${color}",0,0,{}]]`;
  19. window.sockets.forEach(socket => {
  20. if (socket.readyState === WebSocket.OPEN) {
  21. socket.send(message);
  22. }
  23. });
  24. }
  25.  
  26. // Overriding the WebSocket send method to capture sockets
  27. const originalSend = WebSocket.prototype.send;
  28. WebSocket.prototype.send = function (...args) {
  29. if (window.sockets.indexOf(this) === -1) {
  30. window.sockets.push(this);
  31. }
  32. return originalSend.call(this, ...args);
  33. };
  34.  
  35. // Initializing
  36. window.sockets = [];
  37.  
  38. // Function to create a transition effect
  39. function createTransition(name, effectFunction) {
  40. return {
  41. name,
  42. effectFunction
  43. };
  44. }
  45.  
  46. // Transition effects
  47. const transitions = [
  48. createTransition('Slide Right', (color, duration = 1000) => {
  49. let start = null;
  50. const animate = timestamp => {
  51. if (!start) start = timestamp;
  52. const progress = timestamp - start;
  53. const canvas = document.querySelector('canvas');
  54. if (!canvas) return;
  55. const offset = Math.round((progress / duration) * canvas.width);
  56. sendDrawCommand(0, 0, canvas.width - offset, canvas.height, color);
  57. if (progress < duration) requestAnimationFrame(animate);
  58. };
  59. requestAnimationFrame(animate);
  60. }),
  61. createTransition('Slide Down', (color, duration = 1000) => {
  62. let start = null;
  63. const animate = timestamp => {
  64. if (!start) start = timestamp;
  65. const progress = timestamp - start;
  66. const canvas = document.querySelector('canvas');
  67. if (!canvas) return;
  68. const offset = Math.round((progress / duration) * canvas.height);
  69. sendDrawCommand(0, 0, canvas.width, canvas.height - offset, color);
  70. if (progress < duration) requestAnimationFrame(animate);
  71. };
  72. requestAnimationFrame(animate);
  73. }),
  74. createTransition('Slide Left', (color, duration = 1000) => {
  75. let start = null;
  76. const animate = timestamp => {
  77. if (!start) start = timestamp;
  78. const progress = timestamp - start;
  79. const canvas = document.querySelector('canvas');
  80. if (!canvas) return;
  81. const offset = Math.round((progress / duration) * canvas.width);
  82. sendDrawCommand(offset, 0, canvas.width, canvas.height, color);
  83. if (progress < duration) requestAnimationFrame(animate);
  84. };
  85. requestAnimationFrame(animate);
  86. }),
  87. createTransition('Slide Up', (color, duration = 1000) => {
  88. let start = null;
  89. const animate = timestamp => {
  90. if (!start) start = timestamp;
  91. const progress = timestamp - start;
  92. const canvas = document.querySelector('canvas');
  93. if (!canvas) return;
  94. const offset = Math.round((progress / duration) * canvas.height);
  95. sendDrawCommand(0, offset, canvas.width, canvas.height, color);
  96. if (progress < duration) requestAnimationFrame(animate);
  97. };
  98. requestAnimationFrame(animate);
  99. }),
  100. createTransition('Slide Diagonal Top-Left', (color, duration = 1000) => {
  101. let start = null;
  102. const animate = timestamp => {
  103. if (!start) start = timestamp;
  104. const progress = timestamp - start;
  105. const canvas = document.querySelector('canvas');
  106. if (!canvas) return;
  107. const offset = Math.round((progress / duration) * Math.min(canvas.width, canvas.height));
  108. sendDrawCommand(offset, 0, canvas.width, offset, color);
  109. if (progress < duration) requestAnimationFrame(animate);
  110. };
  111. requestAnimationFrame(animate);
  112. }),
  113. createTransition('Slide Diagonal Top-Right', (color, duration = 1000) => {
  114. let start = null;
  115. const animate = timestamp => {
  116. if (!start) start = timestamp;
  117. const progress = timestamp - start;
  118. const canvas = document.querySelector('canvas');
  119. if (!canvas) return;
  120. const offset = Math.round((progress / duration) * Math.min(canvas.width, canvas.height));
  121. sendDrawCommand(0, 0, canvas.width - offset, offset, color);
  122. if (progress < duration) requestAnimationFrame(animate);
  123. };
  124. requestAnimationFrame(animate);
  125. }),
  126. createTransition('Slide Diagonal Bottom-Left', (color, duration = 1000) => {
  127. let start = null;
  128. const animate = timestamp => {
  129. if (!start) start = timestamp;
  130. const progress = timestamp - start;
  131. const canvas = document.querySelector('canvas');
  132. if (!canvas) return;
  133. const offset = Math.round((progress / duration) * Math.min(canvas.width, canvas.height));
  134. sendDrawCommand(offset, canvas.height - offset, canvas.width, canvas.height, color);
  135. if (progress < duration) requestAnimationFrame(animate);
  136. };
  137. requestAnimationFrame(animate);
  138. }),
  139. createTransition('Slide Diagonal Bottom-Right', (color, duration = 1000) => {
  140. let start = null;
  141. const animate = timestamp => {
  142. if (!start) start = timestamp;
  143. const progress = timestamp - start;
  144. const canvas = document.querySelector('canvas');
  145. if (!canvas) return;
  146. const offset = Math.round((progress / duration) * Math.min(canvas.width, canvas.height));
  147. sendDrawCommand(0, canvas.height - offset, canvas.width - offset, canvas.height, color);
  148. if (progress < duration) requestAnimationFrame(animate);
  149. };
  150. requestAnimationFrame(animate);
  151. }),
  152. createTransition('Slide Vertical Split', (color, duration = 1000) => {
  153. let start = null;
  154. const animate = timestamp => {
  155. if (!start) start = timestamp;
  156. const progress = timestamp - start;
  157. const canvas = document.querySelector('canvas');
  158. if (!canvas) return;
  159. const offset = Math.round((progress / duration) * canvas.height / 2);
  160. sendDrawCommand(0, 0, canvas.width, canvas.height / 2 - offset, color);
  161. sendDrawCommand(0, canvas.height / 2 + offset, canvas.width, canvas.height, color);
  162. if (progress < duration) requestAnimationFrame(animate);
  163. };
  164. requestAnimationFrame(animate);
  165. }),
  166. createTransition('Slide Horizontal Split', (color, duration = 1000) => {
  167. let start = null;
  168. const animate = timestamp => {
  169. if (!start) start = timestamp;
  170. const progress = timestamp - start;
  171. const canvas = document.querySelector('canvas');
  172. if (!canvas) return;
  173. const offset = Math.round((progress / duration) * canvas.width / 2);
  174. sendDrawCommand(0, 0, canvas.width / 2 - offset, canvas.height, color);
  175. sendDrawCommand(canvas.width / 2 + offset, 0, canvas.width, canvas.height, color);
  176. if (progress < duration) requestAnimationFrame(animate);
  177. };
  178. requestAnimationFrame(animate);
  179. }),
  180. createTransition('Slide In from Center', (color, duration = 1000) => {
  181. let start = null;
  182. const animate = timestamp => {
  183. if (!start) start = timestamp;
  184. const progress = timestamp - start;
  185. const canvas = document.querySelector('canvas');
  186. if (!canvas) return;
  187. const offset = Math.round((progress / duration) * Math.min(canvas.width, canvas.height) / 2);
  188. sendDrawCommand(canvas.width / 2 - offset, 0, canvas.width / 2 + offset, canvas.height, color);
  189. if (progress < duration) requestAnimationFrame(animate);
  190. };
  191. requestAnimationFrame(animate);
  192. }),
  193. createTransition('Slide Out to Center', (color, duration = 1000) => {
  194. let start = null;
  195. const animate = timestamp => {
  196. if (!start) start = timestamp;
  197. const progress = timestamp - start;
  198. const canvas = document.querySelector('canvas');
  199. if (!canvas) return;
  200. const offset = Math.round((1 - progress / duration) * Math.min(canvas.width, canvas.height) / 2);
  201. sendDrawCommand(canvas.width / 2 - offset, 0, canvas.width / 2 + offset, canvas.height, color);
  202. if (progress < duration) requestAnimationFrame(animate);
  203. };
  204. requestAnimationFrame(animate);
  205. }),
  206. createTransition('Slide In from Corners', (color, duration = 1000) => {
  207. let start = null;
  208. const animate = timestamp => {
  209. if (!start) start = timestamp;
  210. const progress = timestamp - start;
  211. const canvas = document.querySelector('canvas');
  212. if (!canvas) return;
  213. const offset = Math.round((progress / duration) * Math.min(canvas.width, canvas.height) / 2);
  214. sendDrawCommand(0, 0, offset, offset, color);
  215. sendDrawCommand(canvas.width - offset, 0, canvas.width, offset, color);
  216. sendDrawCommand(0, canvas.height - offset, offset, canvas.height, color);
  217. sendDrawCommand(canvas.width - offset, canvas.height - offset, canvas.width, canvas.height, color);
  218. if (progress < duration) requestAnimationFrame(animate);
  219. };
  220. requestAnimationFrame(animate);
  221. }),
  222. createTransition('Slide Out to Corners', (color, duration = 1000) => {
  223. let start = null;
  224. const animate = timestamp => {
  225. if (!start) start = timestamp;
  226. const progress = timestamp - start;
  227. const canvas = document.querySelector('canvas');
  228. if (!canvas) return;
  229. const offset = Math.round((1 - progress / duration) * Math.min(canvas.width, canvas.height) / 2);
  230. sendDrawCommand(0, 0, offset, offset, color);
  231. sendDrawCommand(canvas.width - offset, 0, canvas.width, offset, color);
  232. sendDrawCommand(0, canvas.height - offset, offset, canvas.height, color);
  233. sendDrawCommand(canvas.width - offset, canvas.height - offset, canvas.width, canvas.height, color);
  234. if (progress < duration) requestAnimationFrame(animate);
  235. };
  236. requestAnimationFrame(animate);
  237. }),
  238. createTransition('Slide In from Top and Bottom', (color, duration = 1000) => {
  239. let start = null;
  240. const animate = timestamp => {
  241. if (!start) start = timestamp;
  242. const progress = timestamp - start;
  243. const canvas = document.querySelector('canvas');
  244. if (!canvas) return;
  245. const offset = Math.round((progress / duration) * canvas.height / 2);
  246. sendDrawCommand(0, 0, canvas.width, offset, color);
  247. sendDrawCommand(0, canvas.height - offset, canvas.width, canvas.height, color);
  248. if (progress < duration) requestAnimationFrame(animate);
  249. };
  250. requestAnimationFrame(animate);
  251. }),
  252. createTransition('Slide Out to Top and Bottom', (color, duration = 1000) => {
  253. let start = null;
  254. const animate = timestamp => {
  255. if (!start) start = timestamp;
  256. const progress = timestamp - start;
  257. const canvas = document.querySelector('canvas');
  258. if (!canvas) return;
  259. const offset = Math.round((1 - progress / duration) * canvas.height / 2);
  260. sendDrawCommand(0, 0, canvas.width, offset, color);
  261. sendDrawCommand(0, canvas.height - offset, canvas.width, canvas.height, color);
  262. if (progress < duration) requestAnimationFrame(animate);
  263. };
  264. requestAnimationFrame(animate);
  265. })
  266. ];
  267.  
  268. // Function to create the menu
  269. function createMenu(title, items) {
  270. const menu = document.createElement('div');
  271. menu.id = 'transitionMenu';
  272. menu.innerHTML = `
  273. <div id="menuHeader">
  274. <h3>${title}</h3>
  275. <div id="closeButton">X</div>
  276. </div>
  277. <div id="menuItems">
  278. <input type="color" id="colorInput" value="#000000">
  279. <div id="buttonGrid">
  280. ${items.map(item => `<button data-transition="${item.name}" data-duration="1000">${item.name}</button>`).join('')}
  281. </div>
  282. </div>
  283. `;
  284.  
  285. // Adding styles to the menu (inline for easy copy and paste)
  286. menu.style.cssText = `
  287. position: absolute;
  288. top: 10px;
  289. left: 10px;
  290. background-color: rgba(255, 255, 255, 0.8);
  291. padding: 10px;
  292. border: 1px solid #ccc;
  293. border-radius: 5px;
  294. box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
  295. z-index: 1000; /* Ensure it is above the canvas */
  296. width: 220px;
  297. max-height: 400px;
  298. overflow-y: auto;
  299. `;
  300.  
  301. // Grid layout for buttons
  302. const buttonGrid = menu.querySelector('#buttonGrid');
  303. buttonGrid.style.cssText = `
  304. display: grid;
  305. grid-template-columns: repeat(3, 1fr);
  306. gap: 10px;
  307. `;
  308.  
  309. // Menu events
  310. menu.querySelector('#closeButton').addEventListener('click', () => {
  311. menu.remove();
  312. });
  313.  
  314. menu.querySelectorAll('button').forEach(button => {
  315. button.addEventListener('click', () => {
  316. const transitionName = button.dataset.transition;
  317. const duration = parseInt(button.dataset.duration, 10);
  318. const color = document.getElementById('colorInput').value;
  319. const transition = transitions.find(t => t.name === transitionName);
  320. if (transition) {
  321. transition.effectFunction(color, duration);
  322. }
  323. });
  324. });
  325.  
  326. // Dragging the menu
  327. let isDragging = false;
  328. let offsetX, offsetY;
  329. menu.addEventListener('mousedown', (e) => {
  330. isDragging = true;
  331. offsetX = e.clientX - menu.offsetLeft;
  332. offsetY = e.clientY - menu.offsetTop;
  333. });
  334. document.addEventListener('mouseup', () => {
  335. isDragging = false;
  336. });
  337. document.addEventListener('mousemove', (e) => {
  338. if (isDragging) {
  339. menu.style.left = (e.clientX - offsetX) + 'px';
  340. menu.style.top = (e.clientY - offsetY) + 'px';
  341. }
  342. });
  343.  
  344. document.body.appendChild(menu);
  345. }
  346.  
  347. const menuItems = transitions.map(transition => ({ name: transition.name }));
  348. createMenu('Drawaria Transitions', menuItems);
  349. })();