Advertisement
falling1

egedraw image paste

Dec 7th, 2022 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function rgbatohex(r,g,b,a=255) {
  2.   if (isNaN(r) || isNaN(g) || isNaN(b) || isNaN(a)) return;
  3.   r = parseInt(r);
  4.   g = parseInt(g);
  5.   b = parseInt(b);
  6.   a = parseInt(a);
  7.   r = ("00000" + r.toString(16)).slice(-2);
  8.   g = ("00000" + g.toString(16)).slice(-2);
  9.   b = ("00000" + b.toString(16)).slice(-2);
  10.   a = ("00000" + a.toString(16)).slice(-2);
  11.   if (a == 'ff') return "#"+r+g+b; else return "#"+r+g+b+a;
  12. };
  13.  
  14. var upload = document.createElement("input");
  15. var mousedown = canvas.onmousedown;
  16. var mousemove = canvas.onmousemove;
  17. var paste = document.createElement("button");
  18. paste.innerHTML = "paste";
  19. paste.onclick = function () {
  20.   rct = canvas.getBoundingClientRect();
  21.   if (imagea == null) return;
  22.   this.innerHTML = "now click where you want to paste it!";
  23.   canvas.onmousedown = function (e) {
  24.     createClientMessage("Pasting..");
  25.     paste.innerHTML = "paste";
  26.     this.onmousedown = mousedown;
  27.     this.onmousemove = mousemove;
  28.     const img = new Image();
  29.     img.crossOrigin = "anonymous";
  30.     img.src = imagea;
  31.     img.onload = function () {
  32.       const c = new OffscreenCanvas(0,0);
  33.       const ctx = c.getContext('2d');
  34.       c.width = img.width;
  35.       c.height = img.height;
  36.       ctx.drawImage(img,0,0);
  37.       var imgdata = ctx.getImageData(0,0,c.width,c.height).data;
  38.       stopdraw();
  39.       for (let i = 0; i < c.height; i++) {
  40.         for (let j = 0; j < c.width; j++) {
  41.           var k = (i * c.width + j) * 4;
  42.           var pixeldata = [imgdata[k], imgdata[k + 1], imgdata[k + 2], imgdata[k + 3]];
  43.           if (!pixeldata[3]) continue;
  44.           drawLine(e.clientX-rct.left+j+0.5,e.clientY-rct.top+i+0.5,e.clientX-rct.left+j+0.5,e.clientY-rct.top+i+0.5,rgbatohex(...pixeldata), 1);
  45.         };
  46.       };
  47.     };
  48.   };
  49. };
  50. document.body.appendChild(paste);
  51. let imagea = null;
  52. upload.setAttribute("id", "FileUpload");
  53. upload.setAttribute("type", "file");
  54. upload.setAttribute("accept", "image/png, image/jpeg");
  55. document.body.appendChild(upload);
  56. document.getElementById("FileUpload").onchange = function (e) {
  57.   var tgt = e.target,
  58.     files = tgt.files;
  59.   if (FileReader && files && files.length) {
  60.     var fr = new FileReader();
  61.     fr.onload = function () {
  62.       imagea = fr.result;
  63.     };
  64.     fr.readAsDataURL(files[0]);
  65.   };
  66. };
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement