Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change png image color in fabric js

enter image description here

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.

like image 940
infinitywarior Avatar asked Mar 08 '23 04:03

infinitywarior


1 Answers

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>
like image 199
AndreaBogazzi Avatar answered Mar 15 '23 07:03

AndreaBogazzi