cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <div class="container">
  <canvas id="canvas" width=800 height=40 "></canvas>
                       </div>
            
          
!
            
              .container {
  width: 100%;
  margin-top: 5%;
  padding: 0;
}
canvas {
  display: block;
  margin: auto;
  border: 2px solid #1a1a1a;
  border-radius: 5px;
}

body {
  background: black;
  padding-top: 50px;
}

            
          
!
            
              var c = document.getElementById("canvas");
c.style.cursor = 'none';
var ctx = c.getContext("2d");
var frame = 0;
var numFrames = 15;
var scale = c.height / 10;
var hCenter = c.width / 2 - scale / 2;
var lemArray = [];
//Behold!  The entire CGA color palette!
var blk = '#000000'; //black
var wht = '#ffffff'; // white
var lgy = '#aaaaaa'; // light gray
var dgy = '#555555'; // dark gray
var yel = '#ffff55'; // yellow
var brn = '#aa5500'; // brown
var lrd = '#ff5555'; // light red
var drd = '#aa0000'; // dark red
var lgr = '#55ff55'; // light green
var dgr = '#00aa00'; // dark green
var lcy = '#55ffff'; // light cyan
var dcy = '#00aaaa'; // dark cyan
var lbu = '#5555ff'; // light blue
var dbu = '#0000aa'; // dark blue
var lmg = '#ff55ff'; // light magenta
var dmg = '#aa00aa'; // dark magenta

