Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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="canvas"></canvas>
              
            
!

CSS

              
                canvas {
  background: #000;
  width: 100%;
  height: 100%;
}
              
            
!

JS

              
                var canvas = document.getElementById("canvas"),
  ctx = canvas.getContext("2d");

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

var stars = [], // Array that contains the stars
  FPS = 60, // Frames per second
  number = 300, // Number of stars
  mouse = {
    x: 0,
    y: 0
  }; // mouse location

// Push stars to array
for (var i = 0; i < number; i++) {
  stars.push({
    x: Math.random() * canvas.width,
    y: Math.random() * canvas.height,
    radius: Math.random() * 1 + 1,
    vx: Math.floor(Math.random() * 50) - 25,
    vy: Math.floor(Math.random() * 50) - 25,
    speed: Math.floor(Math.random() * 51) + 10
  });
}

function drawStars() {
  stars.map((star) => {
    ctx.beginPath();
    ctx.fillStyle = "#fff";
    ctx.arc(star.x, star.y, star.radius, 0, 2 * Math.PI);
    ctx.fill();
    // ctx.fillStyle = 'red';
    // ctx.stroke();
  });
}

function drawLines() {
  stars.map((starI) => {
    ctx.moveTo(starI.x, starI.y);
    // distance can connect from mouse to dots
    if (distance(mouse, starI) < 250) ctx.lineTo(mouse.x, mouse.y);

    stars.map((starII) => {
      // distance can connect between the dots
      if (distance(starI, starII) < 50) ctx.lineTo(starII.x, starII.y);
    });
  });
  ctx.lineWidth = 0.1;
  ctx.strokeStyle = "white";
  ctx.stroke();
}

// Draw the scene
function draw() {
  // Draw background
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  ctx.globalCompositeOperation = "lighter";

  // Draw the stars
  drawStars();

  // Draw line connecting star and mouse
  ctx.beginPath();
  drawLines();
}

// Update star locations
function update() {
  stars.map((star) => {
    // star.x += star.vx / FPS;
    // star.y += star.vy / FPS;
    star.x += star.vx / star.speed;
    star.y += star.vy / star.speed;

    if (star.x < 0 || star.x > canvas.width) star.vx = -star.vx;
    if (star.y < 0 || star.y > canvas.height) star.vy = -star.vy;
  });
}

canvas.addEventListener("mousemove", function (e) {
  mouse.x = e.clientX;
  mouse.y = e.clientY;
});

function distance(point1, point2) {
  var xs = 0,
    ys = 0;

  xs = (point2.x - point1.x) ** 2;
  ys = (point2.y - point1.y) ** 2;

  return Math.sqrt(xs + ys);
}

// Update and draw
function tick() {
  draw();
  update();
  requestAnimationFrame(tick);
}

tick();

              
            
!
999px

Console