在fabric js中更改png图像颜色

时间:2018-01-11 12:19:46

标签: image html5-canvas fabricjs

enter image description here

有没有办法使用fabricJS更改图像的颜色?应用色调滤镜只会添加不改变原始颜色的颜色。

1 个答案:

答案 0 :(得分:2)

对于这样的图像,您可以使用色调旋转滤镜。

var canvas = new fabric.StaticCanvas('c');
var image;
var imgEl = document.createElement('img');
imgEl.crossOrigin = 'anonymous';
imgEl.onload = function() {
  image = new fabric.Image(imgEl);
  image.filters = [new       fabric.Image.filters.HueRotation()];
  canvas.add(image);
}
imgEl.src = 'https://i.imgur.com/28kU1bo.png';


document.getElementById('hue').onclick= function() {
  image.filters[0].rotation = 2 * Math.random() - 1;
  console.log(image.filters[0].rotation);
  image.applyFilters();
  canvas.requestRenderAll();
};
<script src="https://rawgithub.com/kangax/fabric.js/master/dist/fabric.js"></script>
<button id="hue">Change to random color</button>
<canvas id="c" width=600 height=600 ></canvas>