Advertisement
12Me21

damn

Aug 14th, 2016
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*=============*\
  2. |░ image thing ░|
  3. \*=============*/
  4. //html stuff
  5. //paste box
  6. var pasteImageHere=document.createElement("div");
  7. pasteImageHere.style.width="3.25rem";
  8. pasteImageHere.id="pasteImageHere";
  9. pasteImageHere.contentEditable=true;
  10. pasteImageHere.oninput=function(){
  11.     var thing=this.getElementsByTagName("img")[0];
  12.     if (thing) loadImage(thing.src);
  13.     while (this.firstChild) this.removeChild(this.lastChild); //delete everything
  14. };
  15. document.querySelector("#sendpane").appendChild(pasteImageHere);
  16.  
  17. var sidePane=document.querySelector("#sidepane")
  18. //file upload button
  19. var fileBrowse=document.createElement("input");
  20. fileBrowse.type="file";
  21. fileBrowse.id="imageUpload";
  22. fileBrowse.onchange=function(){
  23.     console.log("User uploaded an image...");
  24.     var reader=new FileReader();
  25.     reader.readAsDataURL(this.files[0]);
  26.     reader.onload=function(){loadImage(this.result)};
  27. };
  28. sidePane.appendChild(fileBrowse);
  29. //dither slider
  30. var ditherSlider=document.createElement("input");
  31. ditherSlider.id="ditherSlider";
  32. ditherSlider.type="range";
  33. ditherSlider.min="0";
  34. ditherSlider.max="100";
  35. ditherSlider.onchange=function(){
  36.     dither=this.value/100;
  37.     updateImage();
  38. };
  39. sidePane.appendChild(ditherSlider);
  40. //blurring checkbox
  41. var blurring=document.createElement("input");
  42. blurring.id="blurring";
  43. blurring.type="checkbox";
  44. blurring.checked=true;
  45. blurring.onchange=function(){updateImage()};
  46. sidePane.appendChild(blurring);
  47. //fuck
  48. var elemX = 0;
  49. var elemY = 0;
  50. var offsetX, offsetY;
  51. var moving = 0;
  52. var scale = 1;
  53.  
  54. var moveBoxParent=document.createElement("div");
  55. moveBoxParent.style.width="200px";
  56. moveBoxParent.style.height="100px";
  57. moveBoxParent.style.backgroundSize="contain";
  58. moveBoxParent.style.backgroundRepeat="no-repeat";
  59. moveBoxParent.style.backgroundColor="white";
  60. moveBoxParent.onmousemove=function(e){
  61.     if (moving){
  62.         elemX=Math.min(Math.max(e.pageX-offsetX,0),(1-scale)*200);
  63.         elemY=Math.min(Math.max(e.pageY-offsetY,0),(1-scale)*100);
  64.         moveBox.style.left=elemX+"px";
  65.         moveBox.style.top=elemY+"px";
  66.         updateImageFast();
  67.     }
  68. };
  69. moveBoxParent.onmouseup = function(){
  70.     moving = 0;
  71.     updateImage();
  72. };
  73. sidepane.appendChild(moveBoxParent);
  74.  
  75. var moveBox=document.createElement("div");
  76. moveBox.style.backgroundColor="rgba(128,128,128,0.5)";
  77. moveBox.style.position="relative";
  78. moveBox.style.border="1px solid red";
  79. moveBox.style.width="200px";
  80. moveBox.style.height="100px";
  81. //moveBox.style.margin=-scale*100+"px "+-scale*50+"px";
  82. moveBox.onmousedown=function(e){
  83.    moving=1;
  84.    offsetX=e.pageX-elemX;
  85.    offsetY=e.pageY-elemY;
  86. };
  87. moveBoxParent.appendChild(moveBox);
  88.  
  89. var scaleSlider=document.createElement("input");
  90. scaleSlider.type="range";
  91. scaleSlider.min=0;
  92. scaleSlider.max=1;
  93. scaleSlider.step=0.001;
  94. scaleSlider.value=1;
  95. scaleSlider.oninput=function() {
  96.    scale=this.value;
  97.    moveBox.style.width=scale*200+"px";
  98.    moveBox.style.height=scale*100+"px";
  99.    updateImageFast();
  100. };
  101. scaleSlider.onchange=function(){updateImage()};
  102. sidepane.appendChild(scaleSlider);
  103. //variables
  104. var z=new Image(); //best variable name (c) Trinitro 2016
  105. var dither=0.75,noise=0;
  106. var didStuff=false;
  107. var isImage=false;
  108. //more functions
  109. function loadImage(source){
  110.     console.log("Found an image. (assume load failed)");
  111.     z=new Image();
  112.     z.crossOrigin="Anonymous";
  113.     z.onload=function(){
  114.         console.log("Image loaded successfully!");
  115.         moveBoxParent.style.backgroundImage="url('"+source+"')";
  116.         isImage=true;
  117.         updateImage();
  118.     };
  119.     z.src=source;
  120. }
  121. function updateImage(){if (isImage) putImage(z)}
  122. function updateImageFast(){var oldDither=dither;dither=0;updateImage();dither=oldDither;}
  123. //put image on canvas, and convert to 4 colors.
  124. function putImage(img){
  125.     if (!didStuff) { //do these the first time only (put them here so they don't happen before the page loads)
  126.         colorButtons=document.querySelectorAll("#chatdraw button-area button.colorChange");
  127.         paletteSize=colorButtons.length;
  128.         document.getElementById("colorPicker").addEventListener("change",()=>{updateImage()});
  129.         document.querySelector("#chatdraw button-area button").addEventListener("click",()=>{
  130.             isImage=false;
  131.             moveBoxParent.style.backgroundImage="";
  132.         });//✖ button
  133.         canvas=document.querySelector("#chatdraw canvas");
  134.         c2d=canvas.getContext("2d");
  135.         cW=canvas.width,cH=canvas.height;
  136.         didStuff=true;
  137.     }
  138.     const R=0,G=1,B=2;
  139.     var i,palette=[];
  140.     for(i=0;i<paletteSize;i++) palette.push(fillStyleToRgb(colorButtons[i].style.color));
  141.     c2d.rect(0,0,cW,cH);c2d.fillStyle="white";c2d.fill(); //clear canvas
  142.     c2d.webkitImageSmoothingEnabled=c2d.mozImageSmoothingEnabled=c2d.imageSmoothingEnabled=blurring.checked;
  143.     var minheight=Math.max(img.width/200,img.height/100)*100; //not even min lol
  144.     c2d.drawImage(img,elemX/200*minheight*2,elemY/100*minheight,scale*minheight*2,scale*minheight,0,0,200,100);
  145.     var data=c2d.getImageData(0,0,cW,cH);
  146.     for(var x=0,bestIndex;x<cW*cH*4;x+=4){
  147.         if (noise) for (i=0;i<3;i++) data.data[x+i]+=Math.random()*noise-noise/2;
  148.         //find the best color
  149.         var bestDist=Infinity;
  150.         for (i=0;i<paletteSize&&bestDist!==0;i++) {
  151.             for (var j=0,dist=0,color=palette[i];j<3;j++) dist+=Math.abs(data.data[x+j]-color[j]);
  152.             if (dist<bestDist){bestDist=dist;bestIndex=i}
  153.         }
  154.         //dithering
  155.         if (dither){
  156.             for (i=0;i<3;i++) {
  157.                 var err=(data.data[x+i]-palette[bestIndex][i])*dither/3;
  158.                 data.data[x+i]=palette[bestIndex][i];
  159.                 data.data[x     +4+i]+=err;   // right
  160.                 data.data[x+cW*4-4+i]+=err/2; // bottom left
  161.                 data.data[x+cW*4  +i]+=err;   // bottom
  162.                 data.data[x+cW*4+4+i]+=err/2; // bottom right
  163.             }
  164.         } else for (i=0;i<3;i++) data.data[x+i]=palette[bestIndex][i];
  165.     }
  166.     c2d.putImageData(data,0,0);
  167.     console.log("Image conversion sucessful.");
  168. }
  169. //adjust noise amount
  170. commands.push(new Command("noise", function(param){
  171.     if (param.length>1) {
  172.         noise=parseFloat(param,10);
  173.         systemMessage("Noise amount set to "+noise+".");
  174.         updateImage();
  175.     } else systemMessage("Noise amount is "+noise+".");
  176. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement