Advertisement
Guest User

Skribbl

a guest
Jan 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Skribblerator
  3. // @version 3.14
  4. // @description Add sane keyboard controls to the second-worst Pictionary clone
  5. // @author jancc
  6. // @match https://*.skribbl.io/*
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. let $ = window.jQuery;
  15.  
  16. class ColorWheel {
  17. constructor (wheel) {
  18. this.wheel = wheel;
  19. this.index = -1;
  20. }
  21.  
  22. get current () {
  23. this.index = (this.index + 1) % this.wheel.length;
  24. return this.wheel[this.index];
  25. }
  26. }
  27.  
  28. /* the following defines the color sequence that the `W`heel hotkey produces, as manually sorted by the author of the script.
  29. * the spatial arrangement of the colors in-game is as follows:
  30. * 0 2 4 6 8 10 12 14 16 18 20
  31. * 1 3 5 7 9 11 13 15 17 19 21
  32. * so 0 corresponds to the white color, 1 to the black color, and so on.
  33. */
  34. let rainbow= new ColorWheel([8, 9, 7, 5, 4, 6, 20, 21, 19, 18, 16, 17, 15, 14, 12, 13, 11, 10]);
  35.  
  36. const toolMap = {
  37. 'pen': 'div[data-tool="pen"]',
  38. 'fill': 'div[data-tool="fill"]',
  39. 'erase': 'div[data-tool="erase"]',
  40.  
  41. 'white': 'div[data-color="0"]',
  42. 'black': 'div[data-color="1"]',
  43.  
  44. 'size1': 'div[data-size="0"]',
  45. 'size2': 'div[data-size="0.15"]',
  46. 'size3': 'div[data-size="0.45"]',
  47. 'size4': 'div[data-size="1"]',
  48.  
  49. 'trash': '#buttonClearCanvas',
  50. };
  51.  
  52. let tools = {};
  53. for (let tool in toolMap) {
  54. tools[tool] = $(toolMap[tool]);
  55. }
  56.  
  57. /* the following overrides the default brush sizes; the formula that Skribbl uses to calculate
  58. * the pixel width of a brush from its data-size value is:
  59. * width = data-size * (40 - 4) + 4
  60. * where the minimum value is 4 pixels, and the maximum value is 40 pixels; so set appropriate
  61. * values between 0 and 1, inclusive; the defaults can be seen fifteen lines above
  62. */
  63. tools.size1.attr('data-size', '0');
  64. tools.size2.attr('data-size', '0.15');
  65. tools.size3.attr('data-size', '0.3');
  66. tools.size4.attr('data-size', '0.5');
  67.  
  68. let colorPad, colorItems = [];
  69. for (let idx in [...Array(22).keys()]) {
  70. colorItems[idx] = {
  71. item: (colorPad = $('div[data-color="' + idx + '"]')),
  72. color: colorPad.css('background-color'),
  73. };
  74. }
  75.  
  76. let paletteVisible = false;
  77. $(document).keypress(function (event) {
  78. if (paletteVisible) {
  79. /* the following is self-explanatory, but in particular I want to point out that it makes `C` clear the canvas,
  80. * you can remove it or assign it to something else if you think you might accidentally press this hotkey
  81. */
  82. switch (event.key.toUpperCase()) {
  83. case '1':
  84. tools.pen.click();
  85. tools.size1.click();
  86. break;
  87. case '2':
  88. tools.pen.click();
  89. tools.size2.click();
  90. break;
  91. case '3':
  92. tools.pen.click();
  93. tools.size3.click();
  94. break;
  95. case '4':
  96. tools.pen.click();
  97. tools.size4.click();
  98. break;
  99. case 'D':
  100. tools.black.click();
  101. break;
  102. case 'G':
  103. tools.white.click();
  104. break;
  105. case 'C':
  106. tools.trash.click();
  107. break;
  108. case 'X':
  109. clickComplementColor();
  110. break;
  111. case 'R':
  112. colorItems[Math.floor(Math.random() * 4)].item.click();
  113. break;
  114. case 'W':
  115. colorItems[rainbow.current].item.click();
  116. break;
  117. }
  118. }
  119. });
  120.  
  121. let colorPreview = $('.colorPreview');
  122. function clickComplementColor () {
  123. let idx, currentColor = colorPreview.css('background-color');
  124. for (let key in colorItems) {
  125. if (currentColor === colorItems[key].color) {
  126. idx = parseInt(key);
  127. colorItems[idx % 2 ? idx - 1 : idx + 1].item.click();
  128. }
  129. }
  130. }
  131.  
  132. let containerToolbar = document.querySelector('.containerToolbar');
  133. new MutationObserver(function (mutationList, observer) {
  134. paletteVisible = !(containerToolbar.style.display === 'none');
  135. }).observe(containerToolbar, {
  136. attributes: true,
  137. });
  138.  
  139. let buttonsWrapper = $('<div>')
  140. .attr('id', 'buttonsWrapper')
  141. .css({
  142. 'display': 'flex',
  143. 'flex-direction': 'column',
  144. }).appendTo('#containerPlayerlist');
  145.  
  146. $('.tooltip-wrapper').appendTo(buttonsWrapper);
  147.  
  148. let saveCanvasButton = $('<button>Save canvas</button>')
  149. .attr({
  150. class: 'btn btn-warning btn-block',
  151. id: 'saveCanvasButton',
  152. }).css({
  153. 'margin-top': '5px',
  154. }).appendTo(buttonsWrapper);
  155.  
  156. let gameCanvas = document.querySelector('#canvasGame'),
  157. dummyLink = document.createElement('a');
  158. saveCanvasButton.on('click', function () {
  159. dummyLink.href = gameCanvas.toDataURL();
  160. dummyLink.download = Date.now() + '.png';
  161. dummyLink.click();
  162. });
  163. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement