Advertisement
Guest User

prist

a guest
Apr 15th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. (function(images) {
  2.  
  3. function shuffle(array) {
  4. var currentIndex = array.length, temporaryValue, randomIndex;
  5.  
  6. while (0 !== currentIndex) {
  7.  
  8. randomIndex = Math.floor(Math.random() * currentIndex);
  9. currentIndex -= 1;
  10.  
  11. temporaryValue = array[currentIndex];
  12. array[currentIndex] = array[randomIndex];
  13. array[randomIndex] = temporaryValue;
  14. }
  15.  
  16. return array;
  17. }
  18.  
  19. images = shuffle(images);
  20.  
  21. if (Notification.permission !== "granted")
  22. Notification.requestPermission();
  23.  
  24. var om = App.socket.onmessage;
  25.  
  26. App.socket.onmessage = function(message) {
  27. var m = JSON.parse(message.data);
  28.  
  29. if(m.type == "captcha_required") {
  30. if (Notification.permission !== "granted")
  31. Notification.requestPermission();
  32. else {
  33. var notification = new Notification('Капчуй-капчуй', {
  34. body: "Введи капчу, уеба.",
  35. });
  36. }
  37. }
  38.  
  39. om(message);
  40. }
  41.  
  42. var Painter = function(config) {
  43. var board = document.getElementById("board").getContext('2d');
  44. var title = config.title || "unnamed";
  45.  
  46. var img = new Image();
  47. img.crossOrigin = "anonymous";
  48. img.src = config.image;
  49. var x = +config.x;
  50. var y = +config.y;
  51.  
  52. var m = +config.mode || 0;
  53. var w, h;
  54.  
  55. var canvas = document.createElement('canvas');
  56. var image;
  57. var image_pixels;
  58. var board_pixels;
  59.  
  60. var image_loaded_flag = false;
  61. var image_correct_flag = true;
  62.  
  63.  
  64. function isSamePixelColor(coords) {
  65. if(image_pixels[coords+3] <= 127)
  66. return true;
  67.  
  68. for(var i = 0; i < 3; i++) {
  69. if(board_pixels[coords+i] != image_pixels[coords+i])
  70. return false;
  71. }
  72. return true;
  73. }
  74.  
  75. function getColorId(coords) {
  76. var colors = [
  77. [255,255,255],
  78. [228,228,228],
  79. [136,136,136],
  80. [34,34,34],
  81. [255,167,209],
  82. [229,0,0],
  83. [229,149,0],
  84. [160,106,66],
  85. [229,217,0],
  86. [148,224,68],
  87. [2,190,1],
  88. [0,211,221],
  89. [0,131,199],
  90. [0,0,234],
  91. [207,110,228],
  92. [130,0,128]
  93. ];
  94.  
  95. var color_id = -1;
  96. var flag = false;
  97. for(var i = 0, len = colors.length; i < len; i++) {
  98. flag = true;
  99. for(var j = 0; j < 3; j++) {
  100. if(image_pixels[coords+j] != colors[i][j]) {
  101. flag = false;
  102. break;
  103. }
  104. }
  105. if(flag) {
  106. color_id = i;
  107. break;
  108. }
  109. }
  110.  
  111. return color_id;
  112. }
  113.  
  114. function tryToDraw() {
  115. var random_pixels = [];
  116. board_pixels = board.getImageData(x, y, w, h).data;
  117.  
  118. if(+config.mode == 5) // смена режимов
  119. m = 5*Math.random()|0;
  120.  
  121. for(var i = 0, len = board_pixels.length; i < len; i += 4) {
  122.  
  123. var j = i;
  124. switch(m) {
  125. case 0: // cверху вниз
  126. //j = i;
  127. break;
  128. case 1: // cнизу вверх
  129. j = len-4-i;
  130. break;
  131. case 2: // слева направо
  132. j = (i/4%h*w+(i/4/h|0))*4;
  133. break;
  134. case 3: // справа налево
  135. j = len-4-i;
  136. j = (j/4%h*w+(j/4/h|0))*4;
  137. break;
  138. }
  139.  
  140. if(!isSamePixelColor(j)) {
  141. if (m == 4)
  142. random_pixels.push(j);
  143. else
  144. return drawPixel(j);
  145. }
  146. }
  147.  
  148. if (random_pixels.length > 0) {
  149. var i = random_pixels[Math.random()*random_pixels.length|0];
  150. return drawPixel(i);
  151. }
  152.  
  153. if (!image_correct_flag) {
  154. image_correct_flag = true;
  155. console.log(title + " охуенен");
  156. }
  157. return -1;
  158. }
  159.  
  160. function drawPixel(i) {
  161. var _x = i/4%w;
  162. var _y = i/4/w|0;
  163.  
  164. image_correct_flag = false;
  165.  
  166. var color_id = getColorId(i);
  167. if(color_id < 0) {
  168. console.log("пиксель x:" + _x + " y: " + _y + " хуевый");
  169. return -1;
  170. }
  171.  
  172. App.switchColor(color_id);
  173. App.attemptPlace(x + _x, y + _y);
  174.  
  175. console.log("рисую " + title + " пиксель " + " x:" + (x + _x) + " y:" + (y + _y));
  176. return 25;
  177. }
  178.  
  179. function drawImage() {
  180. if(image_loaded_flag) {
  181. return tryToDraw();
  182. }
  183. return -1;
  184. }
  185.  
  186. function isReady() {
  187. return image_loaded_flag;
  188. }
  189.  
  190. img.onload = function(){
  191. w = canvas.width = img.width;
  192. h = canvas.height = img.height;
  193. image = canvas.getContext('2d');
  194. image.drawImage(img, 0, 0, img.width, img.height);
  195. image_pixels = image.getImageData(0, 0, canvas.width, canvas.height).data;
  196.  
  197. image_loaded_flag = true;
  198. };
  199.  
  200.  
  201.  
  202. return {
  203. drawImage: drawImage,
  204. isReady: isReady
  205. }
  206. }
  207.  
  208.  
  209. var painters = [];
  210. for(var i = 0; i < images.length; i++) {
  211. painters[i] = Painter(images[i]);
  212. }
  213.  
  214. function draw() {
  215. var timer = (App.cooldown-(new Date).getTime())/1000;
  216. if(0 < timer) {
  217. //console.log("timer: " + timer);
  218. setTimeout(draw, 1000);
  219. }
  220. else {
  221. for(var i = 0; i < painters.length; i++) {
  222. if(painters[i].isReady()) {
  223. var result = painters[i].drawImage();
  224.  
  225. if(result > 0) {
  226. setTimeout(draw, result*1000);
  227. return;
  228. }
  229. else
  230. continue;
  231. }
  232. else
  233. continue;
  234. }
  235. setTimeout(draw, 15000);
  236. }
  237.  
  238. return;
  239. }
  240.  
  241. draw();
  242. })([
  243. {
  244. title: "prist",
  245. x: 792,
  246. y: 681,
  247. image: "http://i.imgur.com/Uy9aV2A.png",
  248. mode: 0, // 0 - построчно сверху, 1 - снизу, 2 - слева, 3 - справа, 4 - рандом,
  249. // 5 - рандомная смена режимов на лету
  250. }
  251. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement