简体   繁体   English

在fabric.js中用颜色填充图像

[英]Fill image inside with color in fabric.js

Basically, I have a png transparent image and I want to fill color inside the image while maintaining the outer transparency using fabricjs. 基本上,我有一个png透明图像,我想在图像内填充颜色,同时使用fabricjs保持外部透明度。

I tried using Tint and it works for the below image. 我尝试使用Tint,它适用于下面的图像。

在此输入图像描述

var canvas = this.__canvas = new fabric.Canvas('myCanvas'),
f = fabric.Image.filters;

fabric.Image.fromURL('imgpath/img.png', function (img) {
    var oImg = img.set({ left: 50, top: 100 }).scale(1);
    var tint = new fabric.Image.filters.Tint({
        color: '#ff6c78',
        opacity: 1
    });

    oImg.filters.push(tint);
    oImg.applyFilters(canvas.renderAll.bind(canvas));

    canvas.add(oImg).renderAll();
    canvas.setActiveObject(oImg);
 });

Now, I have an image which is transparent on the inside as well as out. 现在,我有一个内部和外部透明的图像。 Is it possible to get similar result for the below image? 是否可以获得下面图像的类似结果?

在此输入图像描述

  • (a) Original image (a)原始图像
  • (b) The result using Tint (b)使用Tint的结果
  • (c) The result I am actually looking for (c)我实际寻找的结果

I am not sure whether this is entirely possible using fabricjs. 我不确定这是否完全可以使用fabricjs。 If not, then what other javascript frameworks are there that can be used? 如果没有,那么还有哪些其他javascript框架可以使用?

Below are the original images. 以下是原始图像。

在此输入图像描述 在此输入图像描述

Any help is appreciated. 任何帮助表示赞赏。 Thank you. 谢谢。

Since these are vector images. 因为这些是矢量图像。 I would convert them to SVG objects and then all you have to do is change the fill: yourColorChoice; 我会将它们转换为SVG对象,然后你要做的就是改变fill: yourColorChoice; of the <path> element in the SVG. SVG中的<path>元素。

With this method you don't need any JS, it can all be done via pure CSS. 使用这种方法你不需要任何JS,它可以通过纯CSS完成。

You can convert images to SVG elements using Adobe Illustrator (I think theres some free software that does it as well) 您可以使用Adobe Illustrator将图像转换为SVG元素(我认为还有一些免费软件可以使用它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM