Tips for manipulating SVG in a 3D realm using CSS

I'm struggling with creating a spinning animation using a `svg` that contains three `polygons`. I want to rotate each of them horizontally in 3D space, but the animation appears flat. Below is the code snippet showcasing the three polygons with classes `cls-1`, `cls-2`, and `cls-3`:

.svg-holder {
    height: 400px;
    width: 800px;
    perspective: 1000px;
}

.svg-holder svg {
    transform-style: preserve-3d;
}

.cls-1 {
    transform-style: preserve-3d;
    animation-name: rotate;
    animation-duration: 4s;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
}

@keyframes rotate {
    from {
        transform: rotateY(0deg);
    }
    to {
        transform: rotateY(180deg);
    }
    
}
<div class="svg-holder">
      <svg viewBox="0 0 237 126">
        <defs>
          <style>
            .cls-1 {
              fill: #2743b7;
            }
            .cls-2 {
              fill: #42b6d1;
            }
            .cls-3 {
              fill: #17c648;
            }
          </style>
        </defs>
        <g id="Layer_2" data-name="Layer 2">
          <g id="Layer_1-2" data-name="Layer 1">
            <polygon class="cls-1" points="237 0 79 0 0 42 158 42 237 0" />
            <polygon class="cls-2" points="237 42 79 42 0 84 158 84 237 42" />
            <polygon class="cls-3" points="237 84 79 84 0 126 158 126 237 84" />
          </g>
        </g>
      </svg>
    </div>

Answer №1

When you include transform-origin: center center; in the styles for .cls-1, .cls-2, .cls-3, it creates an effect where the elements spin around the vertical axis:

.svg-holder {
    height: 400px;
    width: 800px;
    perspective: 1000px;
}

.svg-holder svg {
    transform-style: preserve-3d;
}

.cls-1, .cls-2, .cls-3 {
    transform-style: preserve-3d;
    transform-origin: center center;
    animation-name: rotate;
    animation-duration: 4s;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
}

@keyframes rotate {
    from {
        transform: rotateY(0deg);
    }
    to {
        transform: rotateY(360deg);
    }
    
}
 <div class="svg-holder">
      <svg viewBox="0 0 237 126">
        <defs>
          <style>
            .cls-1 {
              fill: #2743b7;
            }
            .cls-2 {
              fill: #42b6d1;
            }
            .cls-3 {
              fill: #17c648;
            }
          </style>
        </defs>
        <g id="Layer_2" data-name="Layer 2">
          <g id="Layer_1-2" data-name="Layer 1">
            <polygon class="cls-1" points="237 0 79 0 0 42 158 42 237 0" />
            <polygon class="cls-2" points="237 42 79 42 0 84 158 84 237 42" />
            <polygon class="cls-3" points="237 84 79 84 0 126 158 126 237 84" />
          </g>
        </g>
      </svg>
    </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for ensuring a flexbox stays within the bounds of its container and doesn't exceed the available space

I am in the process of developing a modal that needs to be centered on my page and expand organically, but only up to a specific width and height limit. Using flexbox, I have successfully centered this modal (.modal) on the page. Within the modal, there ...

Difficulty with replacing colors in an image on certain devices when using HTML5 Canvas

I have created a 2d RTS HTML5 / Javascript game that utilizes images to represent the player's units and buildings. To achieve different variations of these images with different colors, I use a script that replaces certain colors in the original imag ...

How can we improve our vertical division method?

Looking for a more efficient way to create a vertical divider between two divs. I want the divider to be centered between the "why choose" and "gallery" sections. Here is an example of what I'm trying to achieve. I've attempted the following co ...

Having trouble with loading a 3D gltf model on A-frame.io?

I'm experiencing some difficulties when attempting to load a gltf model onto an a-frame. Unfortunately, the model does not appear to be loading properly. Here is the code snippet in question: <html> <head> <title>Tree model</t ...

Columns are not properly aligning side by side within the .card class

I am facing an issue where my cards are not lining up inline next to each other in columns but are instead jumping under each other. I have tried setting the col-md-6 class to align two of them next to each other, but that did not work. I have also checked ...

