gpt4 book ai didi

javascript - SVG 过滤器 : width 100% height 100% does not cover image

转载 作者:行者123 更新时间:2023-12-03 07:03:07 26 4
gpt4 key购买 nike

我有一个简单的 SVG 过滤器。如果您单击下面的示例,该过滤器将出现/消失:

var image = document.querySelector('image');

var url = 'https://i.picsum.photos/id/202/200/200.jpg?hmac=eGzhW5P2k0gzjc76Tk5T9lOfvn30h3YHuw5jGnBUY4Y';

image.setAttribute('xlink:href', url);

window.addEventListener('click', function() {
var filter = image.getAttribute('filter')
? ''
: 'url(#blur)';
image.setAttribute('filter', filter);
})
image {
background: red;
}
<svg width='200' height='200'>
<filter id='blur' width='100%' height='100%'>
<feGaussianBlur stdDeviation='2' result='blur' />
</filter>
<image x='0' width='200px' height='200px' xlink:href='' id='svg-image' filter='url(#blur)' />
</svg>

我希望过滤后的图像与未过滤的图像大小相同。有谁知道我该怎么做?任何指针将不胜感激!

最佳答案

您需要使用 xy 属性设置初始维度,如下所示。

<filter id='blur' x='0' y='0' width='100%' height='100%'>
...

var image = document.querySelector('image');

var url = 'https://i.picsum.photos/id/202/200/200.jpg?hmac=eGzhW5P2k0gzjc76Tk5T9lOfvn30h3YHuw5jGnBUY4Y';

image.setAttribute('xlink:href', url);

window.addEventListener('click', function() {
var filter = image.getAttribute('filter')
? ''
: 'url(#blur)';
image.setAttribute('filter', filter);
})
image {
background: red;
}
<svg width='200' height='200'>
<filter id='blur' x='0' y='0' width='100%' height='100%'>
<feGaussianBlur stdDeviation='2' result='blur' />
</filter>
<image x='0' width='200px' height='200px' xlink:href='' id='svg-image' filter='url(#blur)' />
</svg>

关于javascript - SVG 过滤器 : width 100% height 100% does not cover image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64540567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com