Enhancing the visual appeal of a javascript canvas animation

I am facing a small issue and I want to incorporate an animation from: http://codepen.io/chuckeles/pen/mJeaNJ

My main query is how can I modify it so that instead of dots, there would be small images? Which part of the code should I edit for this purpose? I am unsure about where to begin, so any guidance on which section needs editing to change dots to images would be greatly appreciated. Full Javascript code provided below:

// --- CANVAS ---

var canvas = document.getElementById("bg");
var ctx = canvas.getContext("2d");

// --- UTILS ---

// request frame
var requestFrame =
    window.requestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.msRequestAnimationFrame;

// window resizing
(window.onresize = function() {
  // set new canvas size
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
})();

// --- STARS ---

var Star = function(x, y) {
  this.x = x || 0;
  this.y = y || 0;

  this.mx = 0;
  this.my = 0;

  this.distance = 0;
  this.mult = Math.random() * 0.4 + 0.6;
};

// --- GLOBALS ---

// the star array
var stars = [];

// stars to remove from the array
var starsToRemove = [];

// create random stars
for (var i = 0; i < 60; ++i) {
  // pos
  var x = Math.random() * canvas.width;
  var y = Math.random() * canvas.height;

  // create
  stars.push(new Star(x, y));
}

...

// start loop
requestFrame(loop);

Answer №1

If you decide to make changes, consider revising it following the comment below:

// --- EDIT ---

There are different canvas drawing methods that you may need to modify. For instance, the code snippet

ctx.clearRect(0, 0, canvas.width, canvas.height);

could erase all previously drawn pixels within the canvas dimensions.

You could retrieve an array representing the canvas pixels with this method.

ctx.getImageData(0, 0, canvas.width, canvas.height);

You could also render each pixel as a star.

// for every star
stars.forEach(function(star) {
    // obtain position
    var x = Math.floor(star.x);
    var y = Math.floor(star.y);

    // draw a pixel
    var i = y * data.width + x;
    // change pixel color
    data.data[i * 4 + 0] = 255;//Red = 255
    data.data[i * 4 + 1] = 255;//Green = 255
    data.data[i * 4 + 2] = 255;//Blue = 255
    // ^ color (rgb)

    // adjust opacity based on distance
    var a = (star.distance - 40 * star.mult) / (canvas.width / 2 - 40 * star.mult);
    data.data[i * 4 + 3] = 255 * a * star.mult;
});
// reinsert
ctx.putImageData(data, 0, 0);
// ^ this line updates the edited canvas pixels above

Using DOM techniques, you could replace pixels in the canvas with spans or divs, although this can negatively impact performance.

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

vue.js watch function failing to update

Hello, I'm just getting started with Vue and currently facing a challenge. I am attempting to update a couple of variables based on changes in another computed variable. The computed variable is connected to a Vuex store and functions correctly, displ ...

Tips on utilizing variables instead of static values when calling objects in JavaScript

How can I make something like this work? I tried putting the variable in [], but it didn't work. Can someone please help me out with this? Thank you. const obj = { car : "a" , bus: "b" }; const x = "car" ; ...

Stream music from SoundCloud by simply clicking a button on an external source, no need

My post contains an embedded SoundCloud player using the following code: <iframe class="soundcloud_iframe" width="100%" height="166" scrolling="no" frameborder="no" src="'.esc_url('https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcl ...

Interacting with a form input by triggering the onChange event

I am encountering a problem where I need to be able to select a radio button both onChange via keydown and mouse click. However, I am struggling with accessing both event parameters of the on keydown and on mouse click within the same function. As a result ...

I'm having trouble making Jquery's $.get function work