Retrieve the CSS attributes within a directive specifically designed for child elements

Currently, I am iterating through the child elements of the element to which the directive is attached, and adding mouseup and mousedown callbacks: var dragDroppables = element[0].children; for (var elementIdx = 0; elementIdx < dragDroppables. ...

Introducing space between div elements changes the arrangement of columns

I am facing an issue with my layout consisting of 6 div tags divided into 2 rows of fixed height. Whenever I try to add a margin around the divs, the rightmost div gets pushed down to the next row. How can I solve this problem? HTML: <div class="contai ...

Is the ctx.arc method in Javascript able to determine the vertices based on the pixel size and radius?

When working with Javascript's Canvas, you have the option to draw a circle easily using the ctx.arc method. I'm curious, does the arc function automatically calculate the optimal number of vertices needed to draw a circle in order to achieve the ...

What about employing the position:fixed property to create a "sticky" footer?

I've come across various methods for creating a sticky footer, such as Ryan Fait's approach found here, another one here, and also here. But why go through the trouble of using those techniques when simply applying #footer{position:fixed; bottom ...

Checking all checkboxes in a list using Angular 5: A simple guide

I have a list that includes a checkbox for each item, and I want to confirm if all of them are checked off. This is the HTML code snippet: <ul class="ingredient-list" > <li class="btn-list" *ngFor="let ingredient of recipe.ingredients"> ...

Struggling to figure out the correct method for aligning calendar using bootstrap-datetimepicker?

Struggling with aligning the calendar correctly under a textbox while using bootstrap-datetimepicker in a form? The positioning seems off, and the calendar icon isn't where it should be. It's not behaving like a simple datetimepicker as shown in ...

Is it possible to click on the second item in a list based on its Span class using Selenium?

self.driver.find_element_by_xpath("//span[@class='bog']").click() This is the current code I am using. It allows me to click on the first email in my Gmail inbox. However, I now want to be able to click on the second or third email. I am struggl ...

Having trouble with submitting an HTML form using JavaScript and PHP

My webpage has an HTML form that sends data to a PHP file for processing. One issue I'm facing is with a dynamically generated combo box that works fine when the page loads, but the selected value is not being passed when the form is submitted. The J ...

What is the best way to navigate to the top of a form panel?

I'm currently developing a Sencha Touch mobile app. I have a form panel with multiple fields, so I made it scrollable. However, every time I open the screen (panel), scroll down, navigate to other screens, and then return to the form panel, it stays s ...

Customize MUI Tabs to resemble the design of Bootstrap Nav Tabs

Looking to customize Material UI Tabs to resemble Bootstrap Tabs. Made significant changes, but struggling with removing the border-bottom on the activeTab. In Bootstrap, they use marginBottom: -1px on the active tab, but it doesn't work for me. Any s ...

Angular service provided by MetronicApp

I've been working on integrating Angular services with the Metronic App. This is how I defined my service: angular.module('MetronicApp').service('shaperService', ['$http', function ($http) { this.shapers = function(param ...

Tips for keeping a dynamic height div in place without affecting surrounding elements

HTML: <div id="autocomplete" hidden></div> <input type = "button" id = "search" value = "Search"> The autocomplete div contains dynamically generated input tags created by jQuery. These input tags cause the button to be shifted down the ...

Unite animations seamlessly with Cocoa-Touch

Trying to seamlessly chain animations together may seem complex at first, but it's actually quite manageable. My goal is to create a smooth transition by animating a UIPickerView that slides up from the bottom of the screen when displayed, and slides ...

Toggle <DIV> Visibility Depending on <SELECT> Selection

Can someone please assist me in figuring out why this code, which works perfectly on jsFiddle, is not functioning on my website? I have spent hours trying to resolve this issue, but no matter what I do, I cannot seem to get it to work. I suspect it may be ...

Content within the two inline divs is experiencing alignment problems?

I am experiencing alignment issues with the content inside two divs that contain multiple nested divs. The content is not properly aligned. CSS Code: .breadcrumb-nav{ margin-left: 230px; left: 70px; top: 70px; width: 1291px; height: 6 ...