seamless blend of canvas distortion and gradual appearance of particles

Take a look at my work in progress on jsfiddle:

https://jsfiddle.net/vzrandom/fkho6grf/8/

I'm experimenting with simplex-noise.js and dat.GUI to add movement to particles. Every 5 seconds, a simulated click occurs on the canvas triggering the start of a new animation. However, I'm facing an issue where the animation begins very abruptly. I'm looking for a way to smoothly fade in the particles upon each click.

Although it may seem like a simple problem, I'm struggling to figure out how to achieve a gradual fade in effect specifically for the elements within the canvas, not the entire canvas itself.

If you're interested in the full code, you can find it on jsfiddle. Here's the snippet of code responsible for handling the click event:

function onCanvasClick(e) {
    context.save();
    context.globalAlpha = 1;
    context.fillStyle = Configs.backgroundColor;
    context.fillRect(0, 0, screenWidth, screenHeight);
    context.restore();

    simplexNoise = new SimplexNoise();
}

Answer №1

In order to achieve the desired effect, you must render to an offscreen canvas.

Start by creating a second canvas:

var canvas2 = document.createElement("canvas");
canvas2.width = canvas.width;
canvas2.height = canvas.height;
var ctx2 = canvas.getContext("2d");

After creating the second canvas, use its 2dContext for all drawing calls.

To implement a fade effect, render the second canvas onto the display canvas with the desired alpha value for the fade.

The function called by requestAnimationFrame receives a high-resolution time as the first argument. The code snippet below demonstrates the update function. Note that if you are using a polyfill for requestAnimationFrame, ensure it aligns with the standard.

var fadeTime = 1; // one second
var fadeTimeStart = undefined; // indicates start of fade when undefined
function update(time){
    // render particles to offscreen canvas

    if(fadeTimeStart === undefined){
        fadeTimeStart = time;
    }

    var fTime = (time - fadeTimeStart) / fadeTime;
    if(fTime < 1){
        ctx.globalAlpha = fTime;
        clearRect(0,0,canvas.width,canvas.height);
        ctx.drawImage(canvas2,0,0);
    }else{
        ctx.drawImage(canvas2,0,0);
    }
    requestAnimationFrame(update);
}

To initiate a new fade in upon a click event, simply set fadeTimeStart = undefined.

Answer №2

Maybe you can achieve the desired effect by partially clearing the canvas with a semi-transparent color in the update function, instead of completely wiping it clean with each click.

You can check out my version here: https://jsfiddle.net/abc123xy/

The major alteration involves adjusting Configs.backgroundColor to a semi-transparent shade and including these two lines in the update function:

context.fillStyle = Configs.backgroundColor;
context.fillRect(0, 0, screenWidth, screenHeight);

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

Validate an object to check for null or empty fields, including arrays, using Javascript

Currently, I am facing an issue with iterating through a complex array that contains objects and embedded arrays. The goal is to detect any empty or null values within the array. However, my challenge lies in accurately determining if an array is empty. De ...

Avoid displaying null values in SELECT and GET operations when using node-postgres

Within my Express API functionality, I aim to offer the client flexibility in providing their contact details, namely phone number or website address, with the option of leaving them blank. The SELECT queries in use are as follows: -- Retrieve all users S ...

Is there a way to replicate a DIV and its contents in ASP.NET using C#?

I am looking to create a form that can duplicate or clone itself when the CPF/CNPJ (Brazilian personal identification) onchange. Below is the current code I have: <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" r ...

The text area in iOS 11 Safari's bootstrap modal is positioned off-center from the cursor

When using Safari with iOS 11, I noticed that the input textbox cursor is appearing outside of the input textbox. This issue is specific to iOS 11 Safari and we are unsure why it is happening. As shown in the image, the cursor is not within the email input ...

Vertical stability bar

I need help creating a vertically fixed navigation bar for my website. Currently, I am using a method that has been discussed in various posts: HTML: <html> <head> src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">< ...

