Exploring Array Iteration and Incrementing with Easing Techniques

I am currently working on a function that involves iterating over an array and incrementing the values by varying amounts. The challenge is to decrease these incremental amounts as the values in the array get larger.

Let's consider the following example:

// Original array
['100','150','220','280','310','330']

// After first iteration
['106','155','224','283','312','331']

// After second iteration
['112','160','228','288','314','332']

// After third iteration (note the last item has been reset to 100)
['100','117','164','232','291','316']

In the given scenario, I utilized sequential incremental amounts of '6,5,4,3,2,1'. However, with these specific numbers, there is a risk of overlap among the values as they progress. The main challenge lies in determining the correct values to ensure that the elements are closer together towards the end of the array without overlapping.

To reiterate, the issue at hand isn't solely about generating an array of values; instead, it revolves around creating a set of values that maintain the desired easing effect - drawing the elements closer near the boundary of the screen, while avoiding any overlaps or catch-up situations...

Adding further complexity, any value exceeding 332 must be reset back to 100. This action essentially shifts the outermost rings back towards the center...

These calculated values will serve as the foundation for a function that aims to create something similar to the image below:

https://i.sstatic.net/N7e6O.png

The incremental values will dictate the animation sizing for the rings. While CSS seems like a viable option, event-driven multi-touch animations necessitate the use of JavaScript.

My Approach

Despite numerous attempts, I'm struggling to devise the appropriate algorithm and values for this operation! Below is my current implementation that unfortunately does not yield the desired outcome:

// Calculating properties for each ring
for(var i=0;i<6;i++){
    var ring_decrement = 6-i;
    ring_properties[i].dimension += ((-2.5 * ((t=(ring_decrement/10)-1)*t*t*t - 1) + 0.1)/100)*container_width;
    ring_properties[i].opacity = i===0 ? 0 : i===1 ? 0.7 : Math.round((0.6-(i/10))*10)/10;
    if(((ring_properties[i].dimension/container_width)*100)>80){
        ring_properties[i].dimension = 100;
        ring_properties[i].opacity = 0;
    }
}
ring_properties.sort(function(a,b){
    if(a.dimension===b.dimension){
        return 0;
    }else{
        return (parseInt(a.dimension)<parseInt(b.dimension)) ? -1 : 1;
    }
});
// Applying the properties to the rings
$('.radius_ring').each(function(i){
    /*if(ring_properties[i].dimension===100){
        $(this).addClass('paused');
    }*/
    $(this).css({
        width: ring_properties[i].dimension+'px',
        height: ring_properties[i].dimension+'px',
        opacity: ring_properties[i].opacity
    });
});

Furthermore, here is the code segment responsible for initializing the rings (which currently functions as expected):

var ring_dimension_percent = 5;
var ring_explode_increment = 0;
var container_width = parseInt($('#scan_radius_container').css('width'));
var ring_properties = new Array();

// Trigger radius ring expansion on load
var ring_explode = setInterval(function(){
    // Calculating the percentage of the container width
    ring_dimension_percent = ring_explode_increment===0 ? ring_dimension_percent : Math.round(-75 * ((t=(ring_explode_increment/10)-1)*t*t*t - 1) + 10);
    // Conversion from percentage to pixels
    ring_properties[ring_explode_increment] = {
        dimension: (ring_dimension_percent/100)*container_width,
        opacity: ring_explode_increment===0 ? 0 : ring_explode_increment===1 ? 0.7 :  Math.round((0.6-(ring_explode_increment/10))*10)/10
    };
    // Application of styles to the radius rings
    $('.radius_ring:eq('+ring_explode_increment+')').css({
        width: ring_properties[ring_explode_increment].dimension+'px',
        height: ring_properties[ring_explode_increment].dimension+'px',
        opacity: ring_properties[ring_explode_increment].opacity
    });
    if($('.radius_ring').length===(ring_explode_increment+1)){
        clearInterval(ring_explode);
    }else{
        ring_explode_increment++;
    }
},50);

Answer №1

Revise to account for comment on lines catching up/overlapping

To address the issue of overlapping lines, consider using a sine function for your array incrementation. Divide the range of [0º,90º] into six segments: [0, 15, 30, 45, 60, 75], then increase these degrees by one each time. Utilize the sine of these values to calculate your radius.

Below is a code snippet to illustrate this approach:

var DEG_TO_RADIAN = Math.PI / 180; // convert degrees to radians
var minRadius = 100;
var maxRadius = 340;
var degrees = [0, 15, 30, 45, 60, 75];
var radius = new Array(6);

function incrementArray(){
  degrees.forEach(function(d, i){
    degrees[i] = (degrees[i] + 1) % 90; // ensure it stays within [0,90]
    var radian = degrees[i] * DEG_TO_RADIAN;
    var sine = Math.sin(radian); 
    // constrain the radius between min and max allowed values
    // and convert to an integer.
    radius[i] = Math.round(minRadius + sine * (maxRadius - minRadius));
  });

  console.log("Radius = " + radius.join(","))
}

setInterval(incrementArray, 50)

View a sample animation in this Link with Sample Animation or access the associated gist here.


Omit previous answer as it does not tackle the specific problem.

This solution functions, but adjustments are needed to incorporate additional functionalities you require.

arr = [100, 150, 220, 280, 310, 330];
increments = [6, 5, 4, 3, 2, 1];
allowedMax = 332; // reset to 100 once we reach this limit

function incrementArray(){
  arr.forEach(function(d, i){
    arr[i] += increments[i];
    if(arr[i] > allowedMax){
      arr[i] = 100;
    }
  });
  arr.sort(); // sort in ascending order
  console.log(arr) // print for clarity
}

