Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<div class="flex w-full my-10 items-center justify-center">
      <div x-data="wordFlip()" class=" mx-auto text-center relative preserve-3d letter-container drop-shadow-lg flex flex-col" @glyph-done.window="glyphDone()" >
         <div  class="inline-flex flex-row gap-2 mx-auto preserve-3d mb-10">
            <template x-for="letter in letters" :key="letter.index">
               <div class="flap" x-data="letterFlap(letter.index)" @set-letters.window="setLetter(event.detail)" x-bind:style="`transform: rotateX(${rotation}deg);`" x-bind:data-letter="letter.glyph" x-bind:style="`--offset:${index};`" x-bind:data-character="glyph" data-front="" data-back="" ></div>
            </template>
         </div>
         <div class="text-center mb-5">
            <button @click.prevent="setWord()" class="bg-green-700 text-white font-bold text-xl p-2 rounded-md  hover:bg-black active:bg-black transition-all duration-200">SET WORD</button>
         </div>
         <div class="text-center ">
            <div class="inline-flex flex-col items-center gap-2">
               <label for="speed" class="text-xl p-2 font-bold inline-block">Speed (slow - fast):</label>
               <div class="inline-flex gap-2">
                  <input type="radio" value="6" x-model="speed" id="speed" id="speed6" name="speed" class="w-6 h-6 bg-green-700" />
                  <input type="radio" value="15" x-model="speed" id="speed" id="speed15" name="speed" class="w-6 h-6 bg-green-700"/>
                  <input type="radio" value="30" x-model="speed" id="speed" id="speed30" name="speed" class="w-6 h-6 bg-green-700"/>
               </div>
            </div>
         </div>
      </div>
   </div>
              
            
!

CSS

              
                .letter-container {
         position: relative;
         perspective: 1200px;
         transform-style: preserve-3d;
         --tileColor: #000000;
      }

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

      .flap {
         --size: 100px;
         --fontSize: 6rem;
         --background: var(--tileColor);
         --color:#ffffff;

         display: block;
         position: relative;
         transform-style: preserve-3d; 
         backface-visibility: visible;
         width: var(--size);
         height: calc(var(--size) + 10px);
         padding:2px;
         font-size:var(--fontSize);
         font-weight: 700;
         color: var(--color); 
      }

      .flap::before, .flap::after {
         display: flex;
         align-items: center;
         justify-content: center;
         line-height: .8em;
         
         width: 100%;
         height: 100%;
         position: absolute;
         top:50%;
         left:50%;
         z-index: 1;
         transform-style: preserve-3d;
         background: var(--background);
         backface-visibility: hidden;
         border-radius: 1rem;
         overflow:hidden;
         transition-property: background;
         transition-duration: 2s;
         transition-delay: calc(100ms * var(--offset, 0));
      }

      .flap::before {
         z-index: 1;
         transform: translate3d(-50%, -50%, 0) rotateX(180deg);
         content: attr(data-back);
      }

      .flap::after {
         
         z-index: 2;
         transform: translate3d(-50%, -50%, 0);
         content: attr(data-front);
         
       
      }
              
            
!

JS

              
                 document.addEventListener('alpine:init', () => {
      Alpine.data('wordFlip', () => {
         return {
            phrases: ['Hockey', 'C0d3!', 'Zinger', 'Salmon', 'Truck', 'Backup', 'Import', '@John', 'Eagles', 'Visor', 'Oct 23', 'Hi :)'],
            size: 0,
            done: 0,
            word: '',
            letters: [],
            run: false,
            speed: 15, //angle value must be a divisor of 90/180/270/360
            colors: ['#000000', '#475569', '#dc2626', '#ea580c', '#eab308', '#16a34a', '#2563eb', '#c026d3'],
            color: false,
            colorInt: false,

            init(){

               this.phrases = this.phrases.map(element => {
                  return element.toUpperCase();
               });

               var longestWord = this.phrases.reduce((longest, currentWord) => {
                  if(currentWord.length > longest.length)
                     return currentWord;
                  else
                     return longest;
               }, "");

               this.size = longestWord.length;

               for (var i=0;i<this.size;i++){
                  this.letters.push({index: i, glyph: ' '});
               }

            },

            setWord(){
               let word = this.word;
               while (word == this.word){
                  word = this.phrases[Math.floor(Math.random()*this.phrases.length)];
               }

               this.word = word;
               let letters = this.word.split('');

               this.letters.map((element, index) => {
                  element.glyph = letters[index] ? letters[index] : ' ';
               });

               this.done = 0;
               this.$dispatch('set-letters', {letters: this.letters});

               this.setColor();

            },

            setColor(){
               if (this.done != this.size){
                  let color = this.color;
                  while (color == this.color){
                     color = this.colors[Math.floor(Math.random()*this.colors.length)];
                  }
                  this.$root.setAttribute('style', `--tileColor:${color};`);               
                  this.colorInt = setTimeout(this.setColor.bind(this), 2000);
               }
                        
            },

            glyphDone(){
               this.done = this.done + 1;
               if (this.done == this.size){
                  clearTimeout(this.colorInt);
               }
            }
         }
      });

      Alpine.data('letterFlap', (index) => {
         return {
            glyphs: ' ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890!@#$%^&*()-_=+,.:;?`~{}[]/',
            glyph: '',
            glyphIndex: 0,
            index: index,
            rotation: 0,
            canFlip: false,
            running: false,

            setLetter(details){

               let newGlyph = details.letters[this.index].glyph;

               if (this.glyph != newGlyph){
                  this.glyph = newGlyph;
                  if (!this.running){
                     
                     if (this.rotation == 0) {
                        this.updateIndex();
                        this.updateBack();
                     } else {
                        this.updateFront();
                     }

                     setTimeout(() => {
                        requestAnimationFrame(this.spin.bind(this));
                     },this.index * 50);
                  }
               }
            },
            updateFlap(){
               this.updateFront();
               this.updateIndex();
               this.updateBack();
            },

            updateFront(){
               this.$root.dataset.front = this.glyphs.charAt(this.glyphIndex);
            },

            updateBack(){
               this.$root.dataset.back = this.glyphs.charAt(this.glyphIndex);
            },

            updateIndex(){
               if (this.glyphIndex < this.glyphs.length - 1){
                  this.glyphIndex = this.glyphIndex + 1;
               } else {
                  this.glyphIndex = 0;
               }
            },

            spin(){
               this.rotation = this.rotation + parseInt(this.speed);

               if (this.rotation > 360){
                  this.rotation = 0;
               }

               if (this.rotation == 90  ){
                  this.updateIndex();
                  this.updateFront();
               } else if (this.rotation == 270 ){ 
                  this.updateIndex();
                  this.updateBack();
               }

               if ((this.rotation == 0 && this.$root.dataset.front == this.glyph) || (this.rotation == 180 && this.$root.dataset.back == this.glyph)){
                 //stop
                 this.$dispatch('glyph-done');
                 this.running = false;
               } else {   
                  this.running = true;              
                  requestAnimationFrame(this.spin.bind(this));
               }

            }
         }
      });
     
   })
              
            
!
999px

Console