Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Sprite(url, success){
  2.     this.dataUrl = url || "";
  3.     this.img = document.createElement("IMG");
  4.     this.img.src = this.dataUrl;
  5.     this.img.onload = success;
  6. }
  7.  
  8. let pic = document.createElement('IMG');
  9. pic.onload = function() {
  10.     cvs.width = pic.width;
  11.     cvs.height = pic.height;
  12.     c.drawImage(pic, 0, 0);
  13.     removeColor(c, [125, 25, 125], pic.width, pic.height);
  14.     let ur = cvs.toDataURL();
  15.     a[file.name] = new Sprite(ur, stepLoad);
  16. }
  17. pic.src = file.toURL();
  18.  
  19. let removeColor = function(ctx, rgb, w, h) {
  20.     var imageData = ctx.getImageData(0, 0, w, h);
  21.  
  22.     // examine every pixel,
  23.     // change any old rgb to the new-rgb
  24.     for (var i = 0; i < imageData.data.length; i += 4) {
  25.       // is this pixel the old rgb?
  26.       if (imageData.data[i] == rgb[0] &&
  27.         imageData.data[i + 1] == rgb[1] &&
  28.         imageData.data[i + 2] == rgb[2]
  29.       ) {
  30.         // change to your new rgb
  31.         imageData.data[i] = 0;
  32.         imageData.data[i + 1] = 0;
  33.         imageData.data[i + 2] = 0;
  34.         imageData.data[i + 3] = 0;
  35.       }
  36.     }
  37.     // put the altered data back on the canvas  
  38.     ctx.putImageData(imageData, 0, 0);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement