使用Javascript/jQuery和CSS更改PNG颜色

hai*_*ina 13 javascript css jquery png image

我有一个黑色的心PNG图像我想用不同的颜色显示.如何使用javascript/css/jquery更改心脏的颜色?

我正在努力打造一名衬衫设计师.所以背景是衬衫,心脏是印花设计(以及其他形状).

PS我知道这已经是重复但似乎没有任何解决方案有帮助.希望你们能帮助我,如果你已经这样做了.非常感谢!

解决方案更新:

解决方案是使用canvas.发现我的解决方案在这里.这是代码:

<h4>Original Image</h4>
<img id="testImage" src='black-heart.png'/>

<h4>Image copied to canvas</h4>
<canvas id="canvas" width="128" height="128"></canvas>

<h4>Modified Image copied to an image tag</h4>
<img id="imageData"/>



<script>
var canvas = document.getElementById("canvas"),
    ctx = canvas.getContext("2d"),
    image = document.getElementById("testImage");

ctx.drawImage(image,0,0);

var imgd = ctx.getImageData(0, 0, 128, 128),
    pix = imgd.data,
    uniqueColor = [0,0,255]; // Blue for an example, can change this value to be anything.

// Loops through all of the pixels and modifies the components.
for (var i = 0, n = pix.length; i <n; i += 4) {
      pix[i] = uniqueColor[0];   // Red component
      pix[i+1] = uniqueColor[1]; // Blue component
      pix[i+2] = uniqueColor[2]; // Green component
      //pix[i+3] is the transparency.
}

ctx.putImageData(imgd, 0, 0);


var savedImageData = document.getElementById("imageData");
savedImageData.src = canvas.toDataURL("image/png"); 
</script>
Run Code Online (Sandbox Code Playgroud)

ada*_*shr 8

招数1

已经创建了多个图像(使用照片编辑软件,如Gimp或Photoshop),并使用jQuery简单地更改图像源.

招数2

另一种选择是在其中安装带有透明心形孔的PNG,并使用jQuery更改背景颜色.

  • +1 表示第二个技巧,只有当他们的页面背景允许简单的背景(如白色)时才有效。 (2认同)