【发布时间】:2020-03-10 18:46:59
【问题描述】:
这里是我的 React 组件演示:https://codesandbox.io/s/epic-brown-osiq1,我现在使用 viewBox 的值,getBBox 和 getBoundingClientRect() 来实现一些计算以定位我的元素。目前,我已经根据控制台从getBoundingClientRect() 的日志中为我提供的返回输入了原始值。你可以在我实现了getBoundingClientRect()的元素上看到它,即<svg>的根元素和clip-path的text的元素。更好,但text 更位于屏幕中心,真正对齐text 框的中心 - 你可以看到“So”的单词在“Food”的开头word而不是在box的中心对齐。所以我目前处于这一点。感谢您的反馈。*
注意:您会在沙箱中看到一些 cmet 提供信息或我以前的试验的部分内容。
我的代码有什么作用?具体来说,我显示了一个clip-path 的文本,其中一些动画面板在clip-path 上移动 - 这是color_panel_group's element - 为构图提供了一些动态。text 后面还有一个阴影,以赋予一些深度作文。
-
期望:显示一个
clip-path的text响应式定位在视口的垂直和水平中心。 -
问题:我的
clip-path隐藏了text的一部分,并且我尝试将元素相对于视口居中,但没有取得成果。 -
我尝试过的: 我尝试过使用元素的宽度和
x元素的位置 - 主要是text、clip-path、symbol和两种情况.甚至尝试通过在其中实现一些类来使用use元素,但最终得到了非常近似的结果。同样在tspan和symbol中,我尝试使用x的属性,再次获得非常近似的结果。我尝试过使用position absolute和relative container- 主要是直接在SVG的CSS 选择器上使用 - 仍然有近似的结果。
我想知道我错过了什么。也许有人可以对我的代码行为进行一些解释?
这是我第二次演示的结果代码(大约是 React 组件生成的):
body {
background: orange;
}
svg {
background: green;
display: block;
margin: auto;
position: relative;
z-index: 2;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
}
.component {
min-width: 100vw;
min-height: 100vh;
font-size: 100%;
}
.fade_in_background {
opacity: 1;
visibility: visible;
transition: all 1.5s ease-out 0s;
}
.brandtype {
margin: auto;
text-align: center;
}
.brandtype_use {
position: absolute;
transform: translate(-112.65px, 0)
}
.clipPath_text {
text-align: center;
}
.color_panel_group {
padding: 25px;
}
.shape_animation {
transform-origin: 0;
transform: scale(0, 1) translate(0, 0);
animation: moving-panel 3s 1.5s 1 alternate forwards;
}
.shadow {
transform: translate(10px, 10px)
}
.shape_animation_shadow {
fill: black;
fill-opacity: .505;
transition: all 1.3s ease-out 0.3s;
}
.brandtype {
font-size: 6.8em;
}
@keyframes moving-panel {
to {
transform: scale(1, 1) translate(20px, 0);
}
}
<div class="component">
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 965 657">
<defs>
<symbol id="panel_animation" y="0">
<clipPath class="clipPath_text" id="clipPath_text"><text class="brandtype" word-spacing="-.45em">
<tspan x="0%" y="50%" dy="1.6em">So</tspan>
<tspan x="0%" y="50%" dy="3em">Food</tspan>
</text></clipPath>
<g class="shadow" clip-path="url(#clipPath_text)">
<rect class="shape_animation shape_animation_shadow" width="100%" height="100%" x="-25px">
</rect>
</g>
<g class="color_panel_group" clip-path="url(#clipPath_text)">
<rect class="shape_animation" fill="#F2385A" width="100%" height="100%"></rect>
<rect class="shape_animation" fill="#F5A503" width="80%" height="100%"></rect>
<rect class="shape_animation" fill="#E9F1DF" width="60%" height="100%"></rect>
<rect class="shape_animation" fill="#56D9CD" width="40%" height="100%"></rect>
<rect id="shape_animation_ref" class="shape_animation" fill="#3AA1BF" width="20%" height="100%" x="-25px">
</rect>
</g>
</symbol>
</defs>
<rect width="100%" height="100%" filter="url(#background_light)"></rect>
<use width="500px" height="100%" x="50%" xlink:href="#panel_animation" class="brandtype_use"></use>
</svg>
</div>
感谢任何提示。
【问题讨论】:
-
您的 svg 需要以用户单位而不是百分比设置的 viewBox 属性。还要在用户单位中设置其他所有内容
-
感谢您的回答,您介意解释一下您的建议背后的理由吗?此外,如果我在用户单元中设置所有内容,我会假设我会失去可能响应的好处?
-
viewBox中不允许使用百分比值。它们必须是无单位的数字。 -
感谢您的回答,现在我的剪辑路径和居中问题仍然存在,也许您知道为什么会出现这种情况?
-
所以基本上你的主要问题是水平居中两行 SVG 文本,对吧?剪辑、动画、过滤器和 React 部分现在不重要吗?
标签: javascript html css reactjs svg