Flash Script – Star Field Effect

Share this article

Creating a starfield effect is easy! To begin, download the sample files here.

1. Create a small star shape, and convert it to a movie clip. Give it an instance name of star.

2. Extend the existing layer by three frames. Then, create a new layer above the existing layer, and create three blank frames in this layer.

3. In the first layer, insert the actions:

numstars = 100; 
speed = 5;
star_field = new Array (numstars);
//x is x axis, y is y axis, speed is speed of star
function star(x,y,z)
{
 this.x = x;
 this.y = y;
 this.speed = speed;
}
for (i=0;i<numstars;i++)
{
star_field[i] = new star(random(300),random(200),random(speed)+1);
duplicateMovieClip ("star", "star_" add i, i+10);
}

4. In the second key frame, insert this action:

for (i=0;i<numstars;i++) 
{
star_field[i].x+= star_field[i].speed;
if (star_field[i].x>300) { star_field[i].x = 0; }
setProperty ("star_" add i , _x , star_field[i].x )
setProperty ("star_" add i , _y , star_field[i].y )
setProperty ("star_" add i , _alpha , star_field[i].speed*40 )
}

5. Finally, in the third, insert:

gotoAndPlay (2);

That’s it! Test the movie!

Frequently Asked Questions about Star Field Effect

How can I create a star field effect using JavaScript?

Creating a star field effect using JavaScript involves creating a canvas, defining the stars, and then animating them. First, you need to create a canvas element in your HTML and get its context in your JavaScript file. Then, you can define the stars as an array of objects, each with its own properties such as size, speed, and position. Finally, you can animate the stars by updating their positions in a loop and redrawing them on the canvas. This creates the illusion of movement, giving the star field effect.

Can I create a star field effect using CSS?

Yes, you can create a star field effect using CSS. This involves creating a div element for each star and using CSS animations to move them around. The keyframes rule in CSS allows you to create animations by changing the style of an element at different points in time. You can use this to animate the position, size, and color of the stars to create a star field effect.

How can I add a blur effect to my star field?

Adding a blur effect to your star field can be achieved using the CSS filter property. This property allows you to apply graphical effects such as blurring to an element. You can use the blur() function with a value in pixels to specify the amount of blur. For example, filter: blur(2px) would apply a blur effect of 2 pixels.

How can I make my star field responsive?

Making your star field responsive involves adjusting its size and the number of stars based on the size of the viewport. You can use the window.innerWidth and window.innerHeight properties in JavaScript to get the size of the viewport, and then use these values to set the size of your canvas and the number of stars. You can also add an event listener for the resize event to update these values when the size of the viewport changes.

How can I add a flashlight effect to my star field?

Adding a flashlight effect to your star field involves creating a light source that illuminates the stars within a certain radius. You can do this by creating a radial gradient in your canvas context, with the center of the gradient representing the light source. You can then use the globalCompositeOperation property to set the blending mode to ‘lighter’, which will make the stars within the gradient appear brighter.

Why are my stars not appearing on the canvas?

If your stars are not appearing on the canvas, there could be several reasons. One possibility is that the stars are being drawn outside the bounds of the canvas. Make sure that the positions of the stars are within the width and height of the canvas. Another possibility is that the color of the stars is the same as the background color of the canvas. Make sure that the stars have a contrasting color to the background.

How can I control the speed of the star field animation?

The speed of the star field animation can be controlled by adjusting the amount by which the positions of the stars are updated in each frame. A larger value will result in a faster animation, while a smaller value will result in a slower animation. You can also control the speed by adjusting the frame rate of the animation, which can be done using the setInterval() function in JavaScript.

Can I create a 3D star field effect?

Yes, you can create a 3D star field effect by giving the stars a sense of depth. This can be achieved by varying the size and speed of the stars based on their z-coordinate. Stars with a higher z-coordinate should be smaller and move slower, while stars with a lower z-coordinate should be larger and move faster. This creates the illusion of depth, giving the star field a 3D effect.

How can I add a twinkling effect to my stars?

Adding a twinkling effect to your stars can be achieved by randomly changing their size and opacity. You can use the Math.random() function in JavaScript to generate random values for these properties, and then use CSS animations to transition between these values. This creates the illusion of the stars twinkling.

Can I use the star field effect for a background?

Yes, you can use the star field effect for a background. You can set the canvas element to cover the entire viewport and position it behind your content using the z-index property in CSS. You can also set the background-color property of the canvas to a dark color to create the illusion of a night sky.

Georgina LaidlawGeorgina Laidlaw
View Author

Georgina has more than fifteen years' experience writing and editing for web, print and voice. With a background in marketing and a passion for words, the time Georgina spent with companies like Sausage Software and sitepoint.com cemented her lasting interest in the media, persuasion, and communications culture.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week