Is there a way to modify the snow script to alter the snow color?

I've been experimenting with a script to create a unique effect, inspired by a js fiddle I found online. The original snow effect has been modified to increase speed, change direction, and more.

My goal is to have "snow flakes" in multiple colors (about 4-7) instead of just white. Is there a simple way to achieve this using the existing js fiddle?

Link to the JS Fiddle

Any assistance on this matter would be greatly appreciated! Thank you for your time.

canvas {
    background:#183794;
    margin-bottom:10px;
     -ms-transform: rotate(180deg); /* IE 9 */
    -webkit-transform: rotate(180deg); /* Chrome, Safari, Opera */
    transform: rotate(180deg);
}

Answer №1

The previous responses are accurate, however, there is a drawback as the snowflake's color changes with each redraw, which may not be ideal.

Presented below is an improved version where each snowflake retains its assigned color throughout:

APP.snowflake = function(settings) {
    this.context = settings.context;
    this.x = settings.x || 30;
    this.y = settings.y || 30;
    this.radius = settings.radius || 10;
    this.resistance = settings.resistance || 1;
    this.speed = settings.speed || 0;
    this.velocityx = 0;
    this.velocityy = 0;
    this.color = this.getRandomColor();
};

APP.snowflake.prototype.drawSelf = function () {
    // Display the Snowflake in its designated color and position.
    this.context.fillStyle = this.color;
    this.context.beginPath();
    this.context.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
    this.context.closePath();
    this.context.fill();
};

APP.snowflake.prototype.getRandomColor = function() {
    colors = ['#F5A9A9', '#FFBF00', '#FFFFFF', '#FF0040', '#58FA58'];
    selected_color = colors[Math.floor(Math.random()*colors.length)];
    return selected_color;
};

Explore the revised functionality here: http://jsfiddle.net/ry8vLhw4/

Answer №2

Here is the function responsible for creating the snowflakes.

var colors = ["#ffffff", "silver", "green"];
APP.snowflake.prototype.drawSelf = function () {
    // Draws the Snowflake with a randomly selected color.
    this.context.fillStyle = colors[Math.floor(Math.random() * colors.length)];
    this.context.beginPath();
    this.context.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
    this.context.closePath();
    this.context.fill();
};

If you want to use different colors, simply add them to the 'colors' array and let it randomly choose from there.

The key line here is

this.context.fillStyle = colors[Math.floor(Math.random() * colors.length)];

Check out the updated Fiddle below:

http://jsfiddle.net/7wsetqqa/36/

Answer №3

The snowflake's color is determined within the drawSelf function.

APP.snowflake.prototype.drawSelf = function () {
    // Draw the Snowflake at its new position.
    this.context.fillStyle = "#ffffff";
    this.context.beginPath();
    this.context.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
    this.context.closePath();
    this.context.fill();
};

To change the color of the snowflake, modify the fillStyle.

Check out this example.

Answer №4

insert this line right after line 216:

this.color = '#'+Math.floor(Math.random()*16777215).toString(16)

then modify line 221 from:

this.context.fillStyle = "#ffffff";

to

this.context.fillStyle = this.color;

that's all. Check it out here: http://jsfiddle.net/7wsetqqa/40/

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

Adjust the height of a DIV element using Jquery Resizable to a minimum height of 1px, smaller than its default value

Having an issue with the Jquery UI Resizable functionality. I've implemented Jquery resizable to adjust a div's width and height dynamically. It's been working well, but I'm encountering a problem when attempting to decrease the height ...

Modify the CSS class by adding attributes dynamically instead of directly to the element

I encountered a situation where I needed to apply a css property to a class rather than an element. For example: $('.class_with_css').css({display:'none'}); This code would add the style "display:none" to all elements with the cl ...

How can I sync changes between two variables in node.js?

Is there a method to create a shared variable in JavaScript? Here is an example of what I am attempting to achieve: var id = 5; var player = new Player(id); var array1[0] = player; var array2[0] = player; array1[0].id = 8 console.log(array1[0]); // ...

Enter key triggering input change event is unresponsive in Internet Explorer

