What is the best way to transform this unfamiliar CSS element into JavaScript code?

I'm facing an issue where I want to animate a CSS image based on time constraints, but since it's in CSS, I'm unable to achieve the desired effect. The animation in question is not the sun itself, but rather a yellowish half-circle animation showcased in this example. My goal is to implement that half circle animation onto a completely random shape.

For example, if there's a white canvas with a random deformed circle in the center, how can I fill that circle with the same animation from the aforementioned example? How do I convert that CSS animation into JavaScript or control the CSS so that it stops and starts at specific values?

I don't expect someone to do all the work for me, but some guidance on where to start when utilizing that yellowish feature from the pen would be greatly appreciated.

Thank you.

Below is the code snippet:

<div class="sunmoon">
        <h2>Sun &amp; Moon</h2>
        ...
   

Answer №1

To achieve this effect in Illustrator, you can create a shape with a transparent inside and a colored outside. Place another box underneath the shape with a different color (such as yellow) and the same width. Use CSS properties like z-index, position:absolute, and left:-100% to position the box perfectly. By clicking on an element, initiate and halt the transition to the right.

A great tool for managing animations like this is GSAP TimeLineMax. It provides functions to control the animation playback:

// Make sure this code runs after the document loads.
let animation = new TimelineMax();
animation
 .to(
      ".underneath-box", // class of the box
      10, // duration in seconds
      { 
        left:"100%", // move to 100% width
        ease: Power4.easeInOut // chosen ease effect
      });

animation.pause(); // Pause the animation by default

$('start-button').click(function(){    // when start button is clicked
    animation.play().timeScale(1);   // start the animation
});


$('reset-button').click(function(){   // when reset button is clicked
    animation.reverse().timeScale(2);   // reverse the entire animation
});

This assumes some knowledge of HTML and CSS basics. Ensure you create the necessary divs and buttons with the correct classes. Good luck!

Answer №2

After spending some time working on this, I've come up with a solution using plain JavaScript. It may not be exactly what you were looking for, but it's the best I could do.

var canvas1 = document.getElementById("canvas1");
var canvas2 = document.getElementById("canvas2");

var context = canvas1.getContext("2d");
context.beginPath();
context.arc(100, 100, 90, 0, 2 * Math.PI);
context.lineWidth = 2;
context.stroke();

animateCanvases();

function animateCanvases(){
  var width = 0;
  var timer = setInterval(function(){
    canvas2.width = width;
    width += 1;
    
    var ctx = canvas2.getContext("2d");
    ctx.beginPath();
    ctx.arc(100, 100, 89, 0, 2 * Math.PI);
    ctx.fillStyle = "#efba32";
    ctx.fill();
    
    if (width === 200) {
      clearInterval(timer);
    }
  }, 20);
}
.canvases{
  position: absolute;
  top: 0;
  left: 0;
}
<canvas id="canvas1" class="canvases" width="200" height="200></canvas>
<canvas id="canvas2" class="canvases" width="200" height="200></canvas>

Answer №3

The provided example demonstrates the use of two divs, an outer and an inner, to achieve a specific visual effect. The outer div is styled with a border radius property that gives it the appearance of a half circle. On the other hand, the inner div is designed as a normal rectangle with overflow set to hidden. Through scripting, the illusion of wiping to the right is created by animating the width of the inner div from 0% to 70% of the outer div. To replicate this effect using a polygon shape, one would need to utilize clip-path instead of border-radius.

For instance, here is code showcasing how an arbitrary polygon can be used to wipe right with a contrasting background color:

<div>
<div class="outer"><div class="inner"></div></div>
</div>

CSS:

.outer { 
  margin-left:200px;
  background-color: lightgray;
  width: 300px;
  height: 100px;
  display: block;
  overflow: hidden;
  -moz-clip-path: polygon(0px 0px, 300px 0px, 300px 300px);
  -webkit-clip-path: polygon(0px 0px, 300px 0px, 300px 300px);
  clip-path: polygon(0px 0px, 300px 0px, 300px 300px);
}
.inner  {
  background-color: red;
  width :0;
  height: 300px;
  display: block;
}

jQuery:

$('.outer').click(function() {
    $('.inner').animate({width:"150px"}, 1800)
}); 

Check out this working fiddle for a live demo: https://jsfiddle.net/r93pocgt/

In my implementation, I've utilized jQuery's animate function to dynamically adjust the CSS property for the width upon clicking the outer div. This approach aims to simplify the process and clarify the functionality of the script.

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

The reason the CSS direct descendant selector does not affect Angular components

