响应式 SVG 填充形状
Responsive SVG fill shape
#fg {
fill: url(#ship);
stroke: #fff;
stroke-width: 10px;
stroke-dasharray: 1024px;
-webkit-animation: dash 2s;
animation: dash 2s;
}
@keyframes dash {
from {
stroke-dashoffset: 1024px;
}
to {
stroke-dashoffset: 0px;
}
}
@-webkit-keyframes dash {
from {
stroke-dashoffset: 1024px;
}
to {
stroke-dashoffset: 0px;
}
}
#bg {
fill: white;
stroke: none;
transform-origin: 1270px 550px;
-webkit-animation: bgfill 2s linear 2s forwards;
animation: bgfill 2s linear 2s forwards;
}
@keyframes bgfill {
from {
transform: scale(1);
}
to {
transform: scale(4);
}
}
@-webkit-keyframes bgfill {
from {
transform: scale(1);
}
to {
transform: scale(4);
}
}
#home {
height: 100vh;
background-image: url("http://4.bp.blogspot.com/-yB6Tc3mleuE/T4NkAKeazYI/AAAAAAAACB0/tlichKzIu3Q/s1600/Simple+unique+odd+weird+wallpapers+minimalistic+%2330+battleship+titanic.jpg");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
overflow: hidden;
}
<div class="background">
<svg viewBox="0 0 1376 764">
<defs>
<clipPath id="ship">
<use xlink:href="#shape" id="bg" />
</clipPath>
<path id="shape" d="M1034.5,770.5a125.59,125.59,0,0,0-17-32c-12.32-16.72-25.68-25.84-33-31-6-4.23-59.88-42.55-100-90-13.64-16.14-20.57-24.48-26-38-15-37.48-3.73-73.65,0-85,8.68-26.45,23-43.26,35-57,19-21.82,33.56-29.9,67-54,33.68-24.27,55.39-39.91,77-60,40.56-37.69,35.94-49.35,81-96,34.18-35.39,51.27-53.08,79-65,7.79-3.35,76-31.44,140,2a142.06,142.06,0,0,1,31,22l7.5,7.5 L 1376,770.5 Z" />
</defs>
<image xlink:href="http://4.bp.blogspot.com/-yB6Tc3mleuE/T4NkAKeazYI/AAAAAAAACB0/tlichKzIu3Q/s1600/Simple+unique+odd+weird+wallpapers+minimalistic+%2330+battleship+titanic.jpg"
width="1376" height="764" preserveAspectRatio="xMidYMid slice" clip-path="url(#ship)" />
<use xlink:href="#shape" id="fg" />
</svg>
</div>
我正在尝试创建页面加载动画,首先绘制白线,然后我想用白色 SVG 动画填充屏幕,一切正常但我不喜欢锐边填充效果,如何将角的形状更改为白线的形状?请观看动画以更好地理解
只需使用您的原始路径形状并扩大(缩放)它。
对于下面的示例,我删除了您使用的不必要的元素,并稍微修改了主要形状,使其具有右下角(而不仅仅是直线),以便我可以填充它。
我在前面放了一份 (id="fg"),按照你在上一个问题中的要求,有一个破折号动画。在它的后面,我放置了一个带有白色填充的路径 (id="bg") 的副本。然后,当破折号动画完成时,我开始第二个动画,将形状的白色版本放大 4 倍以填充 SVG。
#fg {
fill: pink;
stroke: #fff;
stroke-width: 10px;
stroke-dasharray: 1024px;
-webkit-animation: dash 2s;
animation: dash 2s;
}
@keyframes dash {
from { stroke-dashoffset: 1024px; }
to { stroke-dashoffset: 0px; }
}
@-webkit-keyframes dash {
from { stroke-dashoffset: 1024px; }
to { stroke-dashoffset: 0px; }
}
#bg {
fill: white;
stroke: none;
transform-origin: 1270px 550px;
-webkit-animation: bgfill 2s linear 2s forwards;
animation: bgfill 2s linear 2s forwards;
}
@keyframes bgfill {
from { transform: scale(1); }
to { transform: scale(4); }
}
@-webkit-keyframes bgfill {
from { transform: scale(1); }
to { transform: scale(4); }
}
<div id="home" class="section" style="height:100vh;background:pink;">
<div class="background">
<svg viewBox="0 0 1376 764">
<defs>
<path id="shape" d="M1034.5,770.5a125.59,125.59,0,0,0-17-32c-12.32-16.72-25.68-25.84-33-31-6-4.23-59.88-42.55-100-90-13.64-16.14-20.57-24.48-26-38-15-37.48-3.73-73.65,0-85,8.68-26.45,23-43.26,35-57,19-21.82,33.56-29.9,67-54,33.68-24.27,55.39-39.91,77-60,40.56-37.69,35.94-49.35,81-96,34.18-35.39,51.27-53.08,79-65,7.79-3.35,76-31.44,140,2a142.06,142.06,0,0,1,31,22l7.5,7.5 L 1376,770.5 Z"/>
</defs>
<use xlink:href="#shape" id="bg"/>
<use xlink:href="#shape" id="fg"/>
</svg>
</div>
</div>
更新
要用图像填充背景,最简单的方法可能是使用剪辑路径。
#fg {
fill: pink;
stroke: #fff;
stroke-width: 10px;
stroke-dasharray: 1028px;
-webkit-animation: dash 2s;
animation: dash 2s;
}
@keyframes dash {
from { stroke-dashoffset: 1028px; }
to { stroke-dashoffset: 0px; }
}
@-webkit-keyframes dash {
from { stroke-dashoffset: 1028px; }
to { stroke-dashoffset: 0px; }
}
#bg {
fill: url(#kitten);
stroke: none;
transform-origin: 1270px 550px;
-webkit-animation: bgfill 2s linear 2s forwards;
animation: bgfill 2s linear 2s forwards;
}
@keyframes bgfill {
from { transform: scale(1); }
to { transform: scale(4); }
}
@-webkit-keyframes bgfill {
from { transform: scale(1); }
to { transform: scale(4); }
}
<div id="home" class="section" style="height:100vh;background:pink;">
<div class="background">
<svg viewBox="0 0 1376 764">
<defs>
<path id="shape" d="M1034.5,770.5a125.59,125.59,0,0,0-17-32c-12.32-16.72-25.68-25.84-33-31-6-4.23-59.88-42.55-100-90-13.64-16.14-20.57-24.48-26-38-15-37.48-3.73-73.65,0-85,8.68-26.45,23-43.26,35-57,19-21.82,33.56-29.9,67-54,33.68-24.27,55.39-39.91,77-60,40.56-37.69,35.94-49.35,81-96,34.18-35.39,51.27-53.08,79-65,7.79-3.35,76-31.44,140,2a142.06,142.06,0,0,1,31,22l7.5,7.5 L 1376,770.5 Z"/>
<clipPath id="kitten">
<use xlink:href="#shape" id="bg"/>
</clipPath>
</defs>
<image xlink:href="https://placekitten.com/700/350" width="1376" height="764" preserveAspectRatio="xMidYMid slice" clip-path="url(#kitten)"/>
<use xlink:href="#shape" id="fg"/>
</svg>
</div>
</div>
#fg {
fill: url(#ship);
stroke: #fff;
stroke-width: 10px;
stroke-dasharray: 1024px;
-webkit-animation: dash 2s;
animation: dash 2s;
}
@keyframes dash {
from {
stroke-dashoffset: 1024px;
}
to {
stroke-dashoffset: 0px;
}
}
@-webkit-keyframes dash {
from {
stroke-dashoffset: 1024px;
}
to {
stroke-dashoffset: 0px;
}
}
#bg {
fill: white;
stroke: none;
transform-origin: 1270px 550px;
-webkit-animation: bgfill 2s linear 2s forwards;
animation: bgfill 2s linear 2s forwards;
}
@keyframes bgfill {
from {
transform: scale(1);
}
to {
transform: scale(4);
}
}
@-webkit-keyframes bgfill {
from {
transform: scale(1);
}
to {
transform: scale(4);
}
}
#home {
height: 100vh;
background-image: url("http://4.bp.blogspot.com/-yB6Tc3mleuE/T4NkAKeazYI/AAAAAAAACB0/tlichKzIu3Q/s1600/Simple+unique+odd+weird+wallpapers+minimalistic+%2330+battleship+titanic.jpg");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
overflow: hidden;
}
<div class="background">
<svg viewBox="0 0 1376 764">
<defs>
<clipPath id="ship">
<use xlink:href="#shape" id="bg" />
</clipPath>
<path id="shape" d="M1034.5,770.5a125.59,125.59,0,0,0-17-32c-12.32-16.72-25.68-25.84-33-31-6-4.23-59.88-42.55-100-90-13.64-16.14-20.57-24.48-26-38-15-37.48-3.73-73.65,0-85,8.68-26.45,23-43.26,35-57,19-21.82,33.56-29.9,67-54,33.68-24.27,55.39-39.91,77-60,40.56-37.69,35.94-49.35,81-96,34.18-35.39,51.27-53.08,79-65,7.79-3.35,76-31.44,140,2a142.06,142.06,0,0,1,31,22l7.5,7.5 L 1376,770.5 Z" />
</defs>
<image xlink:href="http://4.bp.blogspot.com/-yB6Tc3mleuE/T4NkAKeazYI/AAAAAAAACB0/tlichKzIu3Q/s1600/Simple+unique+odd+weird+wallpapers+minimalistic+%2330+battleship+titanic.jpg"
width="1376" height="764" preserveAspectRatio="xMidYMid slice" clip-path="url(#ship)" />
<use xlink:href="#shape" id="fg" />
</svg>
</div>
我正在尝试创建页面加载动画,首先绘制白线,然后我想用白色 SVG 动画填充屏幕,一切正常但我不喜欢锐边填充效果,如何将角的形状更改为白线的形状?请观看动画以更好地理解
只需使用您的原始路径形状并扩大(缩放)它。
对于下面的示例,我删除了您使用的不必要的元素,并稍微修改了主要形状,使其具有右下角(而不仅仅是直线),以便我可以填充它。
我在前面放了一份 (id="fg"),按照你在上一个问题中的要求,有一个破折号动画。在它的后面,我放置了一个带有白色填充的路径 (id="bg") 的副本。然后,当破折号动画完成时,我开始第二个动画,将形状的白色版本放大 4 倍以填充 SVG。
#fg {
fill: pink;
stroke: #fff;
stroke-width: 10px;
stroke-dasharray: 1024px;
-webkit-animation: dash 2s;
animation: dash 2s;
}
@keyframes dash {
from { stroke-dashoffset: 1024px; }
to { stroke-dashoffset: 0px; }
}
@-webkit-keyframes dash {
from { stroke-dashoffset: 1024px; }
to { stroke-dashoffset: 0px; }
}
#bg {
fill: white;
stroke: none;
transform-origin: 1270px 550px;
-webkit-animation: bgfill 2s linear 2s forwards;
animation: bgfill 2s linear 2s forwards;
}
@keyframes bgfill {
from { transform: scale(1); }
to { transform: scale(4); }
}
@-webkit-keyframes bgfill {
from { transform: scale(1); }
to { transform: scale(4); }
}
<div id="home" class="section" style="height:100vh;background:pink;">
<div class="background">
<svg viewBox="0 0 1376 764">
<defs>
<path id="shape" d="M1034.5,770.5a125.59,125.59,0,0,0-17-32c-12.32-16.72-25.68-25.84-33-31-6-4.23-59.88-42.55-100-90-13.64-16.14-20.57-24.48-26-38-15-37.48-3.73-73.65,0-85,8.68-26.45,23-43.26,35-57,19-21.82,33.56-29.9,67-54,33.68-24.27,55.39-39.91,77-60,40.56-37.69,35.94-49.35,81-96,34.18-35.39,51.27-53.08,79-65,7.79-3.35,76-31.44,140,2a142.06,142.06,0,0,1,31,22l7.5,7.5 L 1376,770.5 Z"/>
</defs>
<use xlink:href="#shape" id="bg"/>
<use xlink:href="#shape" id="fg"/>
</svg>
</div>
</div>
更新
要用图像填充背景,最简单的方法可能是使用剪辑路径。
#fg {
fill: pink;
stroke: #fff;
stroke-width: 10px;
stroke-dasharray: 1028px;
-webkit-animation: dash 2s;
animation: dash 2s;
}
@keyframes dash {
from { stroke-dashoffset: 1028px; }
to { stroke-dashoffset: 0px; }
}
@-webkit-keyframes dash {
from { stroke-dashoffset: 1028px; }
to { stroke-dashoffset: 0px; }
}
#bg {
fill: url(#kitten);
stroke: none;
transform-origin: 1270px 550px;
-webkit-animation: bgfill 2s linear 2s forwards;
animation: bgfill 2s linear 2s forwards;
}
@keyframes bgfill {
from { transform: scale(1); }
to { transform: scale(4); }
}
@-webkit-keyframes bgfill {
from { transform: scale(1); }
to { transform: scale(4); }
}
<div id="home" class="section" style="height:100vh;background:pink;">
<div class="background">
<svg viewBox="0 0 1376 764">
<defs>
<path id="shape" d="M1034.5,770.5a125.59,125.59,0,0,0-17-32c-12.32-16.72-25.68-25.84-33-31-6-4.23-59.88-42.55-100-90-13.64-16.14-20.57-24.48-26-38-15-37.48-3.73-73.65,0-85,8.68-26.45,23-43.26,35-57,19-21.82,33.56-29.9,67-54,33.68-24.27,55.39-39.91,77-60,40.56-37.69,35.94-49.35,81-96,34.18-35.39,51.27-53.08,79-65,7.79-3.35,76-31.44,140,2a142.06,142.06,0,0,1,31,22l7.5,7.5 L 1376,770.5 Z"/>
<clipPath id="kitten">
<use xlink:href="#shape" id="bg"/>
</clipPath>
</defs>
<image xlink:href="https://placekitten.com/700/350" width="1376" height="764" preserveAspectRatio="xMidYMid slice" clip-path="url(#kitten)"/>
<use xlink:href="#shape" id="fg"/>
</svg>
</div>
</div>