2014-09-23 194 views
0

我试过了,但我无法做到。
我选择一个图像显示在<img src=" " onload="getPixels(this)" id="mug"/>然后该功能改变图像颜色。更改图片onclick并更改画布上的图像颜色

当我再次更改图像时,其颜色不会改变,但会改变第一张图像的颜色,而不是更改新图像的颜色。

帮助我,请我没有任何想法:(

<script src="//code.jquery.com/jquery-1.10.2.js"></script> 

      <img src="<?=''?>" onload="getPixels(this)" id="mug"/> 

     <a onClick="showImage(this);" href="#bg/big-polka-dots.png" data-href="bg/big-polka-dots.png"> 
     <img src="bg/big-polka-dots.png" width="35" height="56"/> 
     </a> 

    <a onClick="showImage(this)" href="#bg/chevron-navy.png" data-href="bg/chevron-navy.png"> 
     <img src="bg/chevron-navy.png" width="35" height="56"/> 
     </a> 
     <a onClick="showImage(this)" href="#bg/maple-leaf-lattice.png" data-href="bg/maple-leaf-lattice.png"> 
     <img src="bg/maple-leaf-lattice.png" width="35" height="56"/> 
     </a> 
     <a onClick="showImage(this)" href="#bg/stars-navy.png" data-href="bg/stars-navy.png"> 
     <img src="bg/stars-navy.png" width="35" height="56"/> 
     </a> 

<script> 
function showImage(link) 
{ 
    var url = link.href; 
    url = url.substr(url.indexOf('#')+1); 
    document.getElementById("mug").src=url; 

} 
</script> 


<div style="background-color:#B4CBEC; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#16becf; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#ade8e8; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#57d2c8; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#77989f; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#8dc63f; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#45b44a; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#b9eac6; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#2fb595; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#38605b; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#92278f; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#b16fc1; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#dbbadb; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#b3b3b3; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#f9ed32; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#fbb040; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#be1e2d; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#f16667; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#fabfa5; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#f0a0ab; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#808080; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div style="background-color:#FFF; width:40px; height:40px; margin:5px; float:left; cursor:pointer; border: 1px double #EAE8E8;"></div> 
<div id="result" name="result" style="visibility:hidden; width:40px; height:40px; margin:5px; float:left;"></div> 

<script> 
     var mug = document.getElementById("mug"); 
     var canvas = document.createElement("canvas"); 
     var ctx = canvas.getContext("2d"); 
     var originalPixels = null; 
     var currentPixels = null; 

     function HexToRGB(Hex) 
     { 
      var Long = parseInt(Hex.replace(/^#/, ""), 16); 
      return { 
       R: (Long >>> 16) & 0xff, 
       G: (Long >>> 8) & 0xff, 
       B: Long & 0xff 
      }; 
     } 
</script> 
<script> 
function rgbToHex(color) { 
    if (color.substr(0, 1) === "#") { 
     return color; 
    } 
    var nums = /(.*?)rgb\((\d+),\s*(\d+),\s*(\d+)\)/i.exec(color), 
     r = parseInt(nums[2], 10).toString(16), 
     g = parseInt(nums[3], 10).toString(16), 
     b = parseInt(nums[4], 10).toString(16); 
    return "#"+ (
     (r.length == 1 ? "0"+ r : r) + 
     (g.length == 1 ? "0"+ g : g) + 
     (b.length == 1 ? "0"+ b : b) 
    ); 
} 
$("div").click(function() { 
    var color = $(this).css("background-color"); 
    var color2 = rgbToHex(color); 
    changeColor(document.getElementById("result").innerHTML = color2); 
    }); 

    function changeColor() 
     { 
      if(!originalPixels) return; // Check if image has loaded 
      if(document.getElementById("result").innerHTML =='') return; 
      var newColor = HexToRGB(document.getElementById("result").innerHTML); 

      for(var I = 0, L = originalPixels.data.length; I < L; I += 4) 
      { 
       if(currentPixels.data[I + 3] > 0) 
       { 
        currentPixels.data[I] = originalPixels.data[I]/255 * newColor.R; 
        currentPixels.data[I + 1] = originalPixels.data[I + 1]/255 * newColor.G; 
        currentPixels.data[I + 2] = originalPixels.data[I + 2]/255 * newColor.B; 
       } 
      } 

      ctx.putImageData(currentPixels, 0, 0); 
      mug.src = canvas.toDataURL("image/png"); 
     } 

     function getPixels(img) 
     { 
      canvas.width = img.width; 
      canvas.height = img.height; 

      ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight, 0, 0, img.width, img.height); 
      originalPixels = ctx.getImageData(0, 0, img.width, img.height); 
      currentPixels = ctx.getImageData(0, 0, img.width, img.height); 

      img.onload = null; 

     } 
</script> 

回答

0

在你换色功能,您随时更改杯结束它:

mug.src = canvas.toDataURL("image/png"); 

你也应该更新通过在更改图像时调用getPixels存储在原始像素中的图像数据