SVG 偏移与阴影:四阶段链及现代投影
保留原图偏移、模糊偏移、Alpha黑影、矩阵暗化四组,并区分单纯偏移和真正投影。
学完这一章,你能够
- 保留原图偏移、模糊偏移、Alpha黑影、矩阵暗化四组,并区分单纯偏移和真正投影。
- 保留原图偏移、模糊偏移、Alpha黑影、矩阵暗化四组,并区分单纯偏移和真正投影。
偏移本身不保留原图
feOffset 用 dx/dy 平移输入,正值通常向右/下,负值向左/上,具体映射还受坐标系影响。仅一个 offset 输出的是移动后的图像,不会自动同时留下原图,因此不能把任何偏移都称为完整阴影。
第一阶段将 SourceGraphic 偏移20/20后与原图 normal 混合,显示两份;第二阶段在偏移结果上加模糊,再与原图合成。每一步都给中间结果名称,防止引用错链。
第一阶段是原图和偏移副本的组合。
第二阶段在偏移结果上加入模糊,再保留原图。
黑影与暗色影不同
第三阶段用 SourceAlpha 产生黑色透明度轮廓后模糊、混合,不保留原始颜色。第四阶段对彩色偏移的RGB分别乘.2而保留alpha,再模糊;它趋近暗色,并不保证变成纯黑。
第三阶段用SourceAlpha形成黑色透明度阴影。
第四阶段对RGB乘.2暗化,和纯黑阴影分别解释。
四份独立文件和相应说明分别保留,不用一个综合投影替代教学层次。滤镜区与外层画布同时扩大,避免示例源码完整却看不到阴影边缘。
feDropShadow 的补充方式
现代 drop-shadow.svg 用 feDropShadow 设置 dx12/dy10、标准差4、灰色及.5不透明度,同时保留原图。它是便利的投影原语,与单一 feOffset 功能不同。
flood-color/flood-opacity 负责颜色和透明度,stdDeviation 仍是标准差。实际渲染应比较输入、目标及滤镜前后,不能仅以某个原语标签被浏览器解析认定阴影成功。
四阶段及便利投影
| 分支 | 链路 | 预期 |
| 原图偏移 | SourceGraphic→offset→blend | 原图和彩色偏移 |
| 模糊偏移 | offset→blur→blend | 柔化彩色偏移 |
| 黑色阴影 | SourceAlpha→offset→blur→blend | 黑色轮廓 |
| 矩阵暗化 | RGB*.2→blur→blend | 暗色但非保证纯黑 |
| feDropShadow | dx/dy/stdDeviation/flood | 便利投影且保留原图 |
| dx/dy | 默认0,可正负 | 用户空间平移,非自动保留原图 |
本章独立核心源码与验证步骤
展开查看收起
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="320" height="240" viewBox="0 0 320 240" role="img" aria-labelledby="title desc" ><title id="title">偏移并与原图混合 · 蓝色天空网</title><desc id="desc">原图与偏移图同时存在</desc><defs><filter id="effect" x="-40%" y="-40%" width="200%" height="200%" color-interpolation-filters="sRGB"><feOffset in="SourceGraphic" dx="20" dy="20" result="off"/><feBlend in="SourceGraphic" in2="off" mode="normal"/></filter></defs><g id="subject"><rect id="shape" x="65" y="45" width="150" height="110" fill="#f44336" stroke="#1565c0" stroke-width="4" filter="url(#effect)"/></g><text x="12" y="232" fill="#325b86" font-size="12">蓝色天空网 · mbsky.com</text></svg>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 保留原图偏移、模糊偏移、Alpha黑影、矩阵暗化四组,并区分单纯偏移和真正投影。
偏移后模糊
展开查看收起
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="320" height="240" viewBox="0 0 320 240" role="img" aria-labelledby="title desc" ><title id="title">偏移后模糊 · 蓝色天空网</title><desc id="desc">保留原图及彩色模糊偏移</desc><defs><filter id="effect" x="-40%" y="-40%" width="200%" height="200%" color-interpolation-filters="sRGB"><feOffset in="SourceGraphic" dx="20" dy="20" result="off"/><feGaussianBlur in="off" stdDeviation="6" result="blur"/><feBlend in="SourceGraphic" in2="blur" mode="normal"/></filter></defs><g id="subject"><rect id="shape" x="65" y="45" width="150" height="110" fill="#f44336" stroke="#1565c0" stroke-width="4" filter="url(#effect)"/></g><text x="12" y="232" fill="#325b86" font-size="12">蓝色天空网 · mbsky.com</text></svg>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 保留原图及彩色模糊偏移
透明度通道阴影
展开查看收起
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="320" height="240" viewBox="0 0 320 240" role="img" aria-labelledby="title desc" ><title id="title">透明度通道阴影 · 蓝色天空网</title><desc id="desc">用SourceAlpha产生黑色阴影并保留原图</desc><defs><filter id="effect" x="-40%" y="-40%" width="200%" height="200%" color-interpolation-filters="sRGB"><feOffset in="SourceAlpha" dx="20" dy="20" result="off"/><feGaussianBlur in="off" stdDeviation="6" result="blur"/><feBlend in="SourceGraphic" in2="blur" mode="normal"/></filter></defs><g id="subject"><rect id="shape" x="65" y="45" width="150" height="110" fill="#f44336" stroke="#1565c0" stroke-width="4" filter="url(#effect)"/></g><text x="12" y="232" fill="#325b86" font-size="12">蓝色天空网 · mbsky.com</text></svg>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 用SourceAlpha产生黑色阴影并保留原图
矩阵暗化彩色偏移
展开查看收起
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="320" height="240" viewBox="0 0 320 240" role="img" aria-labelledby="title desc" ><title id="title">矩阵暗化彩色偏移 · 蓝色天空网</title><desc id="desc">RGB乘.2保留alpha,不是任意颜色变成纯黑</desc><defs><filter id="effect" x="-40%" y="-40%" width="200%" height="200%" color-interpolation-filters="sRGB"><feOffset in="SourceGraphic" dx="20" dy="20" result="off"/><feColorMatrix in="off" type="matrix" values="0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0" result="dark"/><feGaussianBlur in="dark" stdDeviation="6" result="blur"/><feBlend in="SourceGraphic" in2="blur" mode="normal"/></filter></defs><g id="subject"><rect id="shape" x="65" y="45" width="150" height="110" fill="#f44336" stroke="#1565c0" stroke-width="4" filter="url(#effect)"/></g><text x="12" y="232" fill="#325b86" font-size="12">蓝色天空网 · mbsky.com</text></svg>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- RGB乘.2保留alpha,不是任意颜色变成纯黑
现代投影写法
展开查看收起
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="320" height="240" viewBox="0 0 320 240" role="img" aria-labelledby="title desc" ><title id="title">现代投影写法 · 蓝色天空网</title><desc id="desc">feDropShadow同时保留原图并构造有色投影</desc><defs><filter id="effect" x="-40%" y="-40%" width="200%" height="200%"><feDropShadow dx="12" dy="10" stdDeviation="4" flood-color="gray" flood-opacity=".5"/></filter></defs><g id="subject"><rect id="shape" x="65" y="45" width="150" height="110" fill="#f44336" stroke="#1565c0" stroke-width="4" filter="url(#effect)"/></g><text x="12" y="232" fill="#325b86" font-size="12">蓝色天空网 · mbsky.com</text></svg>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- feDropShadow同时保留原图并构造有色投影
本章附件与实例包
常见错误与边界检查
- 把正确的http命名空间误改为https。
- 仅看标签/数量就认定渲染或知识完整。
技术规范与官方资料
练习与参考解答
查看参考解答
逐项比较真实图形与对应源码,分开记录几何、像素及运行环境;未知条件继续待办。