JavaScript: Use onclick events to change the class of a div on multiple divs

I have a jQuery script that allows for toggling of the parent class when #icon is clicked. Additionally, when the body is clicked, it reverts back to the original class. Now, I'm looking to achieve the same behavior when clicking on #item5 or #item4 a ...

Executing Multiple AJAX Requests Sequentially with JQuery

Is there a way to ensure that ajax calls in jQuery are executed in sequence without using async: false? I have tried utilizing ajaxComplete but it has not resolved the issue. The code provided loops through elements in an array and fires off ajax request ...

What is the best way to set up an anchor element to execute a JavaScript function when clicked on the left, but open a new page when clicked in

One feature I've come across on certain websites, like the Jira site, is quite interesting. For instance, if we take a look at the timeline page with the following URL - When you click on the name of an issue (which is an anchor element), it triggers ...

Issue with the position of the search dropdown in HTML

When we click on the Dropdown option, it appears above the text instead of below. I have been trying to fix the positioning issue where the Dropdown shows up separately after clicking, but I have not been successful so far. Maybe you can offer some assist ...

Circular arrangement using D3 Circle Pack Layout in a horizontal orientation

I'm currently experimenting with creating a wordcloud using the D3 pack layout in a horizontal format. Instead of restricting the width, I am limiting the height for my layout. The pack layout automatically arranges the circles with the largest one ...

Troubleshooting: Unable to Remove Files in PhoneGap

I've been working on a basic app that heavily utilizes PhoneGap to test its capabilities. Currently, I'm trying to delete a file that has been downloaded within the app, but I'm encountering some issues. The majority of the code I've im ...

What is the secret behind Facebook's ability to keep the chat bar fixed at the bottom of the browser window?

I am curious to learn about the method Facebook uses to keep the chat bar at the bottom of the browser window even when we scroll through the page. ...

What is the best method for extracting html-string from html-string across different browsers?

Works perfectly in Chrome and FF, but encountering issues with Safari. var content = '<div><span><p>Can you catch me?</p></span></div>'; content = $.parseXML(content); var span = $(content).find('span&apo ...

How to Monitor Clicks on a Flash Movie using jQuery

I've been working on a dynamic banner system that can support image banners and flash banners using object/embed. The site relies heavily on jQuery, especially for handling 'click' events. While tracking clicks on image banners is straightf ...

Unable to bring JavaScript into an HTML document

I am diving into the fundamentals of JS and HTML, starting with a simple script. Here is my test.js file: document.getElementById("test").innerHTML = "Loaded js file"; And here is my test.html: <!DOCTYPE HTML> <html lang="de"> <head> & ...

Tips for accessing array information with JQuery Ajax

I am looking to utilize an AJAX jQuery function to call a CodeIgniter controller function and display the result in array form. Specifically, I want to populate different text fields with values from this array. How can I achieve this using jQuery AJAX in ...

Create a gulp task within a callback function that is initiated by calling the filesystem.readdir

When attempting to read the name of the first folder from a directory, and then defining gulp tasks based on that folder, I am encountering an issue. Specifically, after defining a task inside the callback function, the tasks do not get properly defined. T ...

Exploring the fnServerData function in DataTables

I have integrated Datatables into my project and I am really interested in the "fnServerData" callback option. After reviewing the documentation Here, I came across the example code below: $(document).ready( function() { $('#example').dataTabl ...

How can I send dynamic props between pages using Next.js?

I am currently exploring Next.js and attempting to create a page (index.js) that fetches data about different countries and then displays this information. I would like each country element displayed on the page to have a button that leads to another page ...

Arranging cells within a table with numerous rowspan configurations

I need some help creating a table that will be divided using rowspans for a PDF rendering with BFOreports. I have included an example layout below (numbers are just for reference): https://i.stack.imgur.com/Wbe96.png However, the output I am currently ge ...