Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:19:30+00:00 2026-06-06T23:19:30+00:00

I decided to try and learn more about HTML5 canvas and build a page

  • 0

I decided to try and learn more about HTML5 canvas and build a page with the background as falling snow. I’d like to be able to do this dynamically (I know how to make objects move on canvas and etc. and could do this manually). What I mean by this is that I’d like to simply change a variable that controls how many snowflakes there are on the page at one time and not have to copy and paste code. I’m not 100% sure if my syntax is right for some of the objects and loops, either.

I tried looking for a tutorial to walk me through this the first time and couldn’t find any on how to make snow. If anyone happens to know of one and would give a link that would be great. Here is my code so far.

<!DOCTYPE html>
<html>
<head>
<title>Obese</title>
<style type="text/css">
    body
{
     background-color:black;
    }
</style>
</head>
<body>
<script type="text/javascript">
    function vp(woh)
    {
        var viewportwidth;
        var viewportheight;

        if (typeof window.innerWidth != 'undefined')
        {
             viewportwidth = window.innerWidth,
             viewportheight = window.innerHeight
         }

         else if (typeof document.documentElement != 'undefined'
             && typeof document.documentElement.clientWidth !=
             'undefined' && document.documentElement.clientWidth != 0)
         {
             viewportwidth = document.documentElement.clientWidth,
             viewportheight = document.documentElement.clientHeight
         }

         else
         {
               viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
               viewportheight = document.getElementsByTagName('body')[0].clientHeight
         }
            if (woh == 'w')
            {
                return viewportwidth;
            }
            else if (woh == 'h')
            {
                return viewportheight;
            }
            else
            {
                return false;
            }

        }
    function snowflake()
    {
        this.x = Math.random() * canvas.width;
        this.y = 0;
        var toberadius = Math.random() * 50;
        this.radius = toberadius + 10;
        this.color = "white";
        var tobefallingSpeed = Math.random() * 100;
        this.fallingSpeed = tobefallingSpeed + 30;
        numberofSnowflakes++;
    }
    function update(m)
    {
        if (snowflakes < numberofSnowflakes)
        {
            for (i=0;i<numberofSnowflakes;i++)
            {
                sf[i] = new snowflake();
                snowflakes = i;
            }
        }
        for (c=0;c<snowflakes;c++)
        {
            sf[m].y += sf[m].fallingSpeed * m;
        }
    }
    function render()
    {
        for (b=0;b<snowflakes;b++)
        {
            ctx.fillStyle = "#FFFFFF";
            ctx.fillRect(sf[b].x,sf[b].y,sf[b].radius,sf[b].radius);
        }
    }
    function main()
    {
        alert('asdf');
        now = Date.now();
        delta = now - then;
        update(delta/1000);
        render();
        then = now;
    }
    then = Date.now();
    var int = self.setInterval("main()",1);
    var canvas = document.createElement("canvas");
    var ctx = canvas.getContext("2d");
    canvas.width = vp('w');
    canvas.height = vp('h');
    document.body.appendChild(canvas);

    var numberofSnowflakes = 50;
    var snowflakes = 0;
    var sf = new Object();


</script>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:19:32+00:00Added an answer on June 6, 2026 at 11:19 pm

    As I mentioned in the comments, I actually created my own snow plugin thats been used on a few websites, Blog post and Github. So needless to say Im a fan of this effect :).

    The code below should get you started.

    Working Version

     function vp(woh)
        {
            var viewportwidth;
            var viewportheight;
    
            if (typeof window.innerWidth != 'undefined')
            {
                 viewportwidth = window.innerWidth,
                 viewportheight = window.innerHeight
             }
    
             else if (typeof document.documentElement != 'undefined'
                 && typeof document.documentElement.clientWidth !=
                 'undefined' && document.documentElement.clientWidth != 0)
             {
                 viewportwidth = document.documentElement.clientWidth,
                 viewportheight = document.documentElement.clientHeight
             }
    
             else
             {
                   viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
                   viewportheight = document.getElementsByTagName('body')[0].clientHeight
             }
                if (woh == 'w')
                {
                    return viewportwidth;
                }
                else if (woh == 'h')
                {
                    return viewportheight;
                }
                else
                {
                    return false;
                }
    
            }
        function snowflake()
        {
            this.x = Math.random() * canvas.width;
            this.y = Math.random() * canvas.height;
            this.radius = Math.random()*2;
            this.color = "white";
            var tobefallingSpeed = Math.random() * 100;
            this.fallingSpeed = tobefallingSpeed + 30;
        }
        function render()
        {
            ctx.clearRect(0,0,canvas.width, canvas.height);
            for (b=0;b<snowflakes;b++)
            {  
                sf[b].y+=0.4;
                if(sf[b].y> canvas.height){
                 sf[b].y = 0;   
                }
                ctx.fillStyle = "#FFFFFF";
                ctx.fillRect(sf[b].x,sf[b].y,sf[b].radius,sf[b].radius);
            }
        }
        function main()
        {
            now = Date.now();
            delta = now - then;
            render();
            then = now;
        }
        then = Date.now();
        var int = self.setInterval(main,1);
        var canvas = document.createElement("canvas");
        var ctx = canvas.getContext("2d");
        canvas.width = vp('w');
        canvas.height = vp('h');
        document.body.appendChild(canvas);
    
        var numberofSnowflakes = 100;
        var sf = [];
    
        for (i=0;i<numberofSnowflakes;i++)
        {
            sf[i] = new snowflake();
            snowflakes = i;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my self-imposed quest to learn Netbeans, I decided to try out an already
I decided to try to my hand at this, and have had a somewhat
After failing to achieve this with link_to remote, I decided to try jQuery way.
I'm coming from a TortoiseSVN background and decided to give TortoiseHg a try. One
I recently decided to try to learn some bash scripting and as a fun
I've decided that I am going to try and learn backbone.js by making a
I'm trying to learn servlets and JSP and would like to do this with
Decided to try and learn Perl, and currently need to process a number of
So a few days ago I decided I would try and learn Ruby and
I decided to try to mess around with Git today and try to learn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.