We are working with a basic main.html. <app> <sidebar></sidebar> <main-content> <router-outlet></router-outlet> </main-content> </app> After loading a component through routing (changing the ...

The value of ng-repeat list in AngularJS does not update when its value is changed by an ajax call

I am completely perplexed. Why doesn't my ng-repeat update when there is an ajax call that changes its value? I have searched through many questions and answers here, but none of them address the issue with the ajax call. Here is the HTML: <div c ...

Implement a basic JavaScript prompt feature within a Node.js application that can be integrated into

My Angular App is wrapped by electron and utilizes node.js to fetch MySQL data for AngularJs via electron. However, since there is no fixed database in my project, I have to dynamically change the database credentials from the client side, making sure it p ...

What is the best way to manage zoom settings following a completed search query?

Whenever I try to search for an address using this map, it zooms in way too much making the map unusable. Despite my efforts to adjust the map bounds, I have not been successful. It seems like I am on the right track but for some reason, it just isn't ...

React - Defining a global variable within a React component

Can you explain the process to me? I am trying to set up a variable within a react component class so that I can utilize it in multiple sections of my application. Here is what I have attempted: class ColorPick extends React.Component { colorInput; ...

Tips for aligning the elements in the navigation bar in the center

I am faced with a dilemma regarding my navbar design. Here is an image of my current navbar layout: I am attempting to align the components within the navbar to resemble the following design: Here is the HTML code I am using: <!-- Navbar --> < ...

Is the dragging behavior of a rotated image different than that of the original image when using CSS rotation?

While working on a CSS grid to showcase images rotated at 60 degrees for a diagonal view, I encountered an issue. I wanted users to have the ability to drag and drop images within the grid, but when they drag an image, it moves as if it weren't rotate ...

Modify the placeholder color for a disabled Input element to match the overall Mui theme design

I am struggling to change the color of the placeholder text in a disabled input field within my app's theme. Despite attempting to target the MuiFormControl, MuiInputBase, and MuiInput components in my theme.tsx file on CodeSandbox, I have not been su ...

Exporting Javascript functions is not possible

Programming in TypeScript import { Component, OnInit } from '@angular/core'; import {loadCalendar} from '../../../../scripts/artist/artist-home'; import {activate_searchBar} from '../../../../scripts/search_bar_activate'; @C ...

Tips for implementing search suggestions onto an Ajax success event

I have been struggling to implement search suggestion in a text box. I've tried various methods but haven't found the right approach yet. I fetch data (arraylist) from the back end using ajax and return it to jsp as json. Now, I need to add the s ...

Using Waveskeeper for Verification: Solutions for Resolving the 'Waves is not defined' Issue

I'm currently working on creating a page that authenticates users with Waveskeeper. I have successfully installed the Waveskeeper addon on Chrome, but I keep encountering an error: ReferenceError: Waves is not defined. I've attempted to use both ...

Retrieving information via RESTful API using jQuery

Seeking assistance with integrating a JSON feed into an HTML document using jQuery. For instance, I am trying to extract the "base_title" and display it in an h2 tag. Any insights on how to achieve this would be greatly appreciated. Your help will be high ...

A JavaScript function that produces an array as an output

Could anyone explain why the output vector from the function TriangulosParaLinhas is not being stored in the vector Lines? if (lineMode == true) { var lines = triangulosParaLinhas(vertices); } function triangulosParaLinhas(vertices) { var poi ...

Displaying Image Preview in Angular 2 After Uploading to Firebase Storage

At the moment, I am facing an issue where the uploaded image is not being displayed after the uploadTask is successful. This problem arises due to the asynchronous loading nature of the process, causing the view to attempt to display the image before the u ...

Tips for updating a value within ng-repeat only if the value is equal to "string"; otherwise, hide it from display

I'm having trouble updating the data in my table to display "Yup" or blank space based on certain conditions. I am new to this and tried a solution, but it's not working: {{ person.value ? "Yup":" "}} .. Can anyone help me with this? angular.m ...

Tips for populating a dictionary with a large datalist of around 2000 items

Currently, I'm facing an issue where the code I'm using takes about 10 seconds to run on Chrome and around 2 minutes on IE11, which is the primary browser it will be used with. for (var key in dict) { if (dict.hasOwnProperty(key)) { ...

npm encountered an error or issue during the installation process

I have configured my proxy settings in the .npmrc file, but I am encountering errors when running the npm install command: $ npm install npm ERR! Windows_NT 6.1.7601 npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program File ...

What is the correct way to have Material-UI's <TextField/> component return with a ref attribute, similar to <input/> in ReactJS?

Using this particular method: handleClick(event) { const inputText = this.refs.inputText console.log(inputText.value.trim()) } I am attempting to make Material-UI's <TextField/> return the input text accurately with a ref, similar ...

Ways to stop cascading CSS attributes to child elements?

I am in the process of creating three tables. <table id="first"> <tr> <td> 1. CAPTION </td> </tr> <tr> <td> <table id="second"> <tr& ...

What steps can I take to make sure JSON keys containing only digits are treated as Strings instead of Numbers?

Within a JSON String, I have IDs as keys, represented as Strings of numbers, while the values are actual Numbers (Float). The problem arises when parsing this information to obtain an object in Safari 10.1.2... var jsonData = "{\"53352\":0.6, ...