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

Change this npm script into a gulp task

I have a script in npm that I would like to convert into a gulp task. "scripts": { "lint": "eslint .", "start": "npm run build:sdk && node .", "posttest": "npm run lint && nsp check", "build:sdk": "./node_modules/.bin/lb- ...

Troubleshooting Problems with Google Maps and Javascript/JSON in Internet Explorer

Currently, I am utilizing the Google Maps API to construct a map that displays store locations in close proximity to a user-specified location. Everything is functioning properly, however, I am encountering an error in Internet Explorer that I would like t ...

Changing array indices in JavaScript by splicing elements

I'm experiencing a curious issue that seems to involve overwriting array indices after splicing, or at least that's what I suspect. This problem arises in a small game project built using phaser 2 - essentially, it's a multiplayer jumping ga ...

Navigation Bar Search Box Alignment Problem

Hi there, I'm new to programming and I'm trying to center my search bar with the navigation links beside it within a fixed navigation bar. However, I'm having trouble getting it to work properly. Can someone take a look at my HTML and CSS co ...

The error encountered is an unhandled rejection with a message stating "TypeError: Cannot access property 'username' of null

My tech stack includes NodeJS, PassportJS, MySQL, and Sequelize (ORM for MySQL). The following code snippet is taken from my Passport.JS file. Whenever a user registers on my website, an error is returned if the username or email is already in use. If both ...

What is the best method for purging a previously stored variable from the cache when making an Ajax call and simultaneously passing a

I am encountering an issue with variable loading during the ajax call. Upon clicking, I pass a variable to the ajax call which then sends the value to a PHP script. The data obtained is from: rnc.csv DLRNC01 DLRNC02 DLRNC03 DLRNC04 DLRNC05 DLRNC06 DLR ...

Sending numerous parameters in a form

I'm having an issue with my code where the 'name' variable is POSTed but the 'project' variable is not when submitting through an 'onchange' event. I need both variables to go to session variables on submit. Can anyone sp ...

Incorporate custom JavaScript files that contain classes within a Vue component

i am encountering an issue with a js file that contains classes with functions. i have a vue component and i want to create an instance of that class within it. (when directly copying the file into my <script> tag, everything works smoothly) myfile. ...

skip every nth element in the array based on the specified value

The Challenge I'm currently working on a graph that relies on an array of data points to construct itself. One major issue I've encountered is the need for the graph to be resizable, which leads to the necessity of removing certain data points ...

DxDataGrid: Implementing a comprehensive validation system for multiple edit fields

I'm currently working with a DxDataGrid within an Angular Application. Within this particular application, I have the need to input four dates. I've implemented validation rules that work well for each individual field. However, my challenge aris ...

Troubleshooting drag-and-drop functionality in a JavaScript HTML5 application resembling a Gmail upload interface

Here is a snapshot of my user interface: Each node in the tree structure is represented by an <li> element along with an <a> link. Furthermore, each folder serves as a dropzone for file uploads similar to the drag-and-drop feature found in Gm ...

Is it possible to automatically access the most recent Beta build through package.json and npm?

We are currently working on a project that has a specific dependency requirement for the latest beta build from an npm library. However, there are also -dev builds within the library. For instance, in the "x-library" there might be versions like: "1.2.3- ...

The functionality of jquery ujs seems to be impaired when I try to load partials or content remotely using remote calls or

Currently, I am using a piece of jQuery code to populate an element on the webpage with some content: var content = $("#note_"+note.id).html(); $("another_div").html(content); Although this successfully replaces the HTML of the other div, the issue is ...

Swap out the HTML button element for text once the form is submitted

On the main page, I have a button that opens a modal when clicked. Inside the modal, there is a form with a submit button that closes the modal and returns to the main page. After closing the modal, I want to change the HTML button on the main page to plai ...

Saving Style Sheets in a Database

Currently, I am interested in saving CSS data in a mySql database as part of my LAMP setup. My intention is to create an input field that includes the following code: body{ background: url("http://google.com/image.jpg") no-repeat; color: orange; } #someDi ...

Converting an onclick event to onsubmit: Tips for doing it right

Recently, I had to add validation to a file upload form for our Google Drive. After some research, I discovered that using onsubmit instead of onclick was the way to go. However, when I made the switch from onclick to onsubmit, the validation worked but t ...

I am facing an issue where an AJAX post to Express is not returning any data to req.query. I have tried various solutions but nothing seems to

I have encountered an issue with my setup where the body is empty when sending data through ajax. In Chrome's network tab, I can see the post and content with the correct payload: {"EventName":"asd","PrivacyLevel":1,"TypeInt":1,"ExpectedDate":"asd"," ...

Create a duplicate of a div in jQuery while also updating its contents

I need help cloning a div that has the following structure: <div id="multipleSteps"> <div id="step_1"> <input type="text" name="step_name_1" id="step_name_1"> </div> ...

Removing cookies after sending a beacon during the window unload event in Chrome

Here's the situation: I need to make sure that when the browser is closed or the tab is closed, the following steps are taken: Send a reliable post request to my server every time. After sending the request, delete the cookies using my synchronous fu ...

Dealing with Memory Issues in Google Apps Scripts

Currently, I am addressing a challenge within my organization that requires me to maintain some level of ambiguity. However, I have been granted permission to discuss this issue openly. The task at hand involves creating a script to analyze a Google Sheet ...