And here comes another useless but cool HTML 5 effect (See also: Water ripple effect done in HTML 5 and Snow effect done in HTML 5 ), the good old starfield animation you knew and loved from the old Windows 95 screen saver:
The algorithm is incredibly easy, the stars have a position and a size, and you just calculate starPosition += (starPosition – canvasCenter) * starSize and voila, the star moves according to its size (proximity to the screen) and position more or less realistically, no 3D calculations needed. I discovered this algorithm a long time ago while playing around in Flash and now i finally built it in HTML 5.
The star drawing is done by drawing lines, this is slower than using a pre-drawn circle canvas, but it adds a bit more realism to the animation since the line is drawn between the old and newly calculated star position, which creates a much more visible movement of the stars.
Enough yapping, here is the code, just open it in a browser: Starfield animation in HTML5 If you are using this on your website and you are a nice person you can show it by giving me a credit (backlink) somewhere on your page, thank you!
Posted by TimeWaster HTML5 , Programming animation , canvas , drawing , effect , html 5 , html5 , starfield , starfield animation , stars 16 Comments
Alic says: 18.10.2016 (October 18) at 20:52 This is awesome! How did you even come up with this algorithm? Good job! Reply TimeWaster says: 31.12.2016 (December 31) at 01:19 by trying stuff out, i was doing complex calculations first when i noticed that a starfield animation is always moving stars depending on the center point, so i came up with this algorythm by implementing it in a bad way first and then step by step simplifying it. Reply
This is awesome! How did you even come up with this algorithm? Good job!
TimeWaster says: 31.12.2016 (December 31) at 01:19 by trying stuff out, i was doing complex calculations first when i noticed that a starfield animation is always moving stars depending on the center point, so i came up with this algorythm by implementing it in a bad way first and then step by step simplifying it. Reply
by trying stuff out, i was doing complex calculations first when i noticed that a starfield animation is always moving stars depending on the center point, so i came up with this algorythm by implementing it in a bad way first and then step by step simplifying it.
Cassandra says: 31.12.2016 (December 31) at 00:46 Love this. Can I slow the stars down in this one? Would be happy to give you a credit on our website id we use it! Reply TimeWaster says: 31.12.2016 (December 31) at 01:19 yes, on top of the script is a “config” section, just adjust the “flightSpeed” variable. Reply
Love this. Can I slow the stars down in this one? Would be happy to give you a credit on our website id we use it!
TimeWaster says: 31.12.2016 (December 31) at 01:19 yes, on top of the script is a “config” section, just adjust the “flightSpeed” variable. Reply
yes, on top of the script is a “config” section, just adjust the “flightSpeed” variable.
Simon says: 04.04.2017 (April 04) at 23:16 Can you use a random color for the stars, lets say red, blue, yellow, green? Reply TimeWaster says: 05.04.2017 (April 05) at 09:09 yes, search for this line: context.strokeStyle = "rgba(255, 255, 255, " + Math.min(stars[x].size, 1) + ")"; and replace the “255” values (from 0 to 255), the first one is red, then green, then blue. Reply
Can you use a random color for the stars, lets say red, blue, yellow, green?
TimeWaster says: 05.04.2017 (April 05) at 09:09 yes, search for this line: context.strokeStyle = "rgba(255, 255, 255, " + Math.min(stars[x].size, 1) + ")"; and replace the “255” values (from 0 to 255), the first one is red, then green, then blue. Reply
yes, search for this line: context.strokeStyle = "rgba(255, 255, 255, " + Math.min(stars[x].size, 1) + ")"; and replace the “255” values (from 0 to 255), the first one is red, then green, then blue.
Riyan says: 23.05.2018 (May 23) at 03:31 Idk if i’m doing something wrong but when I go to look at the actual code it’s just completely black. Reply TimeWaster says: 02.07.2018 (July 02) at 17:37 I have no clue what you mean. Reply
Idk if i’m doing something wrong but when I go to look at the actual code it’s just completely black.
TimeWaster says: 02.07.2018 (July 02) at 17:37 I have no clue what you mean. Reply
I have no clue what you mean.
Jonathan says: 22.06.2018 (June 22) at 01:51 I’m having trouble… on a particular page of my site (credit will be given, BTW), I could use the starfield to flow FROM the center point instead of following the cursor. Can you advise? Reply TimeWaster says: 02.07.2018 (July 02) at 17:34 Just remove this block of code and the animation will not react to mouse movements anymore: // when mouse moves over canvas change middle point of animation canvas.onmousemove = function(event) { dirX = event.offsetX, dirY = event.offsetY; } Reply
I’m having trouble… on a particular page of my site (credit will be given, BTW), I could use the starfield to flow FROM the center point instead of following the cursor. Can you advise?
TimeWaster says: 02.07.2018 (July 02) at 17:34 Just remove this block of code and the animation will not react to mouse movements anymore: // when mouse moves over canvas change middle point of animation canvas.onmousemove = function(event) { dirX = event.offsetX, dirY = event.offsetY; } Reply
Just remove this block of code and the animation will not react to mouse movements anymore:
// when mouse moves over canvas change middle point of animation canvas.onmousemove = function(event) { dirX = event.offsetX, dirY = event.offsetY; }
Michael Atencio says: 20.07.2018 (July 20) at 21:47 This is cool. Here are a couple of questions as the web has changed since the old days. How can I: make it responsive for a background header video so it works with all devices Change the background to a gradual background color (static) and the stars only come at me without moving where the pointer is. Slow the stars down and make them go from small to bigger towards the edges from a center point Reply TimeWaster says: 03.09.2018 (September 03) at 21:00 since it works with a canvas it has a fixed black background unless you copy in a video or set another background color on every render cycle. take a look here to see how canvas works: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API to not use the mouse pointer look one comment above, i already described it there. to influence the stars play with the variables on top of the script, but be aware that the algorithm mostly defines how they work. Reply
This is cool. Here are a couple of questions as the web has changed since the old days. How can I: make it responsive for a background header video so it works with all devices Change the background to a gradual background color (static) and the stars only come at me without moving where the pointer is. Slow the stars down and make them go from small to bigger towards the edges from a center point
TimeWaster says: 03.09.2018 (September 03) at 21:00 since it works with a canvas it has a fixed black background unless you copy in a video or set another background color on every render cycle. take a look here to see how canvas works: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API to not use the mouse pointer look one comment above, i already described it there. to influence the stars play with the variables on top of the script, but be aware that the algorithm mostly defines how they work. Reply
since it works with a canvas it has a fixed black background unless you copy in a video or set another background color on every render cycle. take a look here to see how canvas works: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API
to not use the mouse pointer look one comment above, i already described it there.
to influence the stars play with the variables on top of the script, but be aware that the algorithm mostly defines how they work.
dee says: 03.09.2018 (September 03) at 01:47 Hi there. Love your work. Could you please advise on how to stop the animation (with the stars still visible but static) after a given time, say 30 seconds? Reply TimeWaster says: 03.09.2018 (September 03) at 21:06 you have to cancel the setInterval timer with a setTimeout timer. you can read how to do that here: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval#Example Reply dee says: 04.09.2018 (September 04) at 01:07 Thanks for your response. I’m really new at this and the link you provided doesn’t make a whole lot of sense to me at this stage. Where in the code would I put the setTimeout ? Reply
Hi there. Love your work. Could you please advise on how to stop the animation (with the stars still visible but static) after a given time, say 30 seconds?
TimeWaster says: 03.09.2018 (September 03) at 21:06 you have to cancel the setInterval timer with a setTimeout timer. you can read how to do that here: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval#Example Reply dee says: 04.09.2018 (September 04) at 01:07 Thanks for your response. I’m really new at this and the link you provided doesn’t make a whole lot of sense to me at this stage. Where in the code would I put the setTimeout ? Reply
you have to cancel the setInterval timer with a setTimeout timer. you can read how to do that here: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval#Example
dee says: 04.09.2018 (September 04) at 01:07 Thanks for your response. I’m really new at this and the link you provided doesn’t make a whole lot of sense to me at this stage. Where in the code would I put the setTimeout ? Reply
Thanks for your response. I’m really new at this and the link you provided doesn’t make a whole lot of sense to me at this stage. Where in the code would I put the setTimeout ?
RevolveR CMS says: 16.09.2018 (September 16) at 14:31 Thank you! it’s very cool! Reply
Thank you! it’s very cool!
Your e-mail address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Save my name, e-mail, and website in this browser for the next time I comment.
This site uses Akismet to reduce spam. Learn how your comment data is processed .
GitHub: Chamfers for OpenSCAD
GitHub: Snake for MicroView
GitHub: TV Screen for MicroView
Thingiverse: Chamfers for OpenSCAD
Thingiverse: Pulley for Geeetech Prusa i3 B+C Idler Belt Tensioner
Devilmastah's Blog
© 2019 TimeWaster's Place