简体   繁体   English

将背景色和图像滤镜组合应用到svg路径

[英]Apply combination of background colour and image filter to a svg path

I have a svg path where I apply both a background colour as well as a filter containing a (possible scaled and translated) image. 我有一个svg路径,我在其中应用背景色以及包含(可能缩放和转换的)图像的滤镜。

First the background colour is applied using the css fill property: myPath.css('fill', bgColour); 首先,使用css fill属性应用背景色: myPath.css('fill', bgColour);

Afterwards the filter is added like this: 之后,将过滤器添加如下:

var filter = svg.filter(defs, 'myImageFilter',
    0, 0, scale + '%', scale + '%',
    {
        filterUnits: 'objectBoundingBox'
    }
);

// add the image
var outputSlot = 'myImage' + sectionNumber;
svg.filters.image(filter, outputSlot, pathToImageFile);
// move it
svg.filters.offset(filter, 'movedImage', outputSlot, moveX, moveY);
// combine image with path for clipping
svg.filters.composite(filter, 'clip', 'in', 'movedImage', 'SourceGraphic');
// mix both images
svg.filters.blend(filter, '', 'multiply', 'SourceGraphic', 'clip');

// assign filter to the path
myPath.attr('filter', 'url(#myImageFilter)');

The problem I have with that is the background colour affects the image, too (as if it was painted above the image). 我的问题是背景颜色也会影响图像(就像它被绘制在图像上方一样)。 What I need is that the image is placed on top of the background instead. 我需要的是将图像放置在背景上方。

Any idea about how to achieve this? 关于如何实现这一目标的任何想法?

Do I maybe need to duplicate the whole path to have it one time for the background colour only and one time for the image? 我是否可能需要复制整个路径,以使它只对背景色一次,对图像一次呢?

It's probably the "multiply" in your svg.filters.blend. 它可能是svg.filters.blend中的“乘积”。 Just use "normal" instead of "multiply". 只需使用“普通”而不是“乘”即可。

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

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