Web*_*man 5 html javascript css svg reactjs
这是我的React组件演示:https : //codesandbox.io/s/epic-brown-osiq1,我现在使用viewBox的值,getBBox并getBoundingClientRect()实现一些计算以定位元素。目前,我已根据控制台从getBoundingClientRect()日志中提供给我的收益输入了原始值。你可以看到,我已经实现了的元素getBoundingClientRect()上,即<svg>的根元素和clip-path的text的元素。更好,但text更多的地方是将屏幕的中心真正对准text框的中心,您可以看到“ So”一词位于“ Food”一词的开头,而不是对准的box的中心。所以我目前在这一点上。
注意:您会在沙箱中看到一些评论,这些评论提供了我的部分信息或以前的试用版。
我的代码是什么?具体来说,我显示的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直接在SVGCSS选择器上使用-仍然具有近似结果。我想知道我缺少什么。也许有人可以对我的代码的行为做出一些解释?
这是我第二次演示的结果代码(大约是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);
}
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
感谢您的任何提示。
SVG中的文本对齐方式无法像从HTML和CSS那样习惯,在HTML和CSS中,所有内容都是具有一定尺寸的框,我们可以应用例如text-align: center。
在<svg:text>起始坐标中定义文本行将从其开始扩展的点。
text-anchor属性控制着展开的方向:centervalue表示它将双向展开,因此初始锚点将位于边界框宽度的中间(对于水平书写系统)。请参阅出色的答案以说明这text-anchor是将文本居中SVG的最佳方法:https : //stackoverflow.com/a/23272367/540955。而且,SVG内没有CSS位置属性(左/上),只有x / y坐标,也没有空白和余量,就像您在HTML中所知道的那样。
因此,在代码中添加坐标并向右text-anchor="middle"移动x将产生居中文本。我建议使用裸<text>元素而不是<tspan>s,因为用dx/ dy相对于最后一个前一个字符移动它们,并且该字符可能与父级之间有一些空格<text>(取决于代码格式),这会导致不平衡的居中。对于更简单的计算(或仅用于水平书写系统),它也很有用,因为它将锚点从基线移到“中心线”。因此,使用属性(如您已经做的那样)将行高的第一行“一半”上移,另一行下移应该可以解决问题:dominant-baseline="central"middledy
<svg viewBox="0 0 800 200" text-anchor="middle" dominant-baseline="central" font-size="100">
<!-- Outline and diagonals with center point for illustration: -->
<path d="M0,0h800v200h-800zl800,200m0 -200L0,200" fill="#FC9" stroke-width="1" stroke="rgba(0,0,0,0.3)"></path>
<circle cx="50%" cy="50%" r="10" fill="red"></circle>
<!-- Centered text: -->
<text x="50%" y="50%" fill="rgba(0,0,0,0.3)">So</text>
<!-- Shifted up and down: -->
<text x="50%" y="50%" dy="-0.5em">So</text>
<text x="50%" y="50%" dy="+0.5em">Food</text>
</svg>Run Code Online (Sandbox Code Playgroud)
(不完全相关:仅可以使用CSS进行裁剪background-clip: text;这是您的设计在Chrome浏览器中的粗略变化,它带有动画文本背景,但没有阴影。不幸的是,添加阴影将需要更多的元素或属性,我认为(在任何支持background-clip的浏览器中都可以使用。)
div {
display: flex;
flex-direction: column;
align-items: center;
font-size: 30vh;
line-height: 30vh;
font-weight: bold;
font-family: Impact;
}
span {
color: #fff;
background-color: #000;
width: 100%;
text-align: center;
}
@supports (-webkit-text-fill-color: transparent) and (-webkit-background-clip: text) {
span {
-webkit-text-fill-color: transparent;
-webkit-background-clip: text;
animation: 2s wohoo infinite alternate cubic-bezier(1,0,1,1);
background-position: 0 0;
background-size: 100% 100%;
background-color: transparent;
background-image: linear-gradient(
to right,
#f2385a 0,
#f2385a 20%,
#f5a503 0,
#f5a503 40%,
#e9f1df 0,
#e9f1df 60%,
#56d9cd 0,
#56d9cd 80%,
#3aa1bf 0,
#3aa1bf 100%
);
background-repeat: no-repeat;
transform-origin: center center;
}
}
@keyframes wohoo {
from {
background-size: 0 100%;
background-position: -5vh 0;
transform: scale(0.7);
}
50% {
transform: scale(0.7);
}
90% {
transform: scale(0.9);
}
to {
background-size: 500% 100%;
background-position: 0vh 0;
transform: scale(0.9)
}
}
html,body{margin:0;overflow:hidden;}
body {
background-color: #1d1f20;
color: snow;
display: flex;
flex-direction: row;
justify-content: center;
width: 100%;
}Run Code Online (Sandbox Code Playgroud)
<div>
<span>Baz</span>
<span>Gazonk</span>
<span>Qux</span>
</div>Run Code Online (Sandbox Code Playgroud)