css - 如何使用 CSS 控制 SVG 过滤器属性值?
问题描述
我想在 CSS 中创建动画,其中一部分我需要控制 SVG 过滤器属性的值。
下面,我尝试在specularConstant属性中调用 CSS 变量,浏览器返回错误。
或者,是否可以使用 CSS 代码中的选择器设置属性,或者有没有办法通过 CSS 控制这样的 SVG 属性?
下面是一个可重现的例子:
@property --illumination-power {
syntax: '<number>';
initial-value: 1;
inherits: true
}
:root {
animation: my-animation 5s;
}
@keyframes my-animation {
0% {
--illumination-power: 0;
}
100% {
--illumination-power: 1.2;
}
}
body {
margin: 0;
background-color: black;
}
<svg width="100vw" height="100vh">
<defs>
<filter id="spotlight">
<feSpecularLighting specularConstant="var(--illumination-power)"
specularExponent="10" lighting-color="white">
<fePointLight x="200" y="100" z="70"/>
</feSpecularLighting>
</filter>
</defs>
<rect id="light-background" x="-500%" y="-500%" width="1000%" height="1000%" fill-opacity="0" filter="url(#spotlight)"/>
</svg>
解决方案
您需要使用 SVG<animate>
元素或 JavaScript 来执行此操作。
推荐阅读
- segmentation-fault - 有条件的跳转或移动取决于未初始化的值(Segmentation Fault / Fortran)
- python - 如何使用 Microsoft graph rest API 从 onedrive 读取 docx 文件
- android-studio - 如何解决此错误:数据绑定注释处理器版本需要与 Android Gradle 插件版本匹配
- javascript - 如何根据价值加入reactjs中的房间?
- c# - 使用异步时无法清除模态
- java - 使用jar包时,Runtime.getRuntime().exec无法获取bash_profile环境变量
- xamarin.forms - xamarin.forms crossmediaplugin:录制没有声音的视频
- python - 我试图改变我的游戏中的水平,但不会工作
- javascript - 如何使用 JQuery 切换下拉菜单 onclick
- php - 如何在 cURL 中管理带有特殊字符的 URL?