Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="c" width="600px" height="600px"></canvas>
              
            
!

CSS

              
                body {
  background-color: #cb9805;
  background: radial-gradient(
    200% 100% at bottom center,
    #f7f7b6,
    #e96f92,
    #75517d,
    #1b2947
  );
  background: radial-gradient(
    220% 105% at top center,
    #1b2947 10%,
    #75517d 40%,
    #e96f92 65%,
    #f7f7b6
  );
  background-attachment: fixed;
  margin: 0;
  overflow: hidden;
}

canvas{

}

              
            
!

JS

              
                // Define star object that keeps track of x,y position and width
// the 'first' flag that will set the progress of tween to random position
// A reference to tween that will be used for restarting the animation
var Star = function() {
  this.x = 0;
  this.y = 0;
  // random width to stars
  this.width = Math.random() * 3 + 1;
  this.first = true;
  this.tween = null;
};

// adds draw method to our Star object
Star.prototype.draw = function(context) {
  context.moveTo(this.x, this.y);
  context.lineTo(this.x + this.width, this.y);
};

var stars = [];
//var cached = [];
var maxCount = 500;
var count =
  Math.round(window.innerWidth / 5) >= maxCount
    ? maxCount
    : Math.round(window.innerWidth / 5);
var resized = true;

var c = document.getElementById("c");
c.width = window.innerWidth;
c.height = window.innerHeight;
var context = c.getContext("2d");

//Loop through and create star instances
for (var i = 0; i < maxCount; i++) {
  var star = new Star();
  stars.push(star);
}

TweenLite.ticker.addEventListener("tick", gsapTick);

function gsapTick() {
  context.clearRect(0, 0, c.width, c.height);
  context.beginPath();
  context.lineWidth = 2;
  context.strokeStyle = "#fff";

  for (var i = 0; i < count - 1; i++) {
    var star = stars[i];

    if (!star.tween) {
      // Set duration based on count which is based on width
      var duration = count / 20;
      star.tween = new TimelineMax();

      // set initial position to random and substract it by 100 so some stars
      // will appear to move in from top
      star.tween.set(star, { x: 0, y: Math.random() * c.height / 2 - 100 });

      // The tween that will actually animate stars
      star.tween.to(star, duration, {
        // SlowMo ease creates that warping effect as well
        // as stars enter and leave fast but slow down in middle
        ease: SlowMo.ease.config(0.2, 0.2, false),
        x: c.width,
        // Tween star to plus hundred from current y position so it creates that
        // warping effect
        y: "+=100"
      });
      
      // This tween shrinks the width of star and squeezes it in center to create warping effect
      // It repeats once and uses yoyo to true
      // Also note that duration is half of main duration so star squeezes and grows back at end
      star.tween.to(
        star,
        duration / 2,
        { width: star.width / 2, repeat: 1, yoyo: true },
        0
      );

      // If tween is being called for first time a random progress is used
      // so stars appear at random positions in center
      if (star.first) {
        star.tween.progress(Math.random());
        star.first = false;
      }
    } else if (star.tween && !star.tween.isActive()) {
      star.tween.play(0);
    }
    //star.tween.timeScale(count/50);
    stars[i].draw(context);
  }

  context.closePath();
  context.stroke();
}

window.addEventListener("resize", function() {
  // Remove the tick event listener on resize
  TweenLite.ticker.removeEventListener("tick", gsapTick);

  c.width = window.innerWidth;
  c.height = window.innerHeight;
  //calculate new star count based on width
  count =
    Math.round(window.innerWidth / 5) >= maxCount
      ? maxCount
      : Math.round(window.innerWidth / 5);
  //kill all ongoing tweens
  TweenMax.killTweensOf(stars);
  
  // reset all the star objects based on count
  // So it will cause all tweens to be recreated and animation will be responsive
  for (var i = 0; i < count; i++) {
    var star = stars[i];
    star.tween = null;
    star.first = true;
    star.x = 0;
    star.y = 0;
    star.width = Math.random() * 3 + 1;
  }
  // only restart tick event listener after completing reset
  TweenLite.ticker.addEventListener("tick", gsapTick);
});

              
            
!
999px

Console