YoungHaKim7 / nomad_Coders_JS_Final

final
https://younghakim7.github.io/nomad_Coders_JS_Final/
0 stars 0 forks source link

Let's send the moon behind the Earth! #1

Open mochafreddo opened 1 year ago

mochafreddo commented 1 year ago

You can achieve the desired effect by using z-index and adjusting the position of the moon and the earth along the z-axis. Here's the modified CSS code:

.main-screen {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 50vh;
    transform-style: preserve-3d;
    position: relative;
}

.main-screen__earth {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) translateZ(1px); /* Add translateZ */
    width: 320px;
    height: 320px;
    background: url(../../images/map_of_world/map-of-the-world-g69ec16f67_1920.jpg);
    background-size: cover;
    background-repeat: repeat-x;
    border-radius: 50%;
    box-shadow: inset 0 0 20px rgba(0, 0, 0, 1), 0 0 50px #4d5e9c;
    animation: animateEarth 120s linear infinite;
    z-index: 2; /* Add z-index */
}

@keyframes animateEarth {
    0% {
        background-position: 0 0;
    }

    100% {
        background-position: -1920px 0;
    }
}

.main-screen__circle {
    transform-style: preserve-3d;
    animation: animateText 60s linear infinite;
}

.main-screen__circle span {
    transform-style: preserve-3d;
    position: absolute;
    top: 150px;
    left: 360px;
    background: url(../../images/map_of_world/RidiculouslyDetailedMoonPictureInFull-642x642.jpeg);
    color: #35146f;
    font-size: 3em;
    background-size: cover;
    background-repeat: repeat-x;
    border-radius: 50%;
    transform-origin: center;
    transform-style: preserve-3d;
    padding: 5px 11px;
    animation: animateText 60s linear infinite;
    transform: rotateY(calc(var(--i) * calc(360deg / 42))) rotateX(calc(var(--i) * calc(360deg / 42))) translateZ(200px);
    border-top: 2px slid #35146f;
    border-bottom: 2px slid #35146f;
    z-index: 1; /* Add z-index */
}

.main-screen__nomad {
    transform-style: preserve-3d;
    position: absolute;
    top: 150px;
    left: 360px;
    background: url(../../images/map_of_world/m.svg);
    color: #35146f;
    font-size: 3em;
    background-size: cover;
    background-repeat: repeat-x;
    border-radius: 50%;
    transform-origin: center;
    transform-style: preserve-3d;
    padding: 5px 11px;
    animation: animateText 60s linear infinite;
    transform: rotateY(calc(var(--i) * calc(360deg / 42))) rotateX(calc(var(--i) * calc(360deg / 42))) translateZ(200px);
    border-top: 2px slid #35146f;
    border-bottom: 2px slid #35146f;
}

@keyframes animateText {
    0% {
        transform: perspective(1000px) rotateY(360deg) rotateX(15deg) translateY(-30px);
    }
    100% {
        transform: perspective(1000px) rotateY(0deg) rotateX(15deg) translateY(-30px);
    }
}

In this modification, I added z-index to the .main-screen__earth and .main-screen__circle span selectors. The z-index property specifies the stack order of an element, with higher values being closer to the viewer. By setting the z-index of the earth to 2 and the moon to 1, the moon will be obscured by the earth when it goes behind it.

Additionally, I added translateZ(1px) to the transform property of the .main-screen__earth selector. This is to ensure that the earth is positioned slightly closer to the viewer along the z-axis, helping create the desired effect of the moon being obscured when it goes behind the earth.

mochafreddo commented 1 year ago

Apologies for the confusion. In order to make the moon go behind the earth, you'll need to adjust the @keyframes animateText animation. Here's the updated CSS code:

@keyframes animateText {
    0%, 50% {
        transform: perspective(1000px) rotateY(0deg) rotateX(15deg) translateY(-30px);
        z-index: 1;
    }
    25%, 75% {
        transform: perspective(1000px) rotateY(180deg) rotateX(15deg) translateY(-30px);
        z-index: 3;
    }
    100% {
        transform: perspective(1000px) rotateY(360deg) rotateX(15deg) translateY(-30px);
        z-index: 1;
    }
}