$("#inputBoxWidth").change(function() { var curValue = $("#inputBoxWidth").val(); alert(curValue); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="text" id="inputBo ...

What is the alternative to using async:false in order to make ajax synchronous?

I'm experiencing an issue with the asynchronous nature of AJAX. I have an array and I am looping through it to make AJAX calls. When the AJAX call is successful, it returns an XML string which I use to populate a table and update a progress bar. I had ...

What could be causing the issues with a deployed AngularJS + RequireJS application that has been concatenated and minified?

Check out my GitHub project (it's a small example). Feel free to explore any files I'm referring to. My application works fine without minifying JS/CSS. Routes and controllers function as expected. However, when I deploy the application, it sto ...

The AreaChart in Google is displaying incorrect dates on the axis

I have encountered an issue that I am struggling to resolve. I am in the process of creating a Google Area Chart using a JSON response from a server, specifically with date type columns. Below is the JSON data obtained from the server (copy/paste), organi ...

The functionality of an Ajax call is limited to a single use, regardless of using

For the past couple of weeks, I've been struggling to get my Ajax call to function more than once. As a self-taught individual, I've been trying my best to troubleshoot this issue on my own, but it's really getting to me. Some others facing ...

Having trouble monitoring the default close button on a desktop window

Currently working on a windows desktop app that utilizes node.js and backbone.js. I'm looking to trigger a specific action when the user closes the application either by clicking the close button on the title bar or right-clicking on the app in the Wi ...

sum inside the while loop

Below is the provided HTML: <form> <textarea id="input1"></textarea> <textarea id="input2"></textarea> <span></span> </form> The following JavaScript code is included: $("#input2").keyup{ var a = ...

Structure of Sencha Touch application

I'm a novice with Sencha Touch and am currently developing a basic application that includes a login form and displays results in lists. My main dilemma is about the structure of the application. Would it be best to have everything contained within a ...

What is the best way to implement CSS Float in typescript programming?

For a specific purpose, I am passing CSS Float as props. To achieve this, I have to define it in the following way: type Props = { float: ???? } const Component = ({ float }: Props) => {......} What is the most effective approach to accomplish this? ...

The occurrence of an unforeseen symbol 'a' was encountered

After using jQuery to create JSON data, the following structure was produced: [{"name":"date","value":"24-05-2013"},{"name":"omschrijving","value":""}] Although this JSON is valid, I encountered an error when trying to process it with jQuery: An unexp ...

Activate the class when clicked

I am facing a minor issue with the JavaScript onClick function. I need a function that will target a specific class, like .modal. Currently, it looks like this <div class="modal" onclick="modalFix()"> <p>here is some text</p> </div> ...

When an array is added to a Json Object in Jquery, the result is an array of arrays instead of a single array of values

Struggling with adding an array to a JSON object from two separate select lists, resulting in nested arrays instead of one unified array. Let me elaborate: The desired output is achieved when two items are selected from a single select list. In this scena ...

Issue with alignment: Safari browser on MacBook displaying Bootstrap buttons all the way to the right side of the screen

I recently viewed one of my websites on Safari and noticed that the buttons were being pushed to the far right of the screen (between 100-200%), causing unnecessary horizontal scrolling, while everything appeared as intended on Google Chrome. Here is a sni ...

bootstrap thumbnail displayed without a border

I have decided to incorporate Bootstrap into my latest project: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS ...

Struggling with passing data to a child component in React

I am currently working on a gallery project where users can click on an image to view it in a separate component using props. The images are sourced from a hardcoded array, and the challenge lies in connecting this data to the component accurately. I have ...

What is the process for building a series of HTML elements using jQuery?

Is it possible to create a moving conveyor belt of html elements (such as divs) using jQuery, and if so, how can this be done? For example: (as shown above, rectangles move from left to right; new ones are added and the last one is removed) And then: [ ...

Tips for preserving the status of a sidebar

As I work on developing my first web application, I am faced with a navigation challenge involving two menu options: Navbar Sidebar When using the navbar to navigate within my application, I tend to hide the sidebar. However, every ti ...