cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <canvas width="500" height="500" id="canvas"></canvas>
<a href="https://createjs.com/" target="_blank"><img id="logo" src="https://createjs.com/mediakit/createjs-badge-reverse.png"></a>

<img id="badge" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/Button_Click_White.svg">
<img id="badge2" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/Button_Hold_White.svg">

            
          
!
            
              canvas, body { background-color: #000; cursor:pointer; }
html, body { margin: 0; padding: 0; overflow: hidden; }
#logo { 
	position: absolute;
  right:0px;
  bottom:0px;
}

#badge,
#badge2 {
  width:60px;
  height:70px;
  position: absolute;
  left:10px;
  bottom:10px;
  cursor: default;
}
#badge2 {
  left: 80px;
}
            
          
!
            
              var stage = new createjs.Stage("canvas");
createjs.Ticker.timingMode = createjs.Ticker.RAF_SYNC;
createjs.Ticker.framerate = 30;
createjs.Ticker.on("tick", update);
stage.autoClear = false;
createjs.Touch.enable(stage);

let snakes = [],
    GRID = 20,
    cols = 20,
    rows = 20,
    DIRS = [0,1,2,3],
    MAX_SNAKE = 500,
    MAX_SNAKES = 10,
    cont = stage.addChild(new createjs.Container());

window.addEventListener("resize", handleResize);
function handleResize(e) {
  var w = window.innerWidth,
      h = window.innerHeight;
  stage.canvas.width = w;  
  stage.canvas.height = h;
  
  GRID = Math.max(10, Math.max(w,h) / 100 | 0);
  cols = (w / GRID | 0);
  rows = (h / GRID | 0);
  bg.rectCmd.w = w; bg.rectCmd.h = h;
  stage.update();
};

function update(e) {  
  activeSnake && (activeSnake.length = Math.min(MAX_SNAKE, activeSnake.length+1));
  for (var i=snakes.length-1; i>=0; i--) {
    updateSnake(snakes[i]);  
  }
  stage.update(e);
}

// Get a theme color.
var dark = 55, 
    hue = (Math.random()*220|0) + 140,
    color = createjs.Graphics.getHSL(hue, 100, 2),
    bg = cont.addChild(new createjs.Shape().set({alpha:0.2}));
bg.graphics.f(color);
bg.rectCmd = bg.graphics.dr(0,0,stage.canvas.width,stage.canvas.height).command;
stage.canvas.style.backgroundColor = document.body.style.backgroundColor = color;

// Update size
handleResize();

var activeSnake;
stage.on("stagemousedown", function() {
  activeSnake = addSnake();
});
stage.on("stagemouseup", function() {
  activeSnake = null; 
});

// Add a new snake
function addSnake(center) {
  
  let snake;
  if (snakes.length >= MAX_SNAKES) {
    snake = snakes[Math.random() * snakes.length |0];
    snake.dead = true;
    clearInterval(snake.grow);
  }
  
  if (center != true && Math.random() > 0.95) {
    dark = 90;
  }
  var offset = Math.random()*5|0 * 5 + 10;
  snake = {
    sprite: cont.addChildAt(new createjs.Shape().set({x:offset,y:offset}), 1),
    path: [new createjs.Point(stage.mouseX/GRID|0, stage.mouseY/GRID|0)],
    color: createjs.Graphics.getHSL(hue + Math.random()*20-10, 100, dark),
    length: 1,
    size: (70-dark)/5
  };
  snake.sprite.size = snake.size;
  if (center == true) {
    snake.length = (Math.random() * 20|0) + 3;
    snake.path[0].setValues(cols>>1, rows>>1); 
  }
  snake.grow = setInterval(function() {
    snake.length = Math.min(MAX_SNAKE, snake.length + 1);
  }, (Math.random() * 20 + 5 | 0) * 1000);
  snakes.push(snake);
  dark = Math.random() * 40 + 10;
  
  cont.children.sort(function(a,b) {
    return b.size-a.size;
  });
  
  return snake;
}

// Add one for fun!
addSnake(true);
addSnake(true);

// Move through the grid.
function updateSnake(snake) {
  let path = snake.path,
      pos = path[0],
      newPos = pos.clone(),
      dirs = DIRS.slice(0);
  
  loop: while (true) {
    if (dirs.length == 0) { 
      // Stuck outside with no move
      if (pos.x > cols || pos.y > rows) {
        pos.setValues(cols>>1, rows>>1);
        path = snake.path = [pos];
      } else {
        // Wait for snake to catch up
        break loop; 
      }
    }
    // Start from the previous step
    newPos.setValues(pos.x, pos.y);
    let index = Math.random() * dirs.length | 0,
        dir = dirs[index];
    dirs.splice(index, 1)[0];
    // Go in one direction
    switch (dir) {
      case 0: newPos.x++; break;
      case 1: newPos.x--; break;
      case 2: newPos.y++; break;
      case 3: newPos.y--; break;
    }
    
    // Can't go backwards
    if (newPos.x < 0 || newPos.y < 0 || newPos.x >= cols || newPos.y >= rows) { continue loop; }
    // Can't go over top
    for (let i=0, l=path.length; i<l; i++) {
      let p = path[i];
      if (newPos.x == p.x && newPos.y == p.y) { continue loop; }
    }
    break loop;
  }

  // Add the new point
  path.unshift(newPos);
  // Keep the snake the right length
  if (path.length > snake.length) { path = snake.path = path.slice(0, snake.length); }
  if (snake.dead) { 
    snake.length--;
    if (snake.length == -1) {
      var index = snakes.indexOf(snake);
      if (index > -1) {
        snakes.splice(index, 1);
        stage.removeChild(snake);
        clearInterval(snake.grow);
      }
      return;
    }
  }
  // Draw the snake again
  snake.sprite.graphics.clear()
    .ss(snake.size)
    .s(snake.color).mt(newPos.x * GRID, newPos.y * GRID);
  path.forEach(function (p) {
    snake.sprite.graphics.lt(p.x * GRID, p.y * GRID);
  });
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console