This updated @keyframes animateText rule will change the z-index of the moon during the animation so that it goes behind the earth when it's at 180 degrees and comes in front of the earth at 0 and 360 degrees. To make this work, you should also remove the z-index property from the .main-screen__circle span selector:

.main-screen__circle span {
    transform-style: preserve-3d;
    position: absolute;
    top: 150px;
    left: 360px;
    background: url(../../images/map_of_world/RidiculouslyDetailedMoonPictureInFull-642x642.jpeg);
    color: #35146f;
    font-size: 3em;
    background-size: cover;
    background-repeat: repeat-x;
    border-radius: 50%;
    transform-origin: center;
    transform-style: preserve-3d;
    padding: 5px 11px;
    animation: animateText 60s linear infinite;
    transform: rotateY(calc(var(--i) * calc(360deg / 42))) rotateX(calc(var(--i) * calc(360deg / 42))) translateZ(200px);
    border-top: 2px slid #35146f;
    border-bottom: 2px slid #35146f;
}

With these modifications, the moon should go behind the earth during its orbit.

mochafreddo commented 1 year ago

I've made some improvements to the code, such as using animation-timing-function to create smoother transitions, adding @keyframes for the sun's brightness change, and using transform-origin for the earth rotation. Here's the updated CSS code:

body {
    margin: 0;
    padding: 0;
    background: rgb(16, 28, 48);
}

.solar-system {
    width: 300px;
    height: 300px;
    position: relative;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    margin: 100px auto;
}

.sun {
    width: 100px;
    height: 100px;
    background: radial-gradient(circle at 40%, rgba(51, 51, 51, 0.4), rgba(51, 51, 51, 0.4) 50%, rgba(238, 238, 238, 0.151) 75%, rgba(51, 51, 51, 0.233) 75%), rgb(255, 230, 0);
    border-radius: 50%;
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    box-shadow: 0px 0px 6px 6px #494949;
    z-index: 1;
    animation: sunBrightness 3s infinite alternate;
}

@keyframes sunBrightness {
    0% {
        background: radial-gradient(circle at 40%, rgba(51, 51, 51, 0.4), rgba(51, 51, 51, 0.4) 50%, rgba(238, 238, 238, 0.151) 75%, rgba(51, 51, 51, 0.233) 75%), rgb(255, 230, 0);
    }
    100% {
        background: radial-gradient(circle at 40%, rgba(51, 51, 51, 0.4), rgba(51, 51, 51, 0.4) 50%, rgba(238, 238, 238, 0.151) 75%, rgba(51, 51, 51, 0.233) 75%), rgb(255, 255, 128);
    }
}

.earth {
    width: 40px;
    height: 40px;
    background: radial-gradient(circle at 40%, rgba(51, 51, 51, 0.4), rgba(51, 51, 51, 0.3) 50%, rgba(238, 238, 238, 0.151) 75%, rgba(51, 51, 51, 0.233) 75%), rgb(78, 140, 211);
    border-radius: 50%;
    position: absolute;
    left: -300px;
    top: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    transform: rotate3d(0deg, 0deg, 0deg) scale(1);
    animation: rotate 3s infinite;
    transform-origin: 470px 50%;
}

@keyframes rotate {
    0% {
        left: -300px;
        z-index: 3;
        transform: scale(1);
    }

    25% {
        transform: scale(1.2);
    }

    50% {
        left: 300px;
        transform: scale(1);
    }

    75% {
        transform: scale(.8);
    }

    100% {
        left: -300px;
        z-index: -1;
    }
}

In the updated code, the animation-timing-function has been removed as it's not needed since the @keyframes for the .earth animation already defines smooth transitions between the different stages of the animation.

The transform-origin property is added to the .earth selector, which changes the center of rotation for the Earth. In this case, it's set to 470px 50%, so the Earth will rotate around the Sun smoothly.

Additionally, the sun's brightness is animated with the sunBrightness @keyframes rule. This rule alternates the background color of the sun from its original color to a slightly brighter shade, creating a pulsating effect. The animation property on the .sun selector has been updated to include the sunBrightness animation, which runs for 3 seconds in an infinite loop with the alternate keyword. This makes the animation run forwards and then backwards, creating a smooth transition between the two colors.

With these changes, the animation should be smoother, and the Earth will rotate around the Sun more naturally.