function blocker(x) {
  this.kind = "blocker";
  this.x = x;
  this.frame = 0;
  this.sprite = [
    [0, 0, 0, 3, 3, 3, 0, 0, 0, 0],
    [0, 0, 3, 3, 3, 3, 3, 0, 0, 0],
    [0, 0, 3, 3, 1, 1, 1, 0, 0, 0],
    [1, 0, 0, 0, 1, 1, 0, 0, 0, 1],
    [1, 1, 1, 1, 2, 2, 1, 1, 1, 1],
    [0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
    [0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
    [0, 0, 0, 2, 2, 2, 2, 0, 0, 0],
    [0, 0, 0, 2, 0, 0, 2, 0, 0, 0],
    [0, 0, 1, 1, 0, 0, 1, 1, 0, 0]
  ];
  this.animate = function() {
    switch (this.frame) {
      case 0:
        this.sprite[0] = [0, 0, 0, 3, 3, 3, 0, 0, 0, 0];
        this.sprite[1] = [0, 0, 3, 3, 3, 3, 3, 0, 0, 0];
        this.sprite[2] = [0, 0, 3, 3, 1, 1, 1, 0, 0, 0];
        this.sprite[8] = [0, 0, 0, 2, 0, 0, 2, 0, 0, 0];
        this.sprite[9] = [0, 0, 1, 1, 0, 0, 1, 1, 0, 0];
        break;
      case 1:
        this.sprite[0] = [0, 0, 0, 0, 3, 3, 0, 0, 0, 0];
        this.sprite[1] = [0, 0, 3, 3, 3, 3, 3, 0, 0, 0];
        this.sprite[2] = [0, 0, 3, 3, 3, 1, 1, 0, 0, 0];
        break;
      case 2:
        this.sprite[0] = [0, 0, 0, 0, 3, 3, 0, 0, 0, 0];
        this.sprite[1] = [0, 0, 0, 3, 3, 3, 3, 0, 0, 0];
        this.sprite[2] = [0, 0, 3, 3, 3, 1, 1, 0, 0, 0];
        this.sprite[8] = [0, 0, 0, 2, 0, 0, 2, 1, 0, 0];
        this.sprite[9] = [0, 0, 1, 1, 0, 0, 1, 0, 0, 0];
        break;
      case 3:
        this.sprite[0] = [0, 0, 0, 0, 3, 3, 0, 0, 0, 0];
        this.sprite[1] = [0, 0, 0, 3, 3, 3, 3, 0, 0, 0];
        this.sprite[2] = [0, 0, 0, 3, 3, 1, 1, 0, 0, 0];
        break;
      case 4:
        this.sprite[8] = [0, 0, 0, 2, 0, 0, 2, 0, 0, 0];
        this.sprite[9] = [0, 0, 1, 1, 0, 0, 1, 1, 0, 0];
        break;
      case 5:
        break;
      case 6:
        this.sprite[0] = [0, 0, 0, 0, 3, 3, 0, 0, 0, 0];
        this.sprite[1] = [0, 0, 0, 3, 3, 3, 3, 0, 0, 0];
        this.sprite[2] = [0, 0, 0, 3, 1, 1, 3, 0, 0, 0];
        this.sprite[8] = [0, 0, 0, 2, 0, 0, 2, 1, 0, 0];
        this.sprite[9] = [0, 0, 1, 1, 0, 0, 1, 0, 0, 0];
        break;
      case 7:
        break;
      case 8:
        this.sprite[0] = [0, 0, 0, 0, 3, 3, 3, 0, 0, 0];
        this.sprite[1] = [0, 0, 0, 3, 3, 3, 3, 3, 0, 0];
        this.sprite[2] = [0, 0, 0, 1, 1, 1, 3, 3, 0, 0];
        this.sprite[8] = [0, 0, 0, 2, 0, 0, 2, 0, 0, 0];
        this.sprite[9] = [0, 0, 1, 1, 0, 0, 1, 1, 0, 0];
        break;
      case 9:
        this.sprite[0] = [0, 0, 0, 0, 3, 3, 0, 0, 0, 0];
        this.sprite[1] = [0, 0, 0, 3, 3, 3, 3, 3, 0, 0];
        this.sprite[2] = [0, 0, 0, 1, 1, 3, 3, 3, 0, 0];
        break;
      case 10:
        this.sprite[0] = [0, 0, 0, 0, 3, 3, 0, 0, 0, 0];
        this.sprite[1] = [0, 0, 0, 3, 3, 3, 3, 0, 0, 0];
        this.sprite[2] = [0, 0, 0, 1, 1, 3, 3, 3, 0, 0];
        this.sprite[8] = [0, 0, 0, 2, 0, 0, 2, 1, 0, 0];
        this.sprite[9] = [0, 0, 1, 1, 0, 0, 1, 0, 0, 0];
        break;
      case 11:
        this.sprite[0] = [0, 0, 0, 0, 3, 3, 0, 0, 0, 0];
        this.sprite[1] = [0, 0, 0, 3, 3, 3, 3, 0, 0, 0];
        this.sprite[2] = [0, 0, 0, 1, 1, 3, 3, 0, 0, 0];
        break;
      case 12:
        this.sprite[8] = [0, 0, 0, 2, 0, 0, 2, 0, 0, 0];
        this.sprite[9] = [0, 0, 1, 1, 0, 0, 1, 1, 0, 0];
        break;
      case 13:
        break;
      case 14:
        this.sprite[0] = [0, 0, 0, 0, 3, 3, 0, 0, 0, 0];
        this.sprite[1] = [0, 0, 0, 3, 3, 3, 3, 0, 0, 0];
        this.sprite[2] = [0, 0, 0, 3, 1, 1, 3, 0, 0, 0];
        this.sprite[8] = [0, 0, 0, 2, 0, 0, 2, 1, 0, 0];
        this.sprite[9] = [0, 0, 1, 1, 0, 0, 1, 0, 0, 0];
        break;
      case 15:
        break;
    }

    for (var i = 0; i < 10; i++) {
      for (var j = 0; j < 10; j++) {
        switch (this.sprite[i][j]) {
          case 0:
            ctx.fillStyle = blk;
            break;
          case 1:
            ctx.fillStyle = wht;
            break;
          case 2:
            ctx.fillStyle = lbu;
            break;
          case 3:
            ctx.fillStyle = dgr;
            break;
        }

        ctx.fillRect((scale * j) + this.x, scale * i, scale, scale);
      }
    }

    if (this.frame < numFrames) {
      this.frame++;
    } else if (this.frame == numFrames) {
      this.frame = 0;
    }

  };

  lemArray.push(this);
}

function walker(x, dr) {
  this.kind = "walker";
  this.frame = 0;
  this.x = x;
  this.dr = dr;
  this.sprite = [

    [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 3, 3, 3, 3, 0, 0, 0],
      [0, 0, 0, 3, 3, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
      [0, 0, 0, 0, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
      [0, 0, 0, 1, 2, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]
    ],
    [
      [0, 0, 0, 0, 3, 0, 3, 0, 0, 0], //frame 1
      [0, 0, 0, 3, 3, 3, 0, 0, 0, 0],
      [0, 0, 0, 3, 3, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
      [0, 0, 0, 0, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 1, 2, 2, 0, 0, 0, 0],
      [0, 0, 0, 1, 2, 2, 0, 1, 0, 0],
      [0, 0, 0, 0, 2, 2, 0, 1, 0, 0],
      [0, 0, 0, 2, 2, 0, 1, 0, 0, 0],
      [0, 0, 0, 1, 1, 0, 0, 0, 0, 0]
    ],
    [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], //frame 2
      [0, 0, 0, 3, 0, 3, 0, 0, 0, 0],
      [0, 0, 0, 3, 3, 3, 0, 0, 0, 0],
      [0, 0, 0, 0, 3, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
      [0, 0, 0, 1, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 1, 2, 2, 0, 0, 0, 0],
      [0, 0, 1, 1, 2, 2, 2, 0, 0, 0],
      [0, 0, 0, 2, 2, 2, 2, 0, 0, 0],
      [0, 0, 1, 1, 0, 0, 1, 1, 0, 0]
    ],
    [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], //frame 3
      [0, 0, 0, 0, 3, 3, 0, 0, 0, 0],
      [0, 0, 0, 3, 3, 1, 3, 0, 0, 0],
      [0, 0, 0, 3, 1, 1, 1, 0, 0, 0],
      [0, 0, 0, 0, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 1, 2, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
      [0, 0, 1, 2, 2, 2, 2, 0, 0, 0],
      [0, 0, 1, 0, 0, 1, 1, 0, 0, 0]
    ],
    [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], //frame 4
      [0, 0, 0, 3, 3, 3, 3, 0, 0, 0],
      [0, 0, 0, 3, 3, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
      [0, 0, 0, 0, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 2, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
      [0, 0, 0, 1, 2, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 1, 0, 0, 0, 0]
    ],
    [
      [0, 0, 0, 0, 3, 0, 3, 0, 0, 0], // frame 5
      [0, 0, 0, 3, 3, 3, 0, 0, 0, 0],
      [0, 0, 0, 3, 3, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
      [0, 0, 0, 0, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 2, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 2, 1, 0, 1, 0, 0],
      [0, 0, 0, 0, 2, 2, 0, 1, 0, 0],
      [0, 0, 0, 2, 2, 0, 1, 0, 0, 0],
      [0, 0, 0, 1, 1, 0, 0, 0, 0, 0]
    ],
    [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // frame 6
      [0, 0, 0, 3, 0, 3, 0, 0, 0, 0],
      [0, 0, 0, 3, 3, 3, 0, 0, 0, 0],
      [0, 0, 0, 0, 3, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
      [0, 0, 0, 0, 2, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 2, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 2, 2, 1, 0, 0, 0],
      [0, 0, 0, 2, 2, 2, 2, 0, 0, 0],
      [0, 0, 1, 1, 0, 0, 1, 1, 0, 0]
    ],
    [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // frame 7
      [0, 0, 0, 0, 3, 3, 0, 0, 0, 0],
      [0, 0, 0, 3, 3, 1, 3, 0, 0, 0],
      [0, 0, 0, 3, 1, 1, 1, 0, 0, 0],
      [0, 0, 0, 0, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 1, 2, 0, 0, 0, 0],
      [0, 0, 0, 0, 2, 1, 0, 0, 0, 0],
      [0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
      [0, 0, 1, 2, 2, 2, 2, 0, 0, 0],
      [0, 0, 1, 0, 0, 1, 1, 0, 0, 0]
    ]
  ];

  this.animate = function() {
    for (var i = 0; i < 10; i++) {
      for (var j = 0; j < 10; j++) {
        switch (this.sprite[this.frame][i][j]) {
          case 1:
            ctx.fillStyle = wht;
            break;
          case 2:
            ctx.fillStyle = lbu;
            break;
          case 3:
            ctx.fillStyle = dgr;
            break;
        }
        if (this.sprite[this.frame][i][j] !== 0) {
          var nt = Math.min(this.dr * scale * 10, 0);
          ctx.fillRect((scale * j * this.dr) + this.x - nt, scale * i, scale, scale);
        }
      }
    }
    if (this.frame < 7) {
      this.frame++;
    } else if (this.frame == 7) {
      this.frame = 0;
    }

    //var b = this.dr * 10 * scale;
    var sprWidth = scale * 10;
    if (this.x + this.dr * scale > 0 && this.x + this.dr * scale + sprWidth < c.width) {
      this.x += this.dr * scale;
    } else {
      this.dr *= -1
    };

    //check for collisions with blockers
    for (var q = 0; q < lemArray.length; q++) {
      if (lemArray[q].kind == "blocker" && this.x + sprWidth + this.dr > lemArray[q].x && this.x + this.dr < lemArray[q].x + sprWidth) {
        this.dr *= -1;
      }
    }

  };
  lemArray.push(this);
}


function cursor(){
  this.frame = 1;
  this.sprite = [
    [
      [0,0,0,0,0,0,1,2,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,1,2,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,1,2,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [2,0,2,0,2,0,3,3,0,1,0,1,0,1],
      [1,0,1,0,1,0,3,3,0,2,0,2,0,2],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,2,1,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,2,1,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,2,1,0,0,0,0,0,0]
    ],
    [
      [2,1,1,0,0,0,3,3,0,0,0,1,1,2],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [3,0,0,0,0,0,0,0,0,0,0,0,0,3],
      [3,0,0,0,0,0,0,0,0,0,0,0,0,3],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [2,1,1,0,0,0,3,3,0,0,0,1,1,2]
    ]
    
  ];
  
  this.display = function(){
    this.frame = 0;
    for(var p = 0;p < lemArray.length; p++){
      if(Math.abs(lemArray[p].x - mousePos.x) < scale*7){
        this.frame = 1;
      }
    }
    
    
    for(var q = 0;q < 14; q++){
      for(var r = 0;r < 14; r++){
        switch(this.sprite[this.frame][q][r]){
          case 1:
            ctx.fillStyle = wht;
            break;
          case 2:
            ctx.fillStyle = dgr;
            break;
          case 3:
             ctx.fillStyle = lbu;
            break;              
         }
        
        if(this.sprite[this.frame][q][r] !== 0){
          ctx.fillRect(mousePos.x + r*scale - scale*7,mousePos.y + q*scale - scale*7,scale,scale);
        };
      }
    }
  };
  
}






//Demo Lemmings (normally, you would do this part)

//create lemmings
var cr = new cursor();
var bl0 = new blocker(600);
var bl1 = new blocker(300);
var wk0 = new walker(0, 1);
var wk1 = new walker(400, -1);
var wk2 = new walker(480, 1);
var wk3 = new walker(710, 1);

function getMousePos(canvas, evt) {
  var rect = c.getBoundingClientRect();
  return {
    x: evt.clientX - rect.left,
    y: evt.clientY - rect.top
  };
}

c.addEventListener('mousemove', function(evt) {
  mousePos = getMousePos(canvas, evt);
}, false);

setInterval(function() {
  //run lemming animations
  ctx.clearRect(0, 0, c.width, c.height);
  
  bl0.animate();
  bl1.animate();
  wk0.animate();
  wk1.animate();
  wk2.animate();
  wk3.animate();
  cr.display();
 

}, 1000 / 15);
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console