// increment every 1 second
setInterval(incrementArray, 1000)

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

Oops! Looks like there was an error. The text strings need to be displayed within a <Text> component in the BottomTabBar. This occurred at line 132 in the

My app includes a bottomTab Navigation feature, but I encountered an error: Error: Text strings must be rendered within a <Text> component. This error is located at: in BottomTabBar (at SceneView.tsx:132) in StaticContainer in StaticCont ...

Having trouble installing gulp locally due to an error with ` ode_modulesansi-regex`

While attempting to set up Gulp on my Windows 10 system, I encountered an error during the local installation process. $ node -v v9.10.1 $ npm -v 5.6.0 I successfully installed Gulp globally: $ npm install --global gulp-cli $ gulp -v CLI version 2.0.1 ...

Chrome autocomplete behaves as if the input fields are disabled and cannot be clicked on

I am experiencing an unusual issue with autofill in Chrome. After logging in and then logging out of the app, the input fields (email, password) are auto-filled but appear to be frozen and unclickable. This problem does not occur every time; it only happe ...

Is there a way to delay rendering the html until all the content has been fully downloaded?

Whenever I visit my webpage, I notice that the content starts loading without the animation applied to it. It seems like the CSS hasn't finished downloading yet. To solve this issue, I added a preloading bar overlay to signal that the content is still ...

The server returned a response that was void of any content

I have encountered an issue while trying to retrieve data from my server. The request works perfectly fine when tested with Postman, returning the expected data. However, upon implementing the request in my application, I receive an empty object with prope ...

Binding arguments to child functions within Vue components

When working with React, it's a common practice to bind parameters for child components in the following manner: <Child onChange={e => doThing(complex.variable.inParentScope[3], e.target.value)} foo="bar" /> In Vue, I want to ach ...

Is it wrong to use <match v-for='match in matches' v-bind:match='match'></match>? Am I allowed to incorporate the match variable from the v-for loop into the v-bind attribute on the

I am attempting to display HTML for each individual match within the matches array. However, I am uncertain if <match v-for='match in matches' v-bind:match='match'></match> is the correct syntax to achieve this. To clarify, ...

Position the vertical bar directly adjacent to the form input field

Looking for assistance with creating a unique webpage layout that includes a form where the employee ID is visually separated from the rest of the content by a vertical bar extending across the entire page. However, my attempts to achieve this using a gr ...

I am looking to halt the AJAX requests within an asynchronous function after reaching a limit of 10 requests

I've been working on an async function that sends AJAX requests based on the previous response. The function is functioning properly, but it's sending multiple requests in quick succession. I need to implement a 5-second interval between each req ...

Emptying the data of a form that has been submitted using Ajax

None of the similar questions really address the issue from my perspective. I am facing a challenge with a user registration form where I utilize .post for ajax handling. Below is the code snippet: $('#register_user_form').submit(function(){ ...

Having trouble integrating jQuery into an Angular CLI project

I'm trying to incorporate jQuery into my angular project created with angular cli. I followed the instructions provided on this website: To begin, I installed jQuery by running: npm install --save jquery; Next, I added type definitions for jQ ...

Is it possible to have several forms triggered by the same AJAX call but produce varying results

If I have multiple PHP-generated forms and want to include a "Reply" button on each form that sends its content via ajax to another PHP page, can I use the same JavaScript code snippet for all of these elements? <form id="foo"> <label for="ba ...

Node.js error: Attempting to set property '' on an undefined object not allowed

I encountered an issue while attempting to update an array within a model. Even though the usagePlan is defined before the update, the error below keeps getting thrown. customer.usagePlan.toolUsage.tools.push(aNewToolObject); customer.updateAttribute(&apo ...

Issue with plugin conflicting with Vimeo's Froogaloop

I've integrated the Vimeowrap Playlist plugin to create a playlist of videos from a specific channel. Everything seems to be working smoothly in that aspect, but now I'm facing an issue with tracking video events such as play and pause. When atte ...

The optimal functioning of Ajax is conditioned upon the successful upload of the HTML page onto my server

Is it normal that the below code returns an "error" message when the HTML file is not uploaded to my server? I find it odd because once I do upload it, everything works perfectly. Can anyone explain why this happens? <head> <script src="http:// ...

JavaScript returns the value 'undefined' when a function variable is used with an array of objects

Here is an example of an array of objects: var theArray = [ {theId:'1', num: 34}, {theId:'2', num: 23}, {theId:'5', num: 26} ]; This function successfully loops through the array: function printValues() { va ...

Create a fresh trail underneath the overlay image

When utilizing fabric.js to draw a new path with isDrawingMode: true enabled (seen on the right side of the screenshot), I am encountering an issue where the path appears above my overlay image, which is a transparent png. https://i.stack.imgur.com/R3fGn. ...

What is the best way to customize a component in a view?

<template> <div class="about"> <Header /> <h1>Welcome to the dashboard page</h1> </div> </template> <script> import Header from "../components/layout/Header.vue"; export default { name: "dashb ...

Tips for transferring HTML code to a controller

Currently facing an issue while working with MVC and attempting to store HTML code from a view in a database field. In the JS section of my MVC solution, I have the following code snippet: var data = { id_perizia: $("#id_perizia").val(), pinSessione: $("# ...

Utilizing PHP and HTML to pull various values from a single select option

Is there a way to retrieve multiple values from a single select option value? Here is an example: <html> <form method="post" action="#"> <select name="retrive_destination"> <?php while($row = mysql_fetch_assoc($result)){ ...