Change png image color in fabric js

夙愿已清 提交于 2019-12-11 07:38:34

问题


Is there any way to change the color of the image using fabricJS ? Applying Tint filter will just add color not changing the original color.


回答1:


For an image like this you can use the hue rotation filter.

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>


来源:https://stackoverflow.com/questions/48206936/change-png-image-color-in-fabric-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!