i want to use caman js and fabric js to give effect on an image in same canvas. how can i combine them?

这一生的挚爱 提交于 2019-12-14 03:52:14

问题


I want to give effect to image using both caman js and fabric js. I tried to combine the code but it gives two images instead of one. What can i do to combine caman js and fabric js and fix this? I reffered to: http://camanjs.com/ and http://fabricjs.com/articles/ Thank you.


回答1:


I don't think there is a direct way to combine fabric and caman. Here is a workaround that i have used (Just a simple one. You can enhance it as per your wish) :

HTML

<canvas id="canvas" width="600" height="300"></canvas>

Javascript

var canvas = new fabric.Canvas('canvas');

fabric.Image.fromURL('image.png', function(img) {
  img.set({
    left: 10,
    top: 50
  });

  canvas.add(img).setActiveObject(img);
  canvas.renderAll();

  var hiddenImg = document.createElement('img');
  hiddenImg.src = canvas.getActiveObject().toDataURL();
  hiddenImg.id = 'target';
  hiddenImg.style.display = 'none';
  document.body.appendChild(hiddenImg);

  Caman('#target', function(value) {
    this.brightness(10);
    this.contrast(20);

    this.render(function() {
      canvas.getActiveObject().setSrc(document.getElementById('target').toDataURL(), function() {
        canvas.renderAll();
      });
    });
  });
});

Here is the fiddle: http://jsfiddle.net/k7moorthi/dfp1b0mq/

NOTE: Set crossOrigin option to fabric.Image.fromURL() method if you want to use external images to avoid browser security blocking for toDataURL() method. Eg:

fabric.Image.fromURL('https://s3-eu-west-1.amazonaws.com/kienzle.dev.cors/img/image2.png', function(img) {
  ...
}, { crossOrigin: 'anonymous' });


来源:https://stackoverflow.com/questions/28123799/i-want-to-use-caman-js-and-fabric-js-to-give-effect-on-an-image-in-same-canvas

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