Advertisement
12Me21

fixed

Aug 14th, 2016
370
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 = 0.5;
  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.oninput=function() {
  95.    scale=this.value;
  96.    moveBox.style.width=scale*200+"px";
  97.    moveBox.style.height=scale*100+"px";
  98.    updateImageFast();
  99. };
  100. scaleSlider.onchange=function(){updateImage()};
  101. sidepane.appendChild(scaleSlider);
  102. //variables
  103. var z=new Image(); //best variable name (c) Trinitro 2016
  104. var dither=0.75,noise=0;
  105. var didStuff=false;
  106. var isImage=false;
  107. //more functions
  108. function loadImage(source){
  109.     console.log("Found an image. (assume load failed)");
  110.     z=new Image();
  111.     z.crossOrigin="Anonymous";
  112.     z.onload=function(){
  113.         console.log("Image loaded successfully!");
  114.         moveBoxParent.style.backgroundImage="url('"+source+"')";
  115.         isImage=true;
  116.         updateImage();
  117.     };
  118.     z.src=source;
  119. }
  120. function updateImage(){if (isImage) putImage(z)}
  121. function updateImageFast(){var oldDither=dither;dither=0;updateImage();dither=oldDither;}
  122. //put image on canvas, and convert to 4 colors.
  123. function putImage(img){
  124.     if (!didStuff) { //do these the first time only (put them here so they don't happen before the page loads)
  125.         colorButtons=document.querySelectorAll("#chatdraw button-area button.colorChange");
  126.         paletteSize=colorButtons.length;
  127.         document.getElementById("colorPicker").addEventListener("change",()=>{updateImage()});
  128.         document.querySelector("#chatdraw button-area button").addEventListener("click",()=>{
  129.             isImage=false;
  130.             moveBoxParent.style.backgroundImage="";
  131.         });//✖ button
  132.         canvas=document.querySelector("#chatdraw canvas");
  133.         c2d=canvas.getContext("2d");
  134.         cW=canvas.width,cH=canvas.height;
  135.         didStuff=true;
  136.     }
  137.     const R=0,G=1,B=2;
  138.     var i,palette=[];
  139.     for(i=0;i<paletteSize;i++) palette.push(fillStyleToRgb(colorButtons[i].style.color));
  140.     c2d.rect(0,0,cW,cH);c2d.fillStyle="white";c2d.fill(); //clear canvas
  141.     c2d.webkitImageSmoothingEnabled=c2d.mozImageSmoothingEnabled=c2d.imageSmoothingEnabled=blurring.checked;
  142.     scale0=Math.min(canvas.width/img.width,canvas.height/img.height);
  143.     c2d.drawImage(img,elemX,elemY,scale*200/scale0,scale*100/scale0,0,0,200,100);
  144.     var data=c2d.getImageData(0,0,cW,cH);
  145.     for(var x=0,bestIndex;x<cW*cH*4;x+=4){
  146.         if (noise) for (i=0;i<3;i++) data.data[x+i]+=Math.random()*noise-noise/2;
  147.         //find the best color
  148.         var bestDist=Infinity;
  149.         for (i=0;i<paletteSize&&bestDist!==0;i++) {
  150.             for (var j=0,dist=0,color=palette[i];j<3;j++) dist+=Math.abs(data.data[x+j]-color[j]);
  151.             if (dist<bestDist){bestDist=dist;bestIndex=i}
  152.         }
  153.         //dithering
  154.         if (dither){
  155.             for (i=0;i<3;i++) {
  156.                 var err=(data.data[x+i]-palette[bestIndex][i])*dither/3;
  157.                 data.data[x+i]=palette[bestIndex][i];
  158.                 data.data[x     +4+i]+=err;   // right
  159.                 data.data[x+cW*4-4+i]+=err/2; // bottom left
  160.                 data.data[x+cW*4  +i]+=err;   // bottom
  161.                 data.data[x+cW*4+4+i]+=err/2; // bottom right
  162.             }
  163.         } else for (i=0;i<3;i++) data.data[x+i]=palette[bestIndex][i];
  164.     }
  165.     c2d.putImageData(data,0,0);
  166.     console.log("Image conversion sucessful.");
  167. }
  168. //adjust noise amount
  169. commands.push(new Command("noise", function(param){
  170.     if (param.length>1) {
  171.         noise=parseFloat(param,10);
  172.         systemMessage("Noise amount set to "+noise+".");
  173.         updateImage();
  174.     } else systemMessage("Noise amount is "+noise+".");
  175. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement