tl;dr: I would like to create an actual 3d sphere with CSS - not just an illusion
我想用CSS创建一个真正的3d球体,而不仅仅是一个幻觉
Note: some of the snippet examples are not responsive. Please use full screen.
注意:有些片段示例没有响应。请使用全屏。
With pure CSS you can create and animate a 3d cube like so:
使用纯CSS,你可以创建和动画一个3d立方体如下:
#cube-wrapper {
position: absolute;
left: 50%;
top: 50%;
perspective: 1500px;
}
.cube {
position: relative;
transform-style: preserve-3d;
animation-name: rotate;
animation-duration: 30s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
@keyframes rotate {
0% {
transform: rotate3d(0, 0, 0, 0);
}
100% {
transform: rotate3d(0, 1, 0, 360deg);
;
}
}
.face {
position: absolute;
width: 200px;
height: 200px;
border: solid green 3px;
}
#front_face {
transform: translateX(-100px) translateY(-100px) translateZ(100px);
background: rgba(255, 0, 0, 0.5);
}
#back_face {
transform: translateX(-100px) translateY(-100px) translateZ(-100px);
background: rgba(255, 0, 255, 0.5);
}
#right_face {
transform: translateY(-100px) rotateY(90deg);
background: rgba(255, 255, 0, 0.5);
}
#left_face {
transform: translateY(-100px) translateX(-200px) rotateY(90deg);
background: rgba(0, 255, 0, 0.5);
}
#top_face {
transform: translateX(-100px) translateY(-200px) rotateX(90deg);
background: rgba(0, 255, 255, 0.5);
}
#bottom_face {
transform: translateX(-100px) rotateX(90deg);
background: rgba(255, 255, 255, 0.5);
}
.cube {
transform: rotateX(90deg) rotateY(90deg);
}
<div id="cube-wrapper">
<div class="cube">
<div id="front_face" class="face"></div>
<div id="right_face" class="face"></div>
<div id="back_face" class="face"></div>
<div id="left_face" class="face"></div>
<div id="top_face" class="face"></div>
<div id="bottom_face" class="face"></div>
</div>
</div>
I want to create and animate a 3d sphere in the same manner.
我想以同样的方式创建并动画一个3d球体。
So... the first idea I get is to use border-radius
and...well... it doesn't work.
所以…我的第一个想法是使用边界半径,然后……它不工作。
#cube-wrapper {
position: absolute;
left: 50%;
top: 50%;
perspective: 1500px;
}
.cube {
position: relative;
transform-style: preserve-3d;
animation-name: rotate;
animation-duration: 30s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
@keyframes rotate {
0% {
transform: rotate3d(0, 0, 0, 0);
}
100% {
transform: rotate3d(0, 1, 0, 360deg);
;
}
}
.face {
position: absolute;
width: 200px;
height: 200px;
border: solid green 3px;
border-radius: 100vw
}
#front_face {
transform: translateX(-100px) translateY(-100px) translateZ(100px);
background: rgba(255, 0, 0, 0.5);
}
#back_face {
transform: translateX(-100px) translateY(-100px) translateZ(-100px);
background: rgba(255, 0, 255, 0.5);
}
#right_face {
transform: translateY(-100px) rotateY(90deg);
background: rgba(255, 255, 0, 0.5);
}
#left_face {
transform: translateY(-100px) translateX(-200px) rotateY(90deg);
background: rgba(0, 255, 0, 0.5);
}
#top_face {
transform: translateX(-100px) translateY(-200px) rotateX(90deg);
background: rgba(0, 255, 255, 0.5);
}
#bottom_face {
transform: translateX(-100px) rotateX(90deg);
background: rgba(255, 255, 255, 0.5);
}
.cube {
transform: rotateX(90deg) rotateY(90deg);
}
<div id="cube-wrapper">
<div class="cube">
<div id="front_face" class="face"></div>
<div id="right_face" class="face"></div>
<div id="back_face" class="face"></div>
<div id="left_face" class="face"></div>
<div id="top_face" class="face"></div>
<div id="bottom_face" class="face"></div>
</div>
</div>
So, I reconsidered my approach and looked for a different method.
所以,我重新考虑了我的方法,并寻找了一种不同的方法。
I looked at:
我看了看:
- Implementing "Pure CSS Sphere" into website - how do I do it?
- 将“纯CSS领域”实现到网站中——如何实现?
- How to create a sphere in css?
- 如何在css中创建一个球体?
- Display an image wrapped around a sphere with CSS/Javascript
- 显示一个用CSS/Javascript包围的球体的图像。
- cssanimation.rocks - CSS spheres
- cssanimation。岩石- CSS球体
- MDN - Circle
- MDN,圆
Then I tried again...the best I got were overly complicated 3d object illusions.
然后我又试了一次…我得到的最好的是过于复杂的3d对象错觉。
Like this:
是这样的:
body {
overflow: hidden;
background: #333;
}
.wrapper {
margin: 1em;
animation-duration: 20s;
}
.planet,
.planet:before,
.planet:after {
height: 300px;
width: 300px;
border-radius: 100vw;
will-change: transform;
margin: 0 auto;
}
.planet {
box-shadow: inset 0px 0px 10px 10px rgba(0, 0, 0, 0.4);
position: relative;
}
.wrapper,
.planet,
.planet:before {
animation-name: myrotate;
animation-duration: 20s;
}
.wrapper,
.planet,
.planet:before,
.planet:after {
animation-timing-function: linear;
animation-iteration-count: infinite;
}
.planet:before,
.planet:after {
content: '';
position: absolute;
top: 0;
left: 0;
}
.planet:before {
box-shadow: inset 20px 20px 100px 00px rgba(0, 0, 0, .5), 0px 0px 5px 3px rgba(0, 0, 0, .1);
}
.planet:after {
filter: saturate(2.5);
background: linear-gradient(rgba(0, 0, 0, 1), transparent), url("https://i.stack.imgur.com/eDYPN.jpg");
opacity: 0.3;
box-shadow: inset -20px -20px 14px 2px rgba(0, 0, 0, .2);
animation-name: myopacity;
animation-duration: 5000000s;
}
@keyframes myrotate {
0% {
transform: rotatez(0deg);
}
100% {
transform: rotatez(360deg);
}
}
@keyframes myopacity {
0% {
background-position: 0px;
transform: rotatez(0deg);
}
50% {
background-position: 100000000px;
}
100% {
background-position: 0;
transform: rotatez(-360deg);
}
}
<div class="wrapper">
<div class="planet"></div>
</div>
And this:
这:
body {
background: #131418;
}
.wrapper {
margin: 1em;
max-width: 100%;
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.planet,
.planet:before,
.planet:after {
height: 500px;
width: 500px;
max-height: 30vw;
max-width: 30vw;
border-radius: 100vw;
will-change: transform;
}
.planet {
box-shadow: inset 0px 0px 100px 10px rgba(0, 0, 0, .5);
position: relative;
float: left;
margin: 0 2em;
}
.planet,
.planet:before,
.planet:after {
animation-name: myrotate;
animation-duration: 10s;
}
.wrapper,
.planet,
.planet:before,
.planet:after {
animation-timing-function: linear;
animation-iteration-count: infinite;
}
.planet:before,
.planet:after {
content: '';
position: absolute;
top: 0;
left: 0;
}
.planet:before {
box-shadow: inset 50px 100px 50px 0 rgba(0, 0, 0, .5), 0 0 50px 3px rgba(0, 0, 0, .25);
background-image: -webkit-radial-gradient( top, circle cover, #ffffff 0%, #000000 80%);
opacity: .5;
}
.planet:after {
opacity: .3;
background-image: -webkit-radial-gradient( bottom, circle, #ffffff 0%, #000000 -200%);
box-shadow: inset 0px 0px 100px 50px rgba(0, 0, 0, .5);
}
@keyframes myrotate {
0% {
transform: rotatez(0deg);
}
100% {
transform: rotatez(-360deg);
}
}
.bg {
background: wheat;
}
<div class="wrapper">
<div class="planet bg"></div>
</div>
Which are okay until you try to actually rotate them on either the x-axis or the y-axis like the cube in my first example...here's what happens then: (simplified example)
这是可以的,直到你试着在x轴或y轴上旋转它们就像我第一个例子中的立方体一样…接下来发生的事情是:(简化的例子)
.sphere {
background: black;
width: 300px;
height: 300px;
border-radius: 100vw;
animation: myrotate 10s linear infinite
}
@keyframes myrotate {
0% {
transform: rotate3d(0, 0, 0, 0);
}
100% {
transform: rotate3d(0, 1, 0, 360deg);
}
}
<div class="sphere"></div>
All you get is a flat 2d object - which is expeceted considering that it's what the element is
你得到的只是一个平面的2d对象——考虑到元素是什么,这是可以预料到的
The closest thing I found is the following shape created in a tutorial by Timo Korinth
我发现的最接近的东西是Timo Korinth在教程中创建的以下图形
@-webkit-keyframes animateWorld {
0% {
-webkit-transform: rotateY(0deg) rotateX(0deg) rotateZ(0deg);
}
50% {
-webkit-transform: rotateY(360deg) rotateX(180deg) rotateZ(180deg);
}
100% {
-webkit-transform: rotateY(720deg) rotateX(360deg) rotateZ(360deg);
}
}
html {
background: #FFFFFF;
}
. world {
-webkit-perspective: 1000px;
}
.cube {
margin-left: auto;
margin-right: auto;
position: relative;
width: 200px;
height: 200px;
-webkit-transform-style: preserve-3d;
-webkit-animation-name: animateWorld;
-webkit-animation-duration: 10s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
}
.circle {
position: absolute;
width: 100%;
height: 100%;
border: 2px dashed #009BC2;
border-radius: 50%;
opacity: 0.8;
background: rgba(255, 255, 255, 0);
}
.zero {
-webkit-transform: rotateX(90deg);
}
.two {
-webkit-transform: rotateY(45deg);
}
.three {
-webkit-transform: rotateY(90deg);
}
.four {
-webkit-transform: rotateY(135deg);
}
.five {
width: 173px;
height: 173px;
margin: 14px;
-webkit-transform: rotateX(90deg) translateZ(50px);
}
.six {
width: 173px;
height: 173px;
margin: 14px;
-webkit-transform: rotateX(90deg) translateZ(-50px);
}
<div class="world">
<div class="cube">
<div class="circle zero"></div>
<div class="circle one"></div>
<div class="circle two"></div>
<div class="circle three"></div>
<div class="circle four"></div>
<div class="circle five"></div>
<div class="circle six"></div>
</div>
</div>
So here's my
这是我的
Question:
How do I create an actual 3 dimensional sphere with pure CSS? More specifically, one that is covered - not just a frame - and doesn't involve hundreds of html elements.
如何用纯CSS创建一个真正的三维球体?更具体地说,它不仅包含一个框架,而且不包含数百个html元素。
Notes:
注:
- Three dimensional spheres have height, width and depth - just like the cube in my first example snippet
- 三维球体具有高度、宽度和深度——就像我的第一个例子片段中的立方体一样
- I don't need any physics and there's no need for any user-interaction. Just an animated spinning sphere.
- 我不需要任何物理,也不需要任何用户交互。只是一个旋转的球体。
Additional resources:
额外的资源:
- paulrhayes.com - Spheres
- paulrhayes.com球体
3d(2d illusion) Earth with Rotating Animation with CSS- 3d (2d错觉)地球与旋转动画与CSS
- Interactive CSS sphere
- 交互式CSS球体
7 个解决方案
#1
69
Strictly speaking, any "3D" shape on a flat screen is more an illusion of a 3D object. All we see is a 2D projection of that shape on the screen plane, and our brain does its best to guess which shape could give the projection we see. If the projection changes, our brain interprets it as a 3D object changing its orientation, which helps it to determine the shape of this object better.
严格地说,平面屏幕上的任何“3D”形状都更像是3D对象的错觉。我们所看到的只是屏幕平面上那个形状的二维投影,我们的大脑会尽最大努力猜测哪个形状可以给我们看到的投影。如果投影变了,我们的大脑就会把它解释为3D物体改变了方向,这有助于它更好地确定物体的形状。
It works well with non-symmetric objects and objects made from polygons (e.g., cubes), but the sphere is a very special case: its projection on the plane always gives just a circle. The static sphere and the rotating one have the same projection, the same circle. Even in real life, if we look at a sphere with a uniform surface without any marks on it (e.g., a polished metal ball), it is hard to determine if it stands still or rotates. Our eyes need some hints, some details that are moving along the surface of the sphere according to its geometry. The more such details move the way you expect from points on the spherical surface to move, the clearer is the perception (well, illusion) of the rotating sphere.
它适用于非对称物体和多边形物体(例如立方体),但是球面是一个非常特殊的情况:它在平面上的投影总是只给出一个圆。静态球和旋转球有相同的投影,相同的圆。即使在现实生活中,如果我们观察一个表面没有任何痕迹的球体(如抛光的金属球),我们也很难确定它是静止还是旋转。我们的眼睛需要一些线索,一些细节,这些线索沿着球体表面根据它的几何形状移动。这样的细节越像你期望的那样从球面上的点移动,你就越能清楚地看到旋转球体的感觉(嗯,错觉)。
And here is the key to the problem in making a CSS scene that would give such perception: to make this illusion strong enough, we need many marks moving along the paths that lie in different planes. And the only way to get this in CSS is to have each mark as a separate CSS box (element or pseudo-element). If our sphere consists only of moving marks, we really need many of them to see it as a sphere — thus "hundreds of elements" in most demos you have seen.
这里有一个问题的关键,就是要制作一个CSS场景来给人这样的感觉:为了让这个错觉足够强大,我们需要很多标记沿着不同平面的路径移动。在CSS中实现这一点的唯一方法是将每个标记作为一个单独的CSS框(元素或伪元素)。如果我们的球体仅仅由移动的标记组成,我们确实需要其中的许多标记来将它视为一个球体——因此,在大多数演示中,我们都看到了“数百个元素”。
So if you want to make a sphere look realistic with a reasonably small number of elements, you would probably need to combine the effects that make an "illusion" of a static basic spherical shape (a circle with radial gradients, inner shadows etc.) with some elements that are relatively small (to make it less obvious that they are actually flat), oriented along the surface of the sphere with 3D transforms, and animated — basically the same way as the faces of the cube in your first demo.
所以如果你想让一个球体看起来相当少量的现实元素,您可能需要结合的影响做一个静态的“错觉”基本球形(一圈径向渐变,内心的阴影等)和一些相对较小的元素(不太明显的,他们实际上是平的),面向沿着球面与3 d变换,和动画——基本上一样的脸立方体在你第一次演示。
Below is my own attempt to put this approach into practice. I used 20 circular elements oriented roughly as the faces of the regular icosahedron (like white hexagons on the classic soccer ball). I grouped them into two groups each making one hemisphere for convenience (it was not necessary, but it made styling a bit simpler). The whole 3D scene consists of the sphere itself and the background frame (pseudo element) which crosses the sphere near its centre (a bit closer, to reduce "flickering" of the circles as they go from the near side to the far side and back) and always faces to the screen. So 24 elements in total (not literally "hundreds", at least:). To make the circles look more "bulging" (like spherical segments), I added two pseudo elements to each of them and elevated them slightly one above other. Works best in Chrome and Firefox 57+ (in Firefox 56- and iOS Safari there is still some "flickering" near the edges). If you hover the circle, you can see the scene without the background frame (and also without "flickering"). A slightly modified version is also available on Codepen.
下面是我自己将这一方法付诸实践的尝试。我使用了20个圆形元素,这些元素的方向大致相当于通常的二十面体的面(比如经典足球上的白色六边形)。我把它们分成两组,每组都是为了方便起见,每个组都做了一个半球(这是不必要的,但它使样式更简单一些)。整个3D场景由球体本身和背景框(伪元素)组成,背景框穿过圆心附近的球体(靠近一点,以减少圆圈从近端到远端和后端的“闪烁”),并且始终面向屏幕。所以总共有24个元素(不是字面上的“数百”,至少是:)。为了让圆看起来更“凸起”(比如球形部分),我在每个圆上添加了两个伪元素,并将它们稍微抬高一点。在Chrome和Firefox 57+中效果最好(在Firefox 56和iOS Safari中,边缘仍然有一些闪烁)。如果你悬停在圆圈上,你可以看到没有背景的场景(也没有闪烁)。在Codepen上也有一个稍微修改过的版本。
.scene {
perspective: 400vmin;
transform-style: preserve-3d;
position: absolute;
width: 80vmin;
height: 80vmin;
top: 10vmin;
left: 10vmin;
}
.sphere {
transform-style: preserve-3d;
position: absolute;
animation: rotate 20s infinite linear;
width: 100%;
height: 100%;
transform-origin: 50% 50%;
top: 0;
left: 0;
}
.scene::before {
content: '';
position: absolute;
width: 100%;
height: 100%;
top: 0%;
left: 0%;
background: radial-gradient(circle farthest-corner at 33% 33%, rgba(240, 240, 220, 0.85) 0%, rgba(30, 30, 40, 0.85) 80%), radial-gradient(circle farthest-corner at 45% 45%, rgba(0, 0, 0, 0) 50%, #000000 80%);
border-radius: 50%;
transform: translateZ(2vmin);
}
.scene:hover::before {
display: none;
}
.hemisphere {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transform-style: preserve-3d;
transform-origin: 50% 50%;
transform: rotateX(90deg);
}
.hemisphere:nth-child(2) {
transform: rotateX(-90deg);
}
.face {
position: absolute;
width: 40vmin;
height: 40vmin;
background: radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.1) 48%, #ff0000 49%, #ff0000 50%, rgba(0, 0, 0, 0) 51%);
transform-style: preserve-3d;
transform-origin: 50% 0;
top: 50%;
left: 20vmin;
}
.face::before, .face::after {
content: '';
position: absolute;
border-radius: 50%;
box-sizing: border-box;
}
.face::before {
width: 50%;
height: 50%;
top: 25%;
left: 25%;
border: 2px solid #333;
background: rgba(255, 255, 255, 0.3);
transform: translateZ(1.6vmin);
}
.face::after {
width: 20%;
height: 20%;
top: 40%;
left: 40%;
background: rgba(0, 0, 0, 0.2);
transform: translateZ(2.8vmin);
}
.face:nth-child(1) {
transform: translateZ(-41.6vmin) rotateZ(36deg) translateY(-6.8vmin) rotateX(143deg);
}
.face:nth-child(2) {
transform: translateZ(-41.6vmin) rotateZ(108deg) translateY(-6.8vmin) rotateX(143deg);
}
.face:nth-child(3) {
transform: translateZ(-41.6vmin) rotateZ(180deg) translateY(-6.8vmin) rotateX(143deg);
}
.face:nth-child(4) {
transform: translateZ(-41.6vmin) rotateZ(252deg) translateY(-6.8vmin) rotateX(143deg);
}
.face:nth-child(5) {
transform: translateZ(-41.6vmin) rotateZ(-36deg) translateY(-6.8vmin) rotateX(143deg);
}
.face:nth-child(6) {
transform: translateZ(-26.8vmin) rotateZ(36deg) translateY(-33.2vmin) rotateX(100deg);
}
.face:nth-child(7) {
transform: translateZ(-26.8vmin) rotateZ(108deg) translateY(-33.2vmin) rotateX(100deg);
}
.face:nth-child(8) {
transform: translateZ(-26.8vmin) rotateZ(180deg) translateY(-33.2vmin) rotateX(100deg);
}
.face:nth-child(9) {
transform: translateZ(-26.8vmin) rotateZ(252deg) translateY(-33.2vmin) rotateX(100deg);
}
.face:nth-child(10) {
transform: translateZ(-26.8vmin) rotateZ(-36deg) translateY(-33.2vmin) rotateX(100deg);
}
.face:nth-child(11) {
transform: translateZ(-26.8vmin) rotateZ(36deg) translateY(-33.2vmin) rotateX(100deg);
}
@keyframes rotate {
0% {
transform: rotateZ(25deg) rotateX(20deg) rotateY(0deg);
}
50% {
transform: rotateZ(-25deg) rotateX(-20deg) rotateY(180deg);
}
100% {
transform: rotateZ(25deg) rotateX(20deg) rotateY(360deg);
}
}
body {
background: #555;
overflow: hidden;
}
<div class="scene">
<div class="sphere">
<div class="hemisphere">
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
</div>
<div class="hemisphere">
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
</div>
</div>
</div>
#2
23
How do I create an actual 3 dimensional sphere with pure CSS?
如何用纯CSS创建一个真正的三维球体?
Well, as many have stated in answers and comments it is simply impossible to create a single 3D entity in a browser with html and css only at this point in time, but it is possible to create an illusion of a 3D Object. Below is my approach to solving the problem.
好吧,正如许多人在回答和评论中所指出的那样,仅在此时用html和css在浏览器中创建一个单一的3D实体是不可能的,但创建一个3D对象的错觉是可能的。下面是我解决问题的方法。
To give a human eye the ability to see a spherical object, points of reference are needed for the eye to follow. In my case it is lines that define the shape of the sphere. The lines are achieved by giving a border to 5 elements that are in the X-axis set and 5 elements that are in the Y-axis set. Only X/Y sets are given a border, because that alone provides enough reference to make the illusion of a sphere. Additional lines on the Z axis are simply clutter and are unnecessary. If all lines are turned off, the entire thing appears like a solid "perfect" sphere (Looks like a circle, but all parts of it are moving and are present on the 3D plane in the browser!).
为了使人眼能够看到球形物体,眼睛需要有参考点才能跟随。在我的例子中,是线条定义了球体的形状。这些线是通过给X轴集合中的5个元素和Y轴集合中的5个元素一个边框来实现的。只有X/Y集合有一个边框,因为这个边框本身就提供了足够的参考,可以产生球面的错觉。Z轴上的附加线只是杂乱的,没有必要。如果所有的线都被关闭,整个东西看起来就像一个固体的“完美”球体(看起来像一个圆,但是它的所有部分都在移动,并且在浏览器的3D平面上显示!)
What I have done:
- Created 15 html elements that each will represent a circle split in 3 sets of 5
The reason for all of these sets, is that when the entire contraption is rotated on x, y, z axis the backgrounds of each of the elements in x, y, z sets are filling the empty spaces for each other.
所有这些集合的原因是,当整个装置在x, y, z轴上旋转时,x, y, z集合中每个元素的背景都在为彼此填充空白。
- Each set of 5 elements is rotated on X,Y,Z axis respectively in 36 degree increments.
- 每组5个元素分别在X、Y、Z轴上旋转36度。
- All elements are rounded using
border-radius:50%;
- 所有元素都是用边界半径圆角:50%;
- Set background of the circle elements to a solid color
- 将圆形元素的背景设置为纯色
- Put the sets together, so they overlap
- 把这些集合放在一起,这样它们就重叠了
- Clipped minor gaps that came as a result of not enough elements covering the empty spaces between x,y,z circles using
clip-path: circle(96px at center);
on the container, and threw a cool shade/light effect on to seal the deal
vsMore circles will result in a less "edgy" sphere, but since the performance is stressed in the question, a quick clip of the entire thing seemed like the thing to do
更多的圆圈将导致一个不那么“尖锐”的球体,但由于表现是强调在问题中,一个快速剪辑整个事情似乎是做
As a closing thought, I wanted to express my appreciation of the question being asked, it really made me think and was a great project that lead to me learning a lot of things about 3D capabilities of html/css.
Also thanks to all the people who have spent the time to pry this open, and came up with awesome approaches to the problem.
也要感谢所有花时间来撬开这个门的人,并且想出了一些很棒的方法来解决这个问题。
I hope the fruits of my research are of use. Cheers!
我希望我的研究成果是有用的。干杯!
This Pen is also based on Timo Korinth's example.
这支笔也是基于Timo Korinth的例子。
* {
margin: 0;
padding: 0;
}
/* Rotate Sphere animation */
@-webkit-keyframes animateSphere {
0% {
transform: rotateY(0deg) rotateX(0deg) rotateZ(0deg);
}
50% {
transform: rotateY(360deg) rotateX(360deg) rotateZ(0deg);
}
100% {
transform: rotateY(720deg) rotateX(720deg) rotateZ(0deg);
}
}
html {
background: black;
}
.scene {
perspective: 1000px;
}
.container {
margin-top: 5vh;
margin-left: auto;
margin-right: auto;
position: relative;
width: 200px;
height: 200px;
transform-style: preserve-3d;
animation-name: animateSphere;
animation-duration: 30s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
.border {
border: 1px solid white;
}
.circle {
position: absolute;
width: 100%;
height: 100%;
border-radius: 50%;
background: rgba(204, 0, 102, 1);
}
.circle:nth-child(1) {
transform: rotate3d(1, 0, 0, 0deg);
}
.circle:nth-child(2) {
transform: rotate3d(1, 0, 0, 36deg);
}
.circle:nth-child(3) {
transform: rotate3d(1, 0, 0, 72deg);
}
.circle:nth-child(4) {
transform: rotate3d(1, 0, 0, 108deg);
}
.circle:nth-child(5) {
transform: rotate3d(1, 0, 0, 144deg);
}
/* 18! difference to align*/
.circle:nth-child(6) {
transform: rotate3d(0, 1, 0, 0deg);
}
.circle:nth-child(7) {
transform: rotate3d(0, 1, 0, 36deg);
}
/* Upper and Lower circle */
.circle:nth-child(8) {
transform: rotate3d(0, 1, 0, 72deg);
}
.circle:nth-child(9) {
transform: rotate3d(0, 1, 0, 108deg);
}
.circle:nth-child(10) {
transform: rotate3d(0, 1, 0, 144deg);
}
.circle:nth-child(11) {
transform: rotate3d(0, 1, 0, 90deg) rotate3d(1, 0, 0, 0deg);
}
.circle:nth-child(12) {
transform: rotate3d(0, 1, 0, 90deg) rotate3d(1, 0, 0, 36deg);
}
/* Upper and Lower circle */
.circle:nth-child(13) {
transform: rotate3d(0, 1, 0, 90deg) rotate3d(1, 0, 0, 72deg);
}
.circle:nth-child(14) {
transform: rotate3d(0, 1, 0, 90deg) rotate3d(1, 0, 0, 108deg);
}
.circle:nth-child(15) {
transform: rotate3d(0, 1, 0, 90deg) rotate3d(1, 0, 0, 144deg);
}
.shadow {
margin: auto;
border-radius: 50%;
width: 200px;
height: 200px;
box-shadow: 10px 1px 30px white;
}
/* Clip the sphere a bit*/
.clip {
clip-path: circle(96px at center);
}
<div class="scene">
<div class="shadow">
<div class="clip">
<div class="container">
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
</div>
</div>
</div>
</div>
#3
16
As already mentioned above, CSS3 can't provide you with a real 3d shapes but just with the illusion. A good sphere illusion that use minimum HTML elements and use image as a texture can be done by a combination of what you have done and masking all with CSS shadows.
如上所述,CSS3不能为您提供真正的3d图形,而只能提供幻象。使用最小的HTML元素和使用图像作为纹理的一个良好的球体错觉可以通过你所做的和用CSS阴影来掩盖所有的东西来完成。
A nice touch that can make the mask more realistic is the use of the :after
pseudo element for creation of an additional spark in shifted location and smaller size. A key thing for a successful effect is to remember that different materials reflect light differently. meaning if you're trying to create a metallic sphere the lighting created by the box-shadow
will be different from the lighting of a plastic sphere.
一个可以使蒙版更真实的好方法是使用:after pseudo元素在移动的位置和更小的尺寸中创建额外的火花。一个成功的效果的关键是要记住不同的材料反射光线的方式是不同的。也就是说,如果你想创造一个金属球盒阴影产生的光将不同于塑料球的光。
Another nice addition is the use of the :before
pseudo element for creation of reflection effect. Adding a premade image of the world on a sphere with some opacity can create a very persuasive effect. notice also with the reflection the material you are trying to create will determine the amount of opacity you want for the reflection.
另外一个很好的补充是使用:在pseudo元素创建反射效果之前。在球体上添加一些不透明的预先制作的世界图像可以产生非常有说服力的效果。同样要注意的是,通过反射,你要创建的材料将决定你想要反射的不透明度。
Notice, I used octagonal prism for having the image behind the scene to look more round when the 3d trasform apply its perspective. Even with using only 8 elements the result is quite realistic. More realistic result can be done with more polygons and more complex shapes and texture mappings but even then no need for too many elements due to the addition of the shadow and the spark above everything.
注意,我使用八角形的棱镜,在三维的trasform应用它的角度时,在场景后面的图像看起来更圆。即使只使用8个元素,结果也是相当真实的。更现实的结果可以用更多的多边形和更复杂的形状和纹理映射来实现,但即使这样,也不需要添加太多的元素,因为阴影和星火高于一切。
Last, in order to hide the rest of the octagonal prism and display only the parts inside the sphere bounds I'm using clip-path: circle(99px at center);
.
最后,为了隐藏其余的八角形棱柱,只显示球体边界内的部分,我使用了剪切路径:circle(中间99px);
body {
width: 100%;
height: 100%;
background-color: #000;
}
.cube-wrapper {
width: 0;
height: 0;
top: 100px;
left: 100px;
position: absolute;
perspective-origin: 0 0;
perspective: 80px;
}
.cube-2 {
transform: translateZ(-100px) scaleX(1.8);
transform-style: preserve-3d;
}
.cube {
top: -100px;
position: relative;
transform-style: preserve-3d;
animation-name: rotate;
animation-duration: 10s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
@keyframes rotate {
0% {
transform: rotate3d(0 0, 0, 360deg);
}
100% {
transform: rotate3d(0, 1, 0, 360deg);
;
}
}
.face {
position: absolute;
background-size: 662.4px 200px;
width: 84px;
height: 200px;
}
#face1 {
transform: translateX(-41.4px) translateZ(100px);
background-position: 0 0;
}
#face2 {
transform: translateX(29.2px) translateZ(70.8px) rotateY(45deg);
background-position: -82.8px 0;
}
#face3 {
transform: translateX(58.5px) rotateY(90deg);
background-position: -165.6px 0;
}
#face4 {
transform: translateX(29.2px) translateZ(-70.8px) rotateY(135deg);
background-position: -248.4px 0;
}
#face5 {
transform: translateX(-41.4px) translateZ(-100px) rotateY(180deg);
background-position: -331.2px 0;
}
#face6 {
transform: translateX(-111.4px) translateZ(-70.8px) rotateY(225deg);
background-position: -414px 0;
}
#face7 {
transform: translateX(-141.4px) rotateY(270deg);
background-position: -496.8px 0;
}
#face8 {
transform: translateX(-111.4px) translateZ(70px) rotateY(315deg);
background-position: -579.6px 0;
}
.circle {
position: absolute;
width: 200px;
height: 200px;
border-radius: 50%;
}
.clip-circle {
position: absolute;
padding: 0;
top: -16px;
width: 200px;
height: 200px;
border-radius: 50%;
clip-path: circle(99px at center);
}
.lighting:after {
content: '';
position: absolute;
top: 50px;
left: 67px;
}
.reflection:before {
content: '';
position: absolute;
top: 0px;
left: 0px;
height: 200px;
width: 200px;
background-image:url(https://i.stack.imgur.com/ayCw7.png);
background-size: 200px 200px;
}
.earth {
position: absolute;
left: 20px;
}
.earth .face{
background-image:url(https://i.stack.imgur.com/fdtNz.jpg);
}
.earth .clip-circle {
transform: rotateX(7deg) rotateZ(15deg);
}
.earth .lighting {
box-shadow: -20px -30px 55px 0 rgba(0, 0, 0, 0.9) inset, -75px -100px 150px 0 rgba(0, 0, 0, 0.4) inset, 75px 100px 200px 0 rgba(255, 255, 255, 0.2) inset, -1px -2px 10px 2px rgba(200, 190, 255, 0.2);
}
.earth .lighting:after {
box-shadow: 0 0 150px 51px rgba(255, 255, 255, 0.2), 0 0 26px 10px rgba(255, 255, 255, 0.2);
}
.wood {
position: absolute;
left: 240px;
}
.wood .face{
background-image:url(https://i.stack.imgur.com/sa5P8.jpg);
}
.wood .cube-wrapper {
transform: rotateZ(45deg);
}
.wood .lighting {
box-shadow: -20px -30px 90px 0 rgba(0, 0, 0, 0.7) inset, -75px -100px 140px 0 rgba(0, 0, 0, 0.6) inset;
}
.wood .lighting:after {
box-shadow: 0 0 42px 15px rgba(255, 255, 255, 0.5);
}
.wood .reflection:before {
opacity: 0.04;
}
.metal {
position: absolute;
left: 460px;
}
.metal .face{
background-image:url(https://i.stack.imgur.com/PGmVN.jpg);
}
.metal .cube-wrapper {
transform: rotateZ(-32deg);
}
.metal .lighting {
box-shadow: -20px -30px 100px 0 rgba(0, 0, 0, 0.9) inset, -75px -100px 107px 0 rgba(0, 0, 0, 0.3) inset, 75px 100px 127px 0 rgba(255, 255, 255, 0.23) inset;
}
.metal .lighting:after {
box-shadow: 0 0 42px 20px rgba(255, 255, 255, 0.7), 0 0 7px 6px rgba(255, 255, 255, 0.9);
}
.metal .reflection:before {
opacity: 0.2;
}
<body>
<div style="position:absolute;top:20px;">
<div class="earth">
<dir class="clip-circle">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</dir>
<div class="circle lighting"></div>
</div>
<div class="wood">
<dir class="clip-circle">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</dir>
<div class="circle reflection lighting"></div>
</div>
<div class="metal">
<dir class="clip-circle">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</dir>
<div class="circle reflection lighting"></div>
</div>
</div>
<div style="position:absolute;top:240px">
<div class="earth">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</div>
<div class="wood">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</div>
<div class="metal">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</div>
</div>
<div style="position:absolute;top:460px;">
<div class="earth">
<div class="circle lighting"></div>
</div>
<div class="wood">
<div class="circle reflection lighting"></div>
</div>
<div class="metal">
<div class="circle reflection lighting"></div>
</div>
</div>
</body>
#4
14
Making a realistic 3d css sphere, without some level of 2d illusion, would require many elements for it to have a smooth perimeter.
制作一个真实的3d css球体,不需要一定程度的2d错觉,需要很多元素使它有一个光滑的周长。
However, I made a version of Timo Korinth's example with:
然而,我用Timo Korinth的例子做了一个版本:
- Clipping of the "back facing" grid lines
- 裁剪“背面”网格线
- Approximate spherical shading by moving a radial gradient
- 通过移动径向梯度近似球形阴影
Can be rotated arbitrarily as long as the shading animation is recalculated.
可以任意旋转,只要阴影动画被重新计算。
This page has some of the maths behind implementing spherical shading with CSS that could be used for this.
这个页面提供了一些用CSS实现球形阴影的数学原理,可以用于此。
Edit: Other answers look nicer, so converted it to a Death Star
编辑:其他的答案看起来不错,所以把它改成了死亡之星
.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;
}
.gridplane {
width: 97vmin;
height: 97vmin;
border-radius: 50%;
border: 0.5vmin dashed rgb(128, 128, 128);
}
.xline { transform: translateY(1%) rotateX(90deg); }
.xline2 { transform: translateY(-1%) rotateX(90deg); }
.yline { transform: rotateY(90deg); }
.zline { transform: rotateZ(90deg); }
.laser {
background-color: rgba(0, 0, 0, 0.05);
transform: translateX(-27.7128%) translateY(-27.7128%) rotateY(90deg) translateX(-27.7128%) rotateY(-135deg) rotateX(45deg) scale(0.3) translateY(-25%);
}
.laser2 {
background-color: rgba(0, 0, 0, 0.05);
transform: translateX(-27.0128%) translateY(-27.0128%) rotateY(90deg) translateX(-27.0128%) rotateY(-135deg) rotateX(45deg) scale(0.2) translateY(-35%);
}
.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 xline'></div>
<div class='layer gridplane xline2'></div>
<div class='layer gridplane yline'></div>
<div class='layer gridplane zline'></div>
<div class='layer gridplane laser'></div>
<div class='layer gridplane laser2'></div>
</div>
<div class='layer clip'>
<div class='shade'>
</div>
</div>
</div>
#5
6
No, it's not possible under your criteria. All examples of 3D stuff using only HTML and CSS have performance issues, because that's not its purpose.
不,按照你的标准是不可能的。所有仅使用HTML和CSS的3D示例都存在性能问题,因为这不是它的目的。
When it comes to heavy graphical effects, HTML and CSS are really bad at it.
当涉及到大量的图形效果时,HTML和CSS真的很糟糕。
The best way to make a real 3D sphere is using WebGL, which is an JavaScript API for creating 3D content.
制作真正的3D球体的最好方法是使用WebGL,这是一个用于创建3D内容的JavaScript API。
#6
4
Have a look at this - sound like what you need and with code snippes you could hopefully edit to your liking. https://codepen.io/Mamboleoo/post/sphere-css
看看这个——听起来像你需要的,有了代码剪切,你就有希望编辑到你喜欢的地方。https://codepen.io/Mamboleoo/post/sphere-css
HTML
HTML
.mommy
.daddy
- for (var x = 1; x < 300; x++)
span
CSS
CSS
@import "compass";
body{
margin: 0;
display: flex;
height: 100vh;
overflow: hidden;
justify-content: center;
align-items: center;
background:black;
}
.mommy{
width: 500px;
height: 500px;
position: relative;
perspective: 800px;
}
.daddy{
width: 500px;
height: 500px;
transform-style: preserve-3d;
animation : rotate 25s infinite linear;
}
span{
display: inline-block;
position: absolute;
top:50%;
left:50%;
perspective: 800px;
transform-style: preserve-3d;
width: 0;
height: 0;
&:before{
content:"";
width: 4px;
height: 4px;
display: inline-block;
position: absolute;
top: calc(50% - 2px);
left: calc(50% - 2px);
background: currentColor;
color: inherit;
border-radius: 50%;
animation: invertRotate 25s infinite linear, scale 2s infinite linear;
box-shadow: 0 0 10px currentColor;
}
}
$amount : 300;
@for $i from 1 through $amount {
$theta : ($i / $amount)*120;
$phi : ($i / $amount) * pi();
$x : 250 * sin($phi) * cos($theta);
$y : 250 * sin($phi) * sin($theta);
$z : 250 * cos($phi);
.mommy span:nth-child(#{$i}){
transform: translate3d($x+px, $y+px, $z+px);
color: hsl(($i/$amount)*360,100%,50%);
&:before{
animation-delay: 0s, -($i/$amount)*2+s;
}
}
}
@keyframes rotate{
to{transform:rotateY(360deg);}
}
@keyframes invertRotate{
to{transform:rotateY(-360deg);}
}
@keyframes scale{
0%, 45%,55%{ box-shadow: 0 0 10px 0px currentColor;}
50%{ box-shadow: 0 0 10px 5px currentColor;}
}
#7
4
Here is an example of an animated sphere / bubble, though this example is more of an illusion. I don't know If everything you are asking for is possible through pure css only, but I may be mistaken.
这是一个动画球体/气泡的例子,尽管这个例子更多的是一种错觉。我不知道你所要求的一切是否仅仅通过纯css就可以实现,但我可能是错的。
.ball {
display: inline-block;
width: 100%;
height: 100%;
border-radius: 100%;
position: relative;
background: radial-gradient(circle at bottom, #81e8f6, #76deef 10%, #055194 80%, #062745 100%); }
.ball:before {
content: "";
position: absolute;
top: 1%;
left: 5%;
width: 90%;
height: 90%;
border-radius: 100%;
background: radial-gradient(circle at top, white, rgba(255, 255, 255, 0) 58%);
-webkit-filter: blur(5px);
filter: blur(5px);
z-index: 2; }
.ball:after {
content: "";
position: absolute;
display: none;
top: 5%;
left: 10%;
width: 80%;
height: 80%;
border-radius: 100%;
-webkit-filter: blur(1px);
filter: blur(1px);
z-index: 2;
-webkit-transform: rotateZ(-30deg);
transform: rotateZ(-30deg); }
.ball .shadow {
position: absolute;
width: 100%;
height: 100%;
background: radial-gradient(circle, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.1) 40%, rgba(0, 0, 0, 0) 50%);
-webkit-transform: rotateX(90deg) translateZ(-160px);
transform: rotateX(90deg) translateZ(-160px);
z-index: 1; }
.ball.plain {
background: black; }
.ball.plain:before, .ball.plain:after {
display: none; }
.ball.bubble {
background: radial-gradient(circle at 50% 55%, rgba(240, 245, 255, 0.9), rgba(240, 245, 255, 0.9) 40%, rgba(225, 238, 255, 0.8) 60%, rgba(43, 130, 255, 0.4));
-webkit-animation: bubble-anim 2s ease-out infinite;
animation: bubble-anim 2s ease-out infinite; }
.ball.bubble:before {
-webkit-filter: blur(0);
filter: blur(0);
height: 80%;
width: 40%;
background: radial-gradient(circle at 130% 130%, rgba(255, 255, 255, 0) 0, rgba(255, 255, 255, 0) 46%, rgba(255, 255, 255, 0.8) 50%, rgba(255, 255, 255, 0.8) 58%, rgba(255, 255, 255, 0) 60%, rgba(255, 255, 255, 0) 100%);
-webkit-transform: translateX(131%) translateY(58%) rotateZ(168deg) rotateX(10deg);
transform: translateX(131%) translateY(58%) rotateZ(168deg) rotateX(10deg); }
.ball.bubble:after {
display: block;
background: radial-gradient(circle at 50% 80%, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0) 74%, white 80%, white 84%, rgba(255, 255, 255, 0) 100%); }
.stage {
width: 300px;
height: 300px;
display: inline-block;
margin: 20px;
-webkit-perspective: 1200px;
-moz-perspective: 1200px;
-ms-perspective: 1200px;
-o-perspective: 1200px;
perspective: 1200px;
-webkit-perspective-origin: 50% 50%;
-moz-perspective-origin: 50% 50%;
-ms-perspective-origin: 50% 50%;
-o-perspective-origin: 50% 50%;
perspective-origin: 50% 50%;
}
body {
width: 300px;
margin: 20px auto;
background: linear-gradient(to bottom, rgba(100, 100, 100, 0.2) 0%, rgba(255, 255, 255, 0.5) 40%, #ffffff 100%);
background-repeat: no-repeat;
}
@-webkit-keyframes bubble-anim {
0% {
-webkit-transform: scale(1);
transform: scale(1); }
20% {
-webkit-transform: scaleY(0.95) scaleX(1.05);
transform: scaleY(0.95) scaleX(1.05); }
48% {
-webkit-transform: scaleY(1.1) scaleX(0.9);
transform: scaleY(1.1) scaleX(0.9); }
68% {
-webkit-transform: scaleY(0.98) scaleX(1.02);
transform: scaleY(0.98) scaleX(1.02); }
80% {
-webkit-transform: scaleY(1.02) scaleX(0.98);
transform: scaleY(1.02) scaleX(0.98); }
97%, 100% {
-webkit-transform: scale(1);
transform: scale(1); } }
@keyframes bubble-anim {
0% {
-webkit-transform: scale(1);
transform: scale(1); }
20% {
-webkit-transform: scaleY(0.95) scaleX(1.05);
transform: scaleY(0.95) scaleX(1.05); }
48% {
-webkit-transform: scaleY(1.1) scaleX(0.9);
transform: scaleY(1.1) scaleX(0.9); }
68% {
-webkit-transform: scaleY(0.98) scaleX(1.02);
transform: scaleY(0.98) scaleX(1.02); }
80% {
-webkit-transform: scaleY(1.02) scaleX(0.98);
transform: scaleY(1.02) scaleX(0.98); }
97%, 100% {
-webkit-transform: scale(1);
transform: scale(1); } }
<section class="stage">
<figure class="ball bubble"></figure>
</section>
#1
69
Strictly speaking, any "3D" shape on a flat screen is more an illusion of a 3D object. All we see is a 2D projection of that shape on the screen plane, and our brain does its best to guess which shape could give the projection we see. If the projection changes, our brain interprets it as a 3D object changing its orientation, which helps it to determine the shape of this object better.
严格地说,平面屏幕上的任何“3D”形状都更像是3D对象的错觉。我们所看到的只是屏幕平面上那个形状的二维投影,我们的大脑会尽最大努力猜测哪个形状可以给我们看到的投影。如果投影变了,我们的大脑就会把它解释为3D物体改变了方向,这有助于它更好地确定物体的形状。
It works well with non-symmetric objects and objects made from polygons (e.g., cubes), but the sphere is a very special case: its projection on the plane always gives just a circle. The static sphere and the rotating one have the same projection, the same circle. Even in real life, if we look at a sphere with a uniform surface without any marks on it (e.g., a polished metal ball), it is hard to determine if it stands still or rotates. Our eyes need some hints, some details that are moving along the surface of the sphere according to its geometry. The more such details move the way you expect from points on the spherical surface to move, the clearer is the perception (well, illusion) of the rotating sphere.
它适用于非对称物体和多边形物体(例如立方体),但是球面是一个非常特殊的情况:它在平面上的投影总是只给出一个圆。静态球和旋转球有相同的投影,相同的圆。即使在现实生活中,如果我们观察一个表面没有任何痕迹的球体(如抛光的金属球),我们也很难确定它是静止还是旋转。我们的眼睛需要一些线索,一些细节,这些线索沿着球体表面根据它的几何形状移动。这样的细节越像你期望的那样从球面上的点移动,你就越能清楚地看到旋转球体的感觉(嗯,错觉)。
And here is the key to the problem in making a CSS scene that would give such perception: to make this illusion strong enough, we need many marks moving along the paths that lie in different planes. And the only way to get this in CSS is to have each mark as a separate CSS box (element or pseudo-element). If our sphere consists only of moving marks, we really need many of them to see it as a sphere — thus "hundreds of elements" in most demos you have seen.
这里有一个问题的关键,就是要制作一个CSS场景来给人这样的感觉:为了让这个错觉足够强大,我们需要很多标记沿着不同平面的路径移动。在CSS中实现这一点的唯一方法是将每个标记作为一个单独的CSS框(元素或伪元素)。如果我们的球体仅仅由移动的标记组成,我们确实需要其中的许多标记来将它视为一个球体——因此,在大多数演示中,我们都看到了“数百个元素”。
So if you want to make a sphere look realistic with a reasonably small number of elements, you would probably need to combine the effects that make an "illusion" of a static basic spherical shape (a circle with radial gradients, inner shadows etc.) with some elements that are relatively small (to make it less obvious that they are actually flat), oriented along the surface of the sphere with 3D transforms, and animated — basically the same way as the faces of the cube in your first demo.
所以如果你想让一个球体看起来相当少量的现实元素,您可能需要结合的影响做一个静态的“错觉”基本球形(一圈径向渐变,内心的阴影等)和一些相对较小的元素(不太明显的,他们实际上是平的),面向沿着球面与3 d变换,和动画——基本上一样的脸立方体在你第一次演示。
Below is my own attempt to put this approach into practice. I used 20 circular elements oriented roughly as the faces of the regular icosahedron (like white hexagons on the classic soccer ball). I grouped them into two groups each making one hemisphere for convenience (it was not necessary, but it made styling a bit simpler). The whole 3D scene consists of the sphere itself and the background frame (pseudo element) which crosses the sphere near its centre (a bit closer, to reduce "flickering" of the circles as they go from the near side to the far side and back) and always faces to the screen. So 24 elements in total (not literally "hundreds", at least:). To make the circles look more "bulging" (like spherical segments), I added two pseudo elements to each of them and elevated them slightly one above other. Works best in Chrome and Firefox 57+ (in Firefox 56- and iOS Safari there is still some "flickering" near the edges). If you hover the circle, you can see the scene without the background frame (and also without "flickering"). A slightly modified version is also available on Codepen.
下面是我自己将这一方法付诸实践的尝试。我使用了20个圆形元素,这些元素的方向大致相当于通常的二十面体的面(比如经典足球上的白色六边形)。我把它们分成两组,每组都是为了方便起见,每个组都做了一个半球(这是不必要的,但它使样式更简单一些)。整个3D场景由球体本身和背景框(伪元素)组成,背景框穿过圆心附近的球体(靠近一点,以减少圆圈从近端到远端和后端的“闪烁”),并且始终面向屏幕。所以总共有24个元素(不是字面上的“数百”,至少是:)。为了让圆看起来更“凸起”(比如球形部分),我在每个圆上添加了两个伪元素,并将它们稍微抬高一点。在Chrome和Firefox 57+中效果最好(在Firefox 56和iOS Safari中,边缘仍然有一些闪烁)。如果你悬停在圆圈上,你可以看到没有背景的场景(也没有闪烁)。在Codepen上也有一个稍微修改过的版本。
.scene {
perspective: 400vmin;
transform-style: preserve-3d;
position: absolute;
width: 80vmin;
height: 80vmin;
top: 10vmin;
left: 10vmin;
}
.sphere {
transform-style: preserve-3d;
position: absolute;
animation: rotate 20s infinite linear;
width: 100%;
height: 100%;
transform-origin: 50% 50%;
top: 0;
left: 0;
}
.scene::before {
content: '';
position: absolute;
width: 100%;
height: 100%;
top: 0%;
left: 0%;
background: radial-gradient(circle farthest-corner at 33% 33%, rgba(240, 240, 220, 0.85) 0%, rgba(30, 30, 40, 0.85) 80%), radial-gradient(circle farthest-corner at 45% 45%, rgba(0, 0, 0, 0) 50%, #000000 80%);
border-radius: 50%;
transform: translateZ(2vmin);
}
.scene:hover::before {
display: none;
}
.hemisphere {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transform-style: preserve-3d;
transform-origin: 50% 50%;
transform: rotateX(90deg);
}
.hemisphere:nth-child(2) {
transform: rotateX(-90deg);
}
.face {
position: absolute;
width: 40vmin;
height: 40vmin;
background: radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.1) 48%, #ff0000 49%, #ff0000 50%, rgba(0, 0, 0, 0) 51%);
transform-style: preserve-3d;
transform-origin: 50% 0;
top: 50%;
left: 20vmin;
}
.face::before, .face::after {
content: '';
position: absolute;
border-radius: 50%;
box-sizing: border-box;
}
.face::before {
width: 50%;
height: 50%;
top: 25%;
left: 25%;
border: 2px solid #333;
background: rgba(255, 255, 255, 0.3);
transform: translateZ(1.6vmin);
}
.face::after {
width: 20%;
height: 20%;
top: 40%;
left: 40%;
background: rgba(0, 0, 0, 0.2);
transform: translateZ(2.8vmin);
}
.face:nth-child(1) {
transform: translateZ(-41.6vmin) rotateZ(36deg) translateY(-6.8vmin) rotateX(143deg);
}
.face:nth-child(2) {
transform: translateZ(-41.6vmin) rotateZ(108deg) translateY(-6.8vmin) rotateX(143deg);
}
.face:nth-child(3) {
transform: translateZ(-41.6vmin) rotateZ(180deg) translateY(-6.8vmin) rotateX(143deg);
}
.face:nth-child(4) {
transform: translateZ(-41.6vmin) rotateZ(252deg) translateY(-6.8vmin) rotateX(143deg);
}
.face:nth-child(5) {
transform: translateZ(-41.6vmin) rotateZ(-36deg) translateY(-6.8vmin) rotateX(143deg);
}
.face:nth-child(6) {
transform: translateZ(-26.8vmin) rotateZ(36deg) translateY(-33.2vmin) rotateX(100deg);
}
.face:nth-child(7) {
transform: translateZ(-26.8vmin) rotateZ(108deg) translateY(-33.2vmin) rotateX(100deg);
}
.face:nth-child(8) {
transform: translateZ(-26.8vmin) rotateZ(180deg) translateY(-33.2vmin) rotateX(100deg);
}
.face:nth-child(9) {
transform: translateZ(-26.8vmin) rotateZ(252deg) translateY(-33.2vmin) rotateX(100deg);
}
.face:nth-child(10) {
transform: translateZ(-26.8vmin) rotateZ(-36deg) translateY(-33.2vmin) rotateX(100deg);
}
.face:nth-child(11) {
transform: translateZ(-26.8vmin) rotateZ(36deg) translateY(-33.2vmin) rotateX(100deg);
}
@keyframes rotate {
0% {
transform: rotateZ(25deg) rotateX(20deg) rotateY(0deg);
}
50% {
transform: rotateZ(-25deg) rotateX(-20deg) rotateY(180deg);
}
100% {
transform: rotateZ(25deg) rotateX(20deg) rotateY(360deg);
}
}
body {
background: #555;
overflow: hidden;
}
<div class="scene">
<div class="sphere">
<div class="hemisphere">
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
</div>
<div class="hemisphere">
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
<div class="face"></div>
</div>
</div>
</div>
#2
23
How do I create an actual 3 dimensional sphere with pure CSS?
如何用纯CSS创建一个真正的三维球体?
Well, as many have stated in answers and comments it is simply impossible to create a single 3D entity in a browser with html and css only at this point in time, but it is possible to create an illusion of a 3D Object. Below is my approach to solving the problem.
好吧,正如许多人在回答和评论中所指出的那样,仅在此时用html和css在浏览器中创建一个单一的3D实体是不可能的,但创建一个3D对象的错觉是可能的。下面是我解决问题的方法。
To give a human eye the ability to see a spherical object, points of reference are needed for the eye to follow. In my case it is lines that define the shape of the sphere. The lines are achieved by giving a border to 5 elements that are in the X-axis set and 5 elements that are in the Y-axis set. Only X/Y sets are given a border, because that alone provides enough reference to make the illusion of a sphere. Additional lines on the Z axis are simply clutter and are unnecessary. If all lines are turned off, the entire thing appears like a solid "perfect" sphere (Looks like a circle, but all parts of it are moving and are present on the 3D plane in the browser!).
为了使人眼能够看到球形物体,眼睛需要有参考点才能跟随。在我的例子中,是线条定义了球体的形状。这些线是通过给X轴集合中的5个元素和Y轴集合中的5个元素一个边框来实现的。只有X/Y集合有一个边框,因为这个边框本身就提供了足够的参考,可以产生球面的错觉。Z轴上的附加线只是杂乱的,没有必要。如果所有的线都被关闭,整个东西看起来就像一个固体的“完美”球体(看起来像一个圆,但是它的所有部分都在移动,并且在浏览器的3D平面上显示!)
What I have done:
- Created 15 html elements that each will represent a circle split in 3 sets of 5
The reason for all of these sets, is that when the entire contraption is rotated on x, y, z axis the backgrounds of each of the elements in x, y, z sets are filling the empty spaces for each other.
所有这些集合的原因是,当整个装置在x, y, z轴上旋转时,x, y, z集合中每个元素的背景都在为彼此填充空白。
- Each set of 5 elements is rotated on X,Y,Z axis respectively in 36 degree increments.
- 每组5个元素分别在X、Y、Z轴上旋转36度。
- All elements are rounded using
border-radius:50%;
- 所有元素都是用边界半径圆角:50%;
- Set background of the circle elements to a solid color
- 将圆形元素的背景设置为纯色
- Put the sets together, so they overlap
- 把这些集合放在一起,这样它们就重叠了
- Clipped minor gaps that came as a result of not enough elements covering the empty spaces between x,y,z circles using
clip-path: circle(96px at center);
on the container, and threw a cool shade/light effect on to seal the deal
vsMore circles will result in a less "edgy" sphere, but since the performance is stressed in the question, a quick clip of the entire thing seemed like the thing to do
更多的圆圈将导致一个不那么“尖锐”的球体,但由于表现是强调在问题中,一个快速剪辑整个事情似乎是做
As a closing thought, I wanted to express my appreciation of the question being asked, it really made me think and was a great project that lead to me learning a lot of things about 3D capabilities of html/css.
Also thanks to all the people who have spent the time to pry this open, and came up with awesome approaches to the problem.
也要感谢所有花时间来撬开这个门的人,并且想出了一些很棒的方法来解决这个问题。
I hope the fruits of my research are of use. Cheers!
我希望我的研究成果是有用的。干杯!
This Pen is also based on Timo Korinth's example.
这支笔也是基于Timo Korinth的例子。
* {
margin: 0;
padding: 0;
}
/* Rotate Sphere animation */
@-webkit-keyframes animateSphere {
0% {
transform: rotateY(0deg) rotateX(0deg) rotateZ(0deg);
}
50% {
transform: rotateY(360deg) rotateX(360deg) rotateZ(0deg);
}
100% {
transform: rotateY(720deg) rotateX(720deg) rotateZ(0deg);
}
}
html {
background: black;
}
.scene {
perspective: 1000px;
}
.container {
margin-top: 5vh;
margin-left: auto;
margin-right: auto;
position: relative;
width: 200px;
height: 200px;
transform-style: preserve-3d;
animation-name: animateSphere;
animation-duration: 30s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
.border {
border: 1px solid white;
}
.circle {
position: absolute;
width: 100%;
height: 100%;
border-radius: 50%;
background: rgba(204, 0, 102, 1);
}
.circle:nth-child(1) {
transform: rotate3d(1, 0, 0, 0deg);
}
.circle:nth-child(2) {
transform: rotate3d(1, 0, 0, 36deg);
}
.circle:nth-child(3) {
transform: rotate3d(1, 0, 0, 72deg);
}
.circle:nth-child(4) {
transform: rotate3d(1, 0, 0, 108deg);
}
.circle:nth-child(5) {
transform: rotate3d(1, 0, 0, 144deg);
}
/* 18! difference to align*/
.circle:nth-child(6) {
transform: rotate3d(0, 1, 0, 0deg);
}
.circle:nth-child(7) {
transform: rotate3d(0, 1, 0, 36deg);
}
/* Upper and Lower circle */
.circle:nth-child(8) {
transform: rotate3d(0, 1, 0, 72deg);
}
.circle:nth-child(9) {
transform: rotate3d(0, 1, 0, 108deg);
}
.circle:nth-child(10) {
transform: rotate3d(0, 1, 0, 144deg);
}
.circle:nth-child(11) {
transform: rotate3d(0, 1, 0, 90deg) rotate3d(1, 0, 0, 0deg);
}
.circle:nth-child(12) {
transform: rotate3d(0, 1, 0, 90deg) rotate3d(1, 0, 0, 36deg);
}
/* Upper and Lower circle */
.circle:nth-child(13) {
transform: rotate3d(0, 1, 0, 90deg) rotate3d(1, 0, 0, 72deg);
}
.circle:nth-child(14) {
transform: rotate3d(0, 1, 0, 90deg) rotate3d(1, 0, 0, 108deg);
}
.circle:nth-child(15) {
transform: rotate3d(0, 1, 0, 90deg) rotate3d(1, 0, 0, 144deg);
}
.shadow {
margin: auto;
border-radius: 50%;
width: 200px;
height: 200px;
box-shadow: 10px 1px 30px white;
}
/* Clip the sphere a bit*/
.clip {
clip-path: circle(96px at center);
}
<div class="scene">
<div class="shadow">
<div class="clip">
<div class="container">
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle border"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
</div>
</div>
</div>
</div>
#3
16
As already mentioned above, CSS3 can't provide you with a real 3d shapes but just with the illusion. A good sphere illusion that use minimum HTML elements and use image as a texture can be done by a combination of what you have done and masking all with CSS shadows.
如上所述,CSS3不能为您提供真正的3d图形,而只能提供幻象。使用最小的HTML元素和使用图像作为纹理的一个良好的球体错觉可以通过你所做的和用CSS阴影来掩盖所有的东西来完成。
A nice touch that can make the mask more realistic is the use of the :after
pseudo element for creation of an additional spark in shifted location and smaller size. A key thing for a successful effect is to remember that different materials reflect light differently. meaning if you're trying to create a metallic sphere the lighting created by the box-shadow
will be different from the lighting of a plastic sphere.
一个可以使蒙版更真实的好方法是使用:after pseudo元素在移动的位置和更小的尺寸中创建额外的火花。一个成功的效果的关键是要记住不同的材料反射光线的方式是不同的。也就是说,如果你想创造一个金属球盒阴影产生的光将不同于塑料球的光。
Another nice addition is the use of the :before
pseudo element for creation of reflection effect. Adding a premade image of the world on a sphere with some opacity can create a very persuasive effect. notice also with the reflection the material you are trying to create will determine the amount of opacity you want for the reflection.
另外一个很好的补充是使用:在pseudo元素创建反射效果之前。在球体上添加一些不透明的预先制作的世界图像可以产生非常有说服力的效果。同样要注意的是,通过反射,你要创建的材料将决定你想要反射的不透明度。
Notice, I used octagonal prism for having the image behind the scene to look more round when the 3d trasform apply its perspective. Even with using only 8 elements the result is quite realistic. More realistic result can be done with more polygons and more complex shapes and texture mappings but even then no need for too many elements due to the addition of the shadow and the spark above everything.
注意,我使用八角形的棱镜,在三维的trasform应用它的角度时,在场景后面的图像看起来更圆。即使只使用8个元素,结果也是相当真实的。更现实的结果可以用更多的多边形和更复杂的形状和纹理映射来实现,但即使这样,也不需要添加太多的元素,因为阴影和星火高于一切。
Last, in order to hide the rest of the octagonal prism and display only the parts inside the sphere bounds I'm using clip-path: circle(99px at center);
.
最后,为了隐藏其余的八角形棱柱,只显示球体边界内的部分,我使用了剪切路径:circle(中间99px);
body {
width: 100%;
height: 100%;
background-color: #000;
}
.cube-wrapper {
width: 0;
height: 0;
top: 100px;
left: 100px;
position: absolute;
perspective-origin: 0 0;
perspective: 80px;
}
.cube-2 {
transform: translateZ(-100px) scaleX(1.8);
transform-style: preserve-3d;
}
.cube {
top: -100px;
position: relative;
transform-style: preserve-3d;
animation-name: rotate;
animation-duration: 10s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
@keyframes rotate {
0% {
transform: rotate3d(0 0, 0, 360deg);
}
100% {
transform: rotate3d(0, 1, 0, 360deg);
;
}
}
.face {
position: absolute;
background-size: 662.4px 200px;
width: 84px;
height: 200px;
}
#face1 {
transform: translateX(-41.4px) translateZ(100px);
background-position: 0 0;
}
#face2 {
transform: translateX(29.2px) translateZ(70.8px) rotateY(45deg);
background-position: -82.8px 0;
}
#face3 {
transform: translateX(58.5px) rotateY(90deg);
background-position: -165.6px 0;
}
#face4 {
transform: translateX(29.2px) translateZ(-70.8px) rotateY(135deg);
background-position: -248.4px 0;
}
#face5 {
transform: translateX(-41.4px) translateZ(-100px) rotateY(180deg);
background-position: -331.2px 0;
}
#face6 {
transform: translateX(-111.4px) translateZ(-70.8px) rotateY(225deg);
background-position: -414px 0;
}
#face7 {
transform: translateX(-141.4px) rotateY(270deg);
background-position: -496.8px 0;
}
#face8 {
transform: translateX(-111.4px) translateZ(70px) rotateY(315deg);
background-position: -579.6px 0;
}
.circle {
position: absolute;
width: 200px;
height: 200px;
border-radius: 50%;
}
.clip-circle {
position: absolute;
padding: 0;
top: -16px;
width: 200px;
height: 200px;
border-radius: 50%;
clip-path: circle(99px at center);
}
.lighting:after {
content: '';
position: absolute;
top: 50px;
left: 67px;
}
.reflection:before {
content: '';
position: absolute;
top: 0px;
left: 0px;
height: 200px;
width: 200px;
background-image:url(https://i.stack.imgur.com/ayCw7.png);
background-size: 200px 200px;
}
.earth {
position: absolute;
left: 20px;
}
.earth .face{
background-image:url(https://i.stack.imgur.com/fdtNz.jpg);
}
.earth .clip-circle {
transform: rotateX(7deg) rotateZ(15deg);
}
.earth .lighting {
box-shadow: -20px -30px 55px 0 rgba(0, 0, 0, 0.9) inset, -75px -100px 150px 0 rgba(0, 0, 0, 0.4) inset, 75px 100px 200px 0 rgba(255, 255, 255, 0.2) inset, -1px -2px 10px 2px rgba(200, 190, 255, 0.2);
}
.earth .lighting:after {
box-shadow: 0 0 150px 51px rgba(255, 255, 255, 0.2), 0 0 26px 10px rgba(255, 255, 255, 0.2);
}
.wood {
position: absolute;
left: 240px;
}
.wood .face{
background-image:url(https://i.stack.imgur.com/sa5P8.jpg);
}
.wood .cube-wrapper {
transform: rotateZ(45deg);
}
.wood .lighting {
box-shadow: -20px -30px 90px 0 rgba(0, 0, 0, 0.7) inset, -75px -100px 140px 0 rgba(0, 0, 0, 0.6) inset;
}
.wood .lighting:after {
box-shadow: 0 0 42px 15px rgba(255, 255, 255, 0.5);
}
.wood .reflection:before {
opacity: 0.04;
}
.metal {
position: absolute;
left: 460px;
}
.metal .face{
background-image:url(https://i.stack.imgur.com/PGmVN.jpg);
}
.metal .cube-wrapper {
transform: rotateZ(-32deg);
}
.metal .lighting {
box-shadow: -20px -30px 100px 0 rgba(0, 0, 0, 0.9) inset, -75px -100px 107px 0 rgba(0, 0, 0, 0.3) inset, 75px 100px 127px 0 rgba(255, 255, 255, 0.23) inset;
}
.metal .lighting:after {
box-shadow: 0 0 42px 20px rgba(255, 255, 255, 0.7), 0 0 7px 6px rgba(255, 255, 255, 0.9);
}
.metal .reflection:before {
opacity: 0.2;
}
<body>
<div style="position:absolute;top:20px;">
<div class="earth">
<dir class="clip-circle">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</dir>
<div class="circle lighting"></div>
</div>
<div class="wood">
<dir class="clip-circle">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</dir>
<div class="circle reflection lighting"></div>
</div>
<div class="metal">
<dir class="clip-circle">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</dir>
<div class="circle reflection lighting"></div>
</div>
</div>
<div style="position:absolute;top:240px">
<div class="earth">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</div>
<div class="wood">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</div>
<div class="metal">
<div class="cube-wrapper">
<div class="cube-2">
<div class="cube">
<div id="face1" class="face"></div>
<div id="face2" class="face"></div>
<div id="face3" class="face"></div>
<div id="face4" class="face"></div>
<div id="face5" class="face"></div>
<div id="face6" class="face"></div>
<div id="face7" class="face"></div>
<div id="face8" class="face"></div>
</div>
</div>
</div>
</div>
</div>
<div style="position:absolute;top:460px;">
<div class="earth">
<div class="circle lighting"></div>
</div>
<div class="wood">
<div class="circle reflection lighting"></div>
</div>
<div class="metal">
<div class="circle reflection lighting"></div>
</div>
</div>
</body>
#4
14
Making a realistic 3d css sphere, without some level of 2d illusion, would require many elements for it to have a smooth perimeter.
制作一个真实的3d css球体,不需要一定程度的2d错觉,需要很多元素使它有一个光滑的周长。
However, I made a version of Timo Korinth's example with:
然而,我用Timo Korinth的例子做了一个版本:
- Clipping of the "back facing" grid lines
- 裁剪“背面”网格线
- Approximate spherical shading by moving a radial gradient
- 通过移动径向梯度近似球形阴影
Can be rotated arbitrarily as long as the shading animation is recalculated.
可以任意旋转,只要阴影动画被重新计算。
This page has some of the maths behind implementing spherical shading with CSS that could be used for this.
这个页面提供了一些用CSS实现球形阴影的数学原理,可以用于此。
Edit: Other answers look nicer, so converted it to a Death Star
编辑:其他的答案看起来不错,所以把它改成了死亡之星
.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;
}
.gridplane {
width: 97vmin;
height: 97vmin;
border-radius: 50%;
border: 0.5vmin dashed rgb(128, 128, 128);
}
.xline { transform: translateY(1%) rotateX(90deg); }
.xline2 { transform: translateY(-1%) rotateX(90deg); }
.yline { transform: rotateY(90deg); }
.zline { transform: rotateZ(90deg); }
.laser {
background-color: rgba(0, 0, 0, 0.05);
transform: translateX(-27.7128%) translateY(-27.7128%) rotateY(90deg) translateX(-27.7128%) rotateY(-135deg) rotateX(45deg) scale(0.3) translateY(-25%);
}
.laser2 {
background-color: rgba(0, 0, 0, 0.05);
transform: translateX(-27.0128%) translateY(-27.0128%) rotateY(90deg) translateX(-27.0128%) rotateY(-135deg) rotateX(45deg) scale(0.2) translateY(-35%);
}
.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 xline'></div>
<div class='layer gridplane xline2'></div>
<div class='layer gridplane yline'></div>
<div class='layer gridplane zline'></div>
<div class='layer gridplane laser'></div>
<div class='layer gridplane laser2'></div>
</div>
<div class='layer clip'>
<div class='shade'>
</div>
</div>
</div>
#5
6
No, it's not possible under your criteria. All examples of 3D stuff using only HTML and CSS have performance issues, because that's not its purpose.
不,按照你的标准是不可能的。所有仅使用HTML和CSS的3D示例都存在性能问题,因为这不是它的目的。
When it comes to heavy graphical effects, HTML and CSS are really bad at it.
当涉及到大量的图形效果时,HTML和CSS真的很糟糕。
The best way to make a real 3D sphere is using WebGL, which is an JavaScript API for creating 3D content.
制作真正的3D球体的最好方法是使用WebGL,这是一个用于创建3D内容的JavaScript API。
#6
4
Have a look at this - sound like what you need and with code snippes you could hopefully edit to your liking. https://codepen.io/Mamboleoo/post/sphere-css
看看这个——听起来像你需要的,有了代码剪切,你就有希望编辑到你喜欢的地方。https://codepen.io/Mamboleoo/post/sphere-css
HTML
HTML
.mommy
.daddy
- for (var x = 1; x < 300; x++)
span
CSS
CSS
@import "compass";
body{
margin: 0;
display: flex;
height: 100vh;
overflow: hidden;
justify-content: center;
align-items: center;
background:black;
}
.mommy{
width: 500px;
height: 500px;
position: relative;
perspective: 800px;
}
.daddy{
width: 500px;
height: 500px;
transform-style: preserve-3d;
animation : rotate 25s infinite linear;
}
span{
display: inline-block;
position: absolute;
top:50%;
left:50%;
perspective: 800px;
transform-style: preserve-3d;
width: 0;
height: 0;
&:before{
content:"";
width: 4px;
height: 4px;
display: inline-block;
position: absolute;
top: calc(50% - 2px);
left: calc(50% - 2px);
background: currentColor;
color: inherit;
border-radius: 50%;
animation: invertRotate 25s infinite linear, scale 2s infinite linear;
box-shadow: 0 0 10px currentColor;
}
}
$amount : 300;
@for $i from 1 through $amount {
$theta : ($i / $amount)*120;
$phi : ($i / $amount) * pi();
$x : 250 * sin($phi) * cos($theta);
$y : 250 * sin($phi) * sin($theta);
$z : 250 * cos($phi);
.mommy span:nth-child(#{$i}){
transform: translate3d($x+px, $y+px, $z+px);
color: hsl(($i/$amount)*360,100%,50%);
&:before{
animation-delay: 0s, -($i/$amount)*2+s;
}
}
}
@keyframes rotate{
to{transform:rotateY(360deg);}
}
@keyframes invertRotate{
to{transform:rotateY(-360deg);}
}
@keyframes scale{
0%, 45%,55%{ box-shadow: 0 0 10px 0px currentColor;}
50%{ box-shadow: 0 0 10px 5px currentColor;}
}
#7
4
Here is an example of an animated sphere / bubble, though this example is more of an illusion. I don't know If everything you are asking for is possible through pure css only, but I may be mistaken.
这是一个动画球体/气泡的例子,尽管这个例子更多的是一种错觉。我不知道你所要求的一切是否仅仅通过纯css就可以实现,但我可能是错的。
.ball {
display: inline-block;
width: 100%;
height: 100%;
border-radius: 100%;
position: relative;
background: radial-gradient(circle at bottom, #81e8f6, #76deef 10%, #055194 80%, #062745 100%); }
.ball:before {
content: "";
position: absolute;
top: 1%;
left: 5%;
width: 90%;
height: 90%;
border-radius: 100%;
background: radial-gradient(circle at top, white, rgba(255, 255, 255, 0) 58%);
-webkit-filter: blur(5px);
filter: blur(5px);
z-index: 2; }
.ball:after {
content: "";
position: absolute;
display: none;
top: 5%;
left: 10%;
width: 80%;
height: 80%;
border-radius: 100%;
-webkit-filter: blur(1px);
filter: blur(1px);
z-index: 2;
-webkit-transform: rotateZ(-30deg);
transform: rotateZ(-30deg); }
.ball .shadow {
position: absolute;
width: 100%;
height: 100%;
background: radial-gradient(circle, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.1) 40%, rgba(0, 0, 0, 0) 50%);
-webkit-transform: rotateX(90deg) translateZ(-160px);
transform: rotateX(90deg) translateZ(-160px);
z-index: 1; }
.ball.plain {
background: black; }
.ball.plain:before, .ball.plain:after {
display: none; }
.ball.bubble {
background: radial-gradient(circle at 50% 55%, rgba(240, 245, 255, 0.9), rgba(240, 245, 255, 0.9) 40%, rgba(225, 238, 255, 0.8) 60%, rgba(43, 130, 255, 0.4));
-webkit-animation: bubble-anim 2s ease-out infinite;
animation: bubble-anim 2s ease-out infinite; }
.ball.bubble:before {
-webkit-filter: blur(0);
filter: blur(0);
height: 80%;
width: 40%;
background: radial-gradient(circle at 130% 130%, rgba(255, 255, 255, 0) 0, rgba(255, 255, 255, 0) 46%, rgba(255, 255, 255, 0.8) 50%, rgba(255, 255, 255, 0.8) 58%, rgba(255, 255, 255, 0) 60%, rgba(255, 255, 255, 0) 100%);
-webkit-transform: translateX(131%) translateY(58%) rotateZ(168deg) rotateX(10deg);
transform: translateX(131%) translateY(58%) rotateZ(168deg) rotateX(10deg); }
.ball.bubble:after {
display: block;
background: radial-gradient(circle at 50% 80%, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0) 74%, white 80%, white 84%, rgba(255, 255, 255, 0) 100%); }
.stage {
width: 300px;
height: 300px;
display: inline-block;
margin: 20px;
-webkit-perspective: 1200px;
-moz-perspective: 1200px;
-ms-perspective: 1200px;
-o-perspective: 1200px;
perspective: 1200px;
-webkit-perspective-origin: 50% 50%;
-moz-perspective-origin: 50% 50%;
-ms-perspective-origin: 50% 50%;
-o-perspective-origin: 50% 50%;
perspective-origin: 50% 50%;
}
body {
width: 300px;
margin: 20px auto;
background: linear-gradient(to bottom, rgba(100, 100, 100, 0.2) 0%, rgba(255, 255, 255, 0.5) 40%, #ffffff 100%);
background-repeat: no-repeat;
}
@-webkit-keyframes bubble-anim {
0% {
-webkit-transform: scale(1);
transform: scale(1); }
20% {
-webkit-transform: scaleY(0.95) scaleX(1.05);
transform: scaleY(0.95) scaleX(1.05); }
48% {
-webkit-transform: scaleY(1.1) scaleX(0.9);
transform: scaleY(1.1) scaleX(0.9); }
68% {
-webkit-transform: scaleY(0.98) scaleX(1.02);
transform: scaleY(0.98) scaleX(1.02); }
80% {
-webkit-transform: scaleY(1.02) scaleX(0.98);
transform: scaleY(1.02) scaleX(0.98); }
97%, 100% {
-webkit-transform: scale(1);
transform: scale(1); } }
@keyframes bubble-anim {
0% {
-webkit-transform: scale(1);
transform: scale(1); }
20% {
-webkit-transform: scaleY(0.95) scaleX(1.05);
transform: scaleY(0.95) scaleX(1.05); }
48% {
-webkit-transform: scaleY(1.1) scaleX(0.9);
transform: scaleY(1.1) scaleX(0.9); }
68% {
-webkit-transform: scaleY(0.98) scaleX(1.02);
transform: scaleY(0.98) scaleX(1.02); }
80% {
-webkit-transform: scaleY(1.02) scaleX(0.98);
transform: scaleY(1.02) scaleX(0.98); }
97%, 100% {
-webkit-transform: scale(1);
transform: scale(1); } }
<section class="stage">
<figure class="ball bubble"></figure>
</section>