Transform a 3D text rotation JavaScript file into an Angular component tailored TypeScript file

I have a javascript file that rotates text in 3D format, and I need help converting it into an Angular component specific TypeScript file. You can find the codepen for the original JavaScript code here.

Below are my Angular files:

index.html

<!doctype html>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <title>RotateText</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.12.0/css/all.css" crossorigin="anonymous">
 </head>
 <body>
  <app-root></app-root>
 </body>
</html>
... // Rest of the Angular files remain the same as mentioned in the original text

Can someone assist me in converting the JavaScript code to TypeScript?

Answer №1

Take a look at the sample on stackblitz Stackblitz

Here's my updated version of the component

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'animation-container',
  templateUrl: './text-rotate.component.html',
  styleUrls: [ './text-rotate.component.css' ]
})
export class AnimationContainer implements OnInit {

 phraseIndex = 1;
  wordIndex = 0;
  texts = [
  ["No!", "This is", "Patrick!"],
  ["I can't", "see my", "forehead"],
  ["Is mayonnaise", "an instrument?"],
  ["F stands", "for Fun", "🎶"],
  ["I wumbo", "you wumbo", "he/she/me wumbo"],
  ["It may be", "stupid, but", "it's also dumb"],
  ["Moss always", "points to", "civilization"]
];

next(text, element) {
  let sample = document.querySelector(element);
  if (sample.dataset.animating === "true") return;
  let sampleH = 50; // will keep it fixed, otherwise sample.offsetHeight
  let sampleT = sample.textContent; // old text
  let sampleNT = text; // new text
  sample.dataset.animating = "true";
  sample.style.height = sampleH + "px";

  // original text element
  let samO = document.createElement("div");
  samO.style.transformOrigin = "0 " + sampleH / 2 + "px -" + sampleH / 2 + "px";
  samO.classList.add("text");
  samO.textContent = sampleT;

  // new text element
  let samN = samO.cloneNode();
  samN.textContent = sampleNT;
  sample.textContent = "";
  sample.appendChild(samO);
  sample.appendChild(samN);

  samO.classList.add("text-out");
  samN.classList.add("text-in");

  samN.addEventListener("animationend", function(event) {
    sample.removeChild(samO);
    sample.removeChild(samN);
    sample.textContent = sampleNT;
    sample.dataset.animating = "false";
  });
}


  changeText(){
  if (this.wordIndex > 2) {
    this.wordIndex = 0;
    this.phraseIndex++;
  }
  if (this.phraseIndex >= this.texts.length) {
    this.phraseIndex = 0;
  }
  let term = this.texts[this.phraseIndex][this.wordIndex];
  this.next(term, ".text-" + ++this.wordIndex);

  if (this.wordIndex == 3) {
    setTimeout(()=>this.changeText(), 2000);
  } else {
    setTimeout(()=>this.changeText(), 150);
  }
}
ngOnInit(){
  setTimeout(()=>this.changeText(), 200);
}
}

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

Exploring the incorporation of an inclusive switch statement within a Redux reducer utilizing Typescript. Strategies for managing Redux's internal @@redux actions

After conducting extensive research, I have yet to discover a definitive answer to this query. There is a question posted on Stack Overflow that provides guidance on how to implement a thorough switch statement: How can I ensure my switch block covers al ...

AJAX loading footer content before images are fully loaded

I am a beginner when it comes to ajax and I'm facing an issue where the footer loads before the images, causing the images to overlap the footer. The problem is illustrated in the image below. <!doctype html> <html lang="en"> <head ...

Is there a way to access the current $sce from a controller?

One way to access the current $scope outside of a controller is by using the following code: var $scope = angular.element('[ng-controller=ProductCtrl]').scope(); Is there a way to retrieve the $sce of the current controller? ...

Struggling to figure out Visual Studio Code - Having trouble loading images and fonts into my CSS file

It seems like I'm missing something here (just so you know, I'm totally new at this). I attempted to use fonts that are loaded on my computer for my CSS stylesheet (font file in my VSC), but no luck. I also tried using images from my files as bac ...

Combine, condense, and distribute JavaScript files using Express without applying gzip compression to the response