I've been attempting to use the $.get function to retrieve a response from a PHP script, but I'm not having any luck. Nothing seems to be happening. Here is the jQuery code I am using: $(document).ready( function(){ $('#button').c ...

Trouble with minification in Sencha cmd 5

I've been attempting to compress a Sencha 5 project using Sencha CMD, but I keep experiencing failures. sencha generate app -ext demoApp ./demoApp Then, in an effort to compress the application, I entered the following command: sencha app build ...

Is it feasible to incorporate the CSS and Bootstrap (CDNs) specific to an HTML component without affecting the styling of other pages?

One way I am able to recycle the HTML component is using this code: $.get("nav.html", function(data) { $("#fulloptions").replaceWith(data) }) However, there’s a conflict because nav.html uses a different version of Bootstrap than my index file does ...

What could be causing the lack of value appearing after I clicked the button?

Setting the value for the array of images as an empty string does not return the expected result. When I move one of the 4 images and click the button, I anticipate a new array with the information of one of the four images including src, x, and y coordina ...

Decoding multiple data with jQuery on the server-side

Scenario: A situation arises where I am utilizing a jQuery.ajax call to send three arrays to the server for database storage. The challenge lies in decoding the combined data object on the server side and breaking it back into the original three arrays. ...

Is there a delay in using ngShow and ngClick for authentication in AngularJS and Firebase?

Just getting started with AngularJS and encountering an unusual issue with Firebase authentication. The basic setup displays the current user status (logged in or not) along with options to sign in and out. Oddly, when I click the Log-in button for the fi ...

Is there a bug causing the Admin LTE content height issue? Looking for solutions to fix it

What's the reason behind the oversized footer? The footer appears normal when I resize or zoom the page, why is that? How can this be resolved? ...

Chrome and Firefox provide excellent compatibility for running JavaScript, whereas Safari may encounter some issues. Opera's performance with JavaScript can be quirky

Disclaimer: I'm new to web design and development. I have encountered an issue with posting information from a form built on CodeIgniter using jQuery. The form posts successfully in Chrome and Firefox, with the current page automatically reloading. H ...

Checkboxes display on a separate line when rendered inlines

I'm having an issue with the rendering of my checkbox in CSS. The checkbox is currently displaying on a separate line from the text, but I would like it to be inline with the rest of the content. For reference, here is the full CSS and HTML code: htt ...

When hovering over the bootstrap dropdown, the main dropdown remains a clickable link

How can I create a bootstrap dropdown menu that activates on hover, while still allowing the main button to link to a different page when clicked? Below is the HTML code: <div class="body-wrap"> <div class="container"> <nav class="navbar n ...

What should I designate as the selector when customizing dialog boxes?

I am attempting to change the title bar color of a dialog box in CSS, but I am running into issues. Below is the HTML code for the dialog box and the corresponding CSS. <div id="picture1Dialog" title = "Title"> <p id="picture1Text"> ...

What is the method for including a Bootstrap Icon within a placeholder text?

I am attempting to insert a search icon inside an input text field, but I am unsure of how to accomplish this. My desired result is similar to: input search Can Bootstrap Icons be used within a placeholder? I have seen some examples, but the icon remaine ...

Jquery Ajax is failing to transmit the authorization header

Attempting to set up basic authentication using Jquery Ajax. Adding an authorization header to the ajax request as shown below. $.ajax({ headers: { "authorization": "basic 123456", }, crossDomain: true, type: "POST", contentTyp ...

React: Conceal additional elements once there are over three elements in each section

React: I am trying to create a menu with submenus. My goal is to calculate the number of submenus and if it goes over 3, hide the additional submenus. Below is the code snippet for counting the elements: export default function App() { const ElementRef ...

The Controller of Conversations

In the HTML code, there is a dialog element with an identification of divMyDialog1. This dialog is connected to a JavaScript class called MyDialog1. Each dialog has its own unique id and associated class name. MyDialog1 = function(divStr){ this.divStr ...

Drop-down menu in a table cell overflowing with lengthy text, causing the table to collapse

I'm facing an issue with my table where I have inputs and selects inside <td>. The problem arises when the text in the options of the <select> element is too long, causing the <td> to expand and disrupt the overall layout of the tabl ...