Fabric Js Image混合滤镜问题

时间:2015-07-05 05:18:19

标签: javascript fabricjs

当我尝试将混合滤镜应用于结构JS中的图像时,我会遇到一些奇怪的行为。当我应用滤镜时,它只会使图像变白而不是添加指定的颜色。谁能说清楚我做错了什么?下面是代码和JS小提琴的链接,我尝试添加过滤器。 谢谢 http://jsfiddle.net/ahalbleib/bdofdbqg/2/

function init() {
    var canvas = this.__canvas = new fabric.Canvas('c1', {
        hoverCursor: 'pointer'
    });

    var urls = ['https://dl.dropboxusercontent.com/s/ix6mvv49wnx226a/Central-Richmon_clipped_rev_1.png?dl=0',
        'https://dl.dropboxusercontent.com/s/jjp2l0kgdw8iitb/Laurel-Heights.png?dl=0',
        'https://dl.dropboxusercontent.com/s/wdk02w40z1466g5/LoneMountain.png?dl=0',
        'https://dl.dropboxusercontent.com/s/t6tnptndu2k22xr/OuterRichmond.png?dl=0',
        'https://dl.dropboxusercontent.com/s/tv4rhwjc0nw35iz/Presidio-Heights.png?dl=0',
        'https://dl.dropboxusercontent.com/s/ttbf390w2vdx4id/Inner-richmond.png?dl=0'];
    for (var i = 0; i < urls.length; i++) {
        fabric.Image.fromURL(urls[i], function (img) {

            img.perPixelTargetFind = true;
            img.targetFindTolerance = 4;
            img.hasControls = img.hasBorders = false;
            img.filters.push(filter);
            var filter = new fabric.Image.filters.Blend({
                image: img
            });
            filter.color = '#00f900';
            filter.mode = 'add';
            img.filters.push(filter);
            canvas.add(img).renderAll();
            img.applyFilters(canvas.renderAll.bind(canvas));
        }, {
            crossOrigin: 'Anonymous'
        });
    }

    canvas.findTarget = (function (originalFn) {
        return function () {
            var target = originalFn.apply(this, arguments);
            if (target) {
                if (this._hoveredTarget !== target) {
                    canvas.fire('object:over', {
                        target: target
                    });
                    if (this._hoveredTarget) {
                        canvas.fire('object:out', {
                            target: this._hoveredTarget
                        });
                    }
                    this._hoveredTarget = target;
                }
            } else if (this._hoveredTarget) {
                canvas.fire('object:out', {
                    target: this._hoveredTarget
                });
                this._hoveredTarget = null;
            }
            return target;
        };
    })(canvas.findTarget);
}

init();

2 个答案:

答案 0 :(得分:0)

更改

var filter = new fabric.Image.filters.Blend({
  image: img
});

var filter = new fabric.Image.filters.Blend({});

文档在这方面非常混乱,但基本上图像参数用于定义要与目标混合的图像。在您的示例中,您试图将图像与自身混合,从而产生完全白色的图像。

答案 1 :(得分:0)

我认为你想要使用Tint过滤器而不是Blend过滤器。

替换当前的过滤器代码
img.filters.push(new fabric.Image.filters.Tint({
    color: '#00f900',
    // adjust as required
    opacity: 0.1
}));

你的图像不是完全白色的,只有当添加的(img.red + img.red,img.blue + ...)颜色超过255时才会显示白色(这实际上是大部分图像,但你仍然可以看到图像的微弱阴影。如果你有一个更暗的图像,你可以更好地看到这种效果)

小提琴 - http://jsfiddle.net/m37gyLbf/