Currently, I am developing a web application using Express. My goal is to merge, minify, and serve .js files efficiently. To achieve this, I have created a middleware with the following code: var fs = require('fs'), path = require('path ...

Duo and reference loop

I'm trying to learn how to use backreferences in JavaScript. I have an array and want to replace it within a string. Here's what I've attempted so far: var items = ["book", "table"]; var sentence = "The $1 is on the $2"; var newSentence ...

Instead of relying on Vue TypeScript, we are leveraging IntelliJ with TypeScript 5.0.3 to compile our Vue project

My current version of IntelliJ IDEA is 2023.1 (Ultimate Edition) Build #IU-231.8109.175, released on March 28, 2023. I am facing an issue where my project fails to compile using "Vue TypeScript", resulting in some type mismatches being overlooked. In the ...

Assign a value of 0 to the ngModel if it is empty

I am trying to ensure that when the value is null, it defaults to 0. At the moment, I am utilizing Angular PrimeNG components. <p-calendar [(ngModel)]="model.start_time" [timeOnly]="true" placeholder="00:00"></p-calen ...

incongruity discovered during string conversion in HmacSHA256 within Ionic framework

I am facing an issue while trying to create a token in IONIC using the CryptoJS library. The signature generated by the method is different from what I expect. The expected signature is lLJuDJVb4DThZq/yP4fgYOk/14d3piOvlSuWEI/E7po= but the method provides m ...

Sending a series of filtered GET requests to my Express backend in a MERN (MongoDB, Express, React,

I have developed a MERN web application and am in the process of adding some GET methods to retrieve the same item for different scenarios. However, when I attempt to implement a second filter method and pass an existing parameter in my item schema, Postma ...

What is the best way to showcase nested array JSON data in an HTML Table?

https://i.stack.imgur.com/OHL0A.png I attempted to access the following link http://jsfiddle.net/jlspake/v2L1ny8r/7/ but without any success. This is my TypeScript code: var viewModel = function(data){ var self = this; self.orders = ko.observableArr ...

Header text refuses to cooperate and center itself

Struggling to find the best way to center the "Header" text while keeping the icon in place. I've tried using text-align: center;, but it's not producing the desired results. Could anyone provide guidance on how to achieve this? Thanks. IMG: ...

The FormControl is currently presenting ",required(control)" within its value field

Upon loading my form, the default values in the input fields are set to: ,required(control) { return isEmptyInputValue(control.value) ? { 'required': true } : null; } The template structure of my form is as follows: <form [formG ...

Laravel throws an error message "expression expected" when trying to use the @

I keep encountering an issue when attempting to pass a variable from PHP code to JavaScript. Expression expected Here is the code snippet I am using in Laravel 7.0 : <script> let variable = @json($array); </script> Although the code still ...

Clicking on the spinner does not activate it

I've implemented a basic spinner on my website, and it works fine when using the keyboard. However, I'm experiencing issues with mouse clicks. I can't seem to figure out what is causing this problem. Below is the code snippet related to the ...

I am puzzled as to why the JSON response is visible in the browser but cannot be forwarded to the Ajax callback function, causing the page to remain on the initial request page

Currently, I am in the process of developing a web application that utilizes AJAX and servlet. My main focus right now is creating a functional login page for the application. Although the servlet successfully generates a response in JSON format, the issu ...

"Changing background color, incorporating hover effects, utilizing !important, and manipulating .css properties with

Encountered a dilemma. I devised a "tabs" feature illustrated in the following demo: http://jsfiddle.net/4FLCe/ The original intention was for the tab to change color to A when hovered over, and to color B when clicked on. However, as shown in the demo, ...

Save the raw InputMask data in Formik with Material-UI

I currently have Input Mask implemented with a customized Material UI text field inside a Formik form: <InputMask mask="999-99-9999" maskChar="X" value={values.ssn} ...

What is the best way to spin an element around its center?

I'm faced with a challenge where I have an element that needs to be rotated using JavaScript. The rotation is currently functional, but it's rotating around points other than its center. My goal is to rotate the element around its own center. ...

Uploading a Node.js Package to GitHub Packages - Issue ENEEDAUTH

Hello everyone, I am currently attempting to deploy my NPM package to GitHub packages using the following yaml configuration: # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created # For m ...