【问题标题】:merge pure CSS 3-dimensional spheres in svg code在 svg 代码中合并纯 CSS 3 维球体
【发布时间】:2022-01-11 18:59:39
【问题描述】:

1- 第一个代码是我添加到旋转功能的 svg 圆(我从另一个 Q 的 stackoverflow 了解动画、关键帧)。并将其塑造成 html 正文。

<html>
<body>
<svg width="100" height="100">
<defs>   
<style id="style20"> #test {animation: wheel 4s infinite linear;transform-origin: center;transform-box: fill-box;} @keyframes wheel { from { transform: rotatex(360deg); } to { transform: rotatey(0deg);}}</style>
</defs>

<circle id="test" cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="black" />
</svg> 
 
</body>
</html>

2- 下一个代码来自“如何创建纯 CSS 3 维球体?”主题下的另一个问题的答案。 [https://stackoverflow.com/questions/45238194/how-can-i-create-pure-css-3-dimensional-spheres][1]

.ball {
  position: absolute;
  top:0px;
  left:0px;
  width: 98vmin;
  height: 98vmin;
  margin: 1vmin;  
  transform-style: preserve-3d;  
  transform: rotateX(-5deg);
}

@keyframes rot{
  0% { transform: rotateY(0deg) rotateX(0deg) rotateZ(0deg); }
  100% { transform: rotateY(360deg) rotateX(0deg) rotateZ(0deg); }
}

.layer {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 98vmin;
  height: 98vmin;
}

.moving
{
  transform-style: preserve-3d;
  transform-origin: 49vmin 49vmin;
  animation: rot 10s linear infinite;
}






.clip
{
  border-radius: 50%;  
  overflow:hidden;
  transform: translateZ(-0vmin);
}

     
 @keyframes highlightanim {     
  0.00% {left: -150.00%; top: -178.00% }
  12.50% {left: -117.67%; top: -179.64% }
  25.00% {left: -97.69%; top: -195.87% }
  28.75% {left: -95.00%; top: -207.09% }
  32.50% {left: -97.69%; top: -220.70% }
  40.00% {left: -117.67%; top: -240.01% }
  47.50% {left: -150.00%; top: -247.50% }
  55.00% {left: -182.33%; top: -240.01% }
  62.50% {left: -202.31%; top: -220.70% }
  68.75% {left: -205.00%; top: -207.09% }
  75.00% {left: -202.31%; top: -195.87% }
  87.50% {left: -182.33%; top: -179.64% }
  100.00% {left: -150.00%; top: -178.00% }
} 
  
.shade
{
  position: relative;
  top: -150%;
  left: -150%;
  width: 400%;
  height: 400%;
  background: radial-gradient(at 50% 50%, white, black, grey, black, black);
  animation: highlightanim 10s linear infinite;
}
<div class="ball">
  <div class='layer moving'>
     
    <div class='layer gridplane laser'></div>  
    <div class='layer gridplane laser2'></div>  
  </div> 
  <div class='layer clip'>
    <div class='shade'> 
    </div>
  </div>
</div>

3-如何将第二个代码与第一个代码放在同一个模型中(在html的主体内,分为函数和受函数影响的路径?

4- 我尝试在代码中编辑 x、y 和 z 的值,使其看起来像围绕 X 轴旋转(从页面下方到页面上方,方向与第一个代码相同),但没有任何效果.有没有办法做到这一点?

5-颜色,我尝试改变值

感谢提前

【问题讨论】:

  • 我不确定我是否理解您的问题。您想要第二个 (HTML) 示例的 SVG 版本吗?
  • 是第二个代码“3-d spheres”中的svg版本

标签: html css svg


【解决方案1】:

HTML 的 SVG 等价物如下:

.shade
{
  animation: highlightanim 10s linear infinite;
}

@keyframes highlightanim {     
  0.00% {x: -150.00; y: -178.00 }
  12.50% {x: -117.67; y: -179.64 }
  25.00% {x: -97.69; y: -195.87 }
  28.75% {x: -95.00; y: -207.09 }
  32.50% {x: -97.69; y: -220.70 }
  40.00% {x: -117.67; y: -240.01 }
  47.50% {x: -150.00; y: -247.50 }
  55.00% {x: -182.33; y: -240.01 }
  62.50% {x: -202.31; y: -220.70 }
  68.75% {x: -205.00; y: -207.09 }
  75.00% {x: -202.31; y: -195.87 }
  87.50% {x: -182.33; y: -179.64 }
  100.00% {x: -150.00; y: -178.00 }
} 
  
<svg viewBox="0 0 100 100" width="500">
  <defs>
    <radialGradient id="shade-gradient"
      cx="50%" cy="50%" r="70.7%">
      <stop offset="0%" stop-color="white"/>
      <stop offset="25%" stop-color="black"/>
      <stop offset="50%" stop-color="grey"/>
      <stop offset="75%" stop-color="black"/>
      <stop offset="100%" stop-color="black"/>
    </radialGradient>
    
    <clipPath id="layer-clip">
      <circle cx="50" cy="50" r="50"/>
    </clipPath>
  </defs>
  
  <rect class="shade" x="0" y="0" width="400" height="400"
        fill="url(#shade-gradient)" clip-path="url(#layer-clip)"/>
</svg>

请求:从下到上轮换

最简单的方法是将 SVG 旋转 90 度。

不过我觉得效果不是很好。

svg
{
  transform-origin: center;
  transform-box: fill-box;
  transform: rotate(-90deg);
}

.shade
{
  animation: highlightanim 10s linear infinite;
}

@keyframes highlightanim {     
  0.00% {x: -150.00; y: -178.00 }
  12.50% {x: -117.67; y: -179.64 }
  25.00% {x: -97.69; y: -195.87 }
  28.75% {x: -95.00; y: -207.09 }
  32.50% {x: -97.69; y: -220.70 }
  40.00% {x: -117.67; y: -240.01 }
  47.50% {x: -150.00; y: -247.50 }
  55.00% {x: -182.33; y: -240.01 }
  62.50% {x: -202.31; y: -220.70 }
  68.75% {x: -205.00; y: -207.09 }
  75.00% {x: -202.31; y: -195.87 }
  87.50% {x: -182.33; y: -179.64 }
  100.00% {x: -150.00; y: -178.00 }
} 
  
<svg viewBox="0 0 100 100" width="500">
  <defs>
    <radialGradient id="shade-gradient"
      cx="50%" cy="50%" r="70.7%">
      <stop offset="0%" stop-color="white"/>
      <stop offset="25%" stop-color="black"/>
      <stop offset="50%" stop-color="grey"/>
      <stop offset="75%" stop-color="black"/>
      <stop offset="100%" stop-color="black"/>
    </radialGradient>
    
    <clipPath id="layer-clip">
      <circle cx="50" cy="50" r="50"/>
    </clipPath>
  </defs>
  
  <rect class="shade" x="0" y="0" width="400" height="400"
        fill="url(#shade-gradient)" clip-path="url(#layer-clip)"/>
</svg>

请求:更改颜色

只需更改渐变中的颜色即可。

svg
{
  transform-origin: center;
  transform-box: fill-box;
  transform: rotate(-90deg);
}

.shade
{
  animation: highlightanim 10s linear infinite;
}

@keyframes highlightanim {     
  0.00% {x: -150.00; y: -178.00 }
  12.50% {x: -117.67; y: -179.64 }
  25.00% {x: -97.69; y: -195.87 }
  28.75% {x: -95.00; y: -207.09 }
  32.50% {x: -97.69; y: -220.70 }
  40.00% {x: -117.67; y: -240.01 }
  47.50% {x: -150.00; y: -247.50 }
  55.00% {x: -182.33; y: -240.01 }
  62.50% {x: -202.31; y: -220.70 }
  68.75% {x: -205.00; y: -207.09 }
  75.00% {x: -202.31; y: -195.87 }
  87.50% {x: -182.33; y: -179.64 }
  100.00% {x: -150.00; y: -178.00 }
} 
  
<svg viewBox="0 0 100 100" width="500">
  <defs>
    <radialGradient id="shade-gradient"
      cx="50%" cy="50%" r="70.7%">
      <stop offset="0%" stop-color="white"/>
      <stop offset="25%" stop-color="#ff0000"/>
      <stop offset="50%" stop-color="#ff8080"/>
      <stop offset="75%" stop-color="#ff0000"/>
      <stop offset="100%" stop-color="#ff0000"/>
    </radialGradient>
    
    <clipPath id="layer-clip">
      <circle cx="50" cy="50" r="50"/>
    </clipPath>
  </defs>
  
  <rect class="shade" x="0" y="0" width="400" height="400"
        fill="url(#shade-gradient)" clip-path="url(#layer-clip)"/>
</svg>

向后兼容的版本

在上面的示例中,我依赖于能够使用 CSS 设置几何属性的样式。在这种情况下 xy 在动画中。

样式化几何属性是一个相对较新的东西,并非所有浏览器都支持。所以我在下面添加了一个更向后兼容的版本。

svg
{
  transform-origin: center;
  transform-box: fill-box;
  transform: rotate(-90deg);
}

.shade
{
  animation: highlightanim 10s linear infinite;
}

@keyframes highlightanim {     
  0.00% { transform: translate(-150.00px, -178.00px); }
  12.50% { transform: translate(-117.67px, -179.64px); }
  25.00% { transform: translate(-97.69px, -195.87px); }
  28.75% { transform: translate(-95.00px, -207.09px); }
  32.50% { transform: translate(-97.69px, -220.70px); }
  40.00% { transform: translate(-117.67px, -240.01px); }
  47.50% { transform: translate(-150.00px, -247.50px); }
  55.00% { transform: translate(-182.33px, -240.01px); }
  62.50% { transform: translate(-202.31px, -220.70px); }
  68.75% { transform: translate(-205.00px, -207.09px); }
  75.00% { transform: translate(-202.31px, -195.87px); }
  87.50% { transform: translate(-182.33px, -179.64px); }
  100.00% { transform: translate(-150.00px, -178.00px); }
}
<svg viewBox="0 0 100 100" width="500">
  <defs>
    <radialGradient id="shade-gradient"
      cx="50%" cy="50%" r="70.7%">
      <stop offset="0%" stop-color="white"/>
      <stop offset="25%" stop-color="#ff0000"/>
      <stop offset="50%" stop-color="#ff8080"/>
      <stop offset="75%" stop-color="#ff0000"/>
      <stop offset="100%" stop-color="#ff0000"/>
    </radialGradient>
    
    <clipPath id="layer-clip">
      <circle cx="50" cy="50" r="50"/>
    </clipPath>
  </defs>
  
  <g clip-path="url(#layer-clip)">
    <rect class="shade" x="0" y="0" width="400" height="400"
          fill="url(#shade-gradient)"/>
  </g>
</svg>

【讨论】:

  • 非常感谢您。真的很适合你的努力。但是当使用“运行代码 sn-p”查看结果时,它只给出没有动画的形状(没有出现移动,只是固定图像)。有什么办法可以让模糊的白色从一边到另一边无限运动,所以看起来像 3D 球运动?
  • 它在 Chrome 中对我来说很好用。你用的是哪个浏览器?
  • 火狐。当点击“运行代码sn-p”时,它会显示圆圈和白色部分(当改变他的位置时会产生移动效果),但白色部分保持不动而没有任何移动。我也尝试在w3schools.com/graphics/tryit.asp?filename=trysvg_myfirst 上运行代码,但没有成功。我将安装 chrome 并尝试使用它。
  • 好的。道歉。我在我的答案中添加了一个新版本,适用于 Firefox。
  • 它工作,非常感谢。
猜你喜欢
  • 2017-12-27
  • 2012-05-07
  • 1970-01-01
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 2019-02-08
  • 1970-01-01
相关资源
最近更新 更多