svg - svg - 如何绘制椭圆渐变?
问题描述
我阅读了 SVG 文档,但找不到解决方案。如何绘制如下图所示的 SVG 椭圆渐变?
我使用 svgradialGradient 但它呈现了我不想要的圆形。所以接下来我画椭圆并用radialGradient填充它,我最终得到了这个:
我使用 svg 蒙版来裁剪椭圆,但它没有按预期工作。有没有办法做到这一点?
svg 代码链接: https ://codesandbox.io/s/fancy-dew-skokw
<svg
tabindex="0"
focusable="true"
shape-rendering="optimizeSpeed"
viewBox="0 0 640 545"
style="border: 1px solid blue; width: 100%; height: 100%;"
>
<g id="text_7">
<rect
x="138"
y="455"
width="355"
height="60"
style=" mask: url('#mask_ellipse_7)'"
></rect>
<ellipse
cx="292"
cy="80"
rx="116"
ry="51"
transform="matrix(1,0,0,1,65,400)"
style="fill:url('#bg-ellipse-gradient_7'); "
></ellipse>
<text font-size="12" transform="matrix(1,0,0,1,65,400)">
<tspan x="75" y="67" dy="0" fill="#FF0000" fill-opacity="1">
Lorep ipsum - radial gradient
</tspan>
</text>
<defs>
<radialGradient
id="bg-ellipse-gradient_7"
fx="74%"
fy="37%"
>
<stop
stop-color="#00FFFF"
stop-opacity="1"
offset="0.000000"
></stop>
<stop
stop-color="#FFFF00"
stop-opacity="1"
offset="0.500000"
></stop>
<stop
stop-color="#000000"
stop-opacity="1"
offset="1.000000"
></stop>
</radialGradient>
</defs>
<defs>
<mask
id="mask_ellipse_7"
cx="292"
cy="80"
rx="116"
ry="51"
transform="matrix(1,0,0,1,65,400)"
style="fill:url('#bg-ellipse-gradient_7'); "
></mask>
</defs>
</g>
</svg>
解决方案
要获得第一个屏幕截图中的图像,您需要应用蒙版。
蒙版由两个矩形组成:第一个矩形fill ="black"
切割所有东西
第二个矩形fill ="white"
用椭圆留下所需部分
<mask id="mask_ellipse_7" >
<rect width="100%" height="100%" fill="black" />
<rect x="138" y="455" width="355" height="60" stroke="red" fill="white" />
</mask>
以下是完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Static Template</title>
</head>
<body>
<svg
tabindex="0"
focusable="true"
shape-rendering="optimizeSpeed"
viewBox="0 400 640 545"
style="border: 1px solid blue; width: 100%; height: 100%;"
>
<g id="text_7" style="mask:url(#mask_ellipse_7)">
<rect
x="138.734375"
y="455.703125"
width="355"
height="60"
fill="black"
></rect>
<ellipse
cx="292.000183"
cy="80.999115"
rx="116.99969500000003"
ry="51.003084999999984"
transform="matrix(1,0,0,1,65,400)"
style="fill:url('#bg-ellipse-gradient_7'); "
></ellipse>
<text font-size="12" transform="matrix(1,0,0,1,65,400)">
<tspan x="75" y="67" dy="0" fill="#FF0000" fill-opacity="1">
Lorep ipsum - radial gradient
</tspan>
</text>
</g>
<defs>
<radialGradient
id="bg-radial-gradient_7"
gradientTransform="matrix(1,0,0,1,65,400)"
gradientUnits="userSpaceOnUse"
fx="304"
fy="49"
cx="292.000183"
cy="80.999115"
r="127.63323747993802"
>
<stop
stop-color="#00FFFF"
stop-opacity="1"
offset="0.000000"
></stop>
<stop
stop-color="#FFFF00"
stop-opacity="1"
offset="0.500000"
></stop>
<stop
stop-color="#000000"
stop-opacity="1"
offset="1.000000"
></stop>
</radialGradient>
</defs>
<defs>
<radialGradient
id="bg-ellipse-gradient_7"
fx="74.32765053294222%"
fy="37.120593444654716%"
>
<stop
stop-color="#00FFFF"
stop-opacity="1"
offset="0.000000"
></stop>
<stop
stop-color="#FFFF00"
stop-opacity="1"
offset="0.500000"
></stop>
<stop
stop-color="#000000"
stop-opacity="1"
offset="1.000000"
></stop>
</radialGradient>
</defs>
<defs>
<mask
id="mask_ellipse_7" >
<rect width="100%" height="100%" fill="black" />
<rect x="138" y="455" width="355" height="60" stroke="red" fill="white" />
</mask>
</defs>
</svg>
</body>
</html>
推荐阅读
- sql - 使用 bigquery 查找特定日期的整体数据
- amazon-web-services - 如何从 ec2 安全组授予对 s3 存储桶的完全访问权限
- google-cloud-platform - 如何在谷歌云中识别组织的所有者
- rust - 在“if let Some(ref mut x) = option”和“if let Some(x) = option.as_mut()”中匹配可变选项引用有什么区别?
- c - 为什么在 asm 中“guarded do”比“jump to middle”好
- apache-spark - 如何在 SparkSQL 中收集“视图”的统计信息?
- discord.py - discord.py:命名执行命令的通道
- javascript - 如何使用给定限制验证 cron 表达式?
- javascript - 'if (!array.includes(item))' 是用于检查数组是否不包含项的正确 JavaScript 语法吗?
- javascript - 从java脚本中的数组中获取小数和绝对值