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

              
                <!-- 

NOTE: Ehem! No canvas!

Just use the arrow keys to move around and discover the pixel world!

Map editor options:
- You can set the size and the zoom of the map by editing the 'terrain' array.
- You can custom the scenario by editing the 'pattern' array.
- You can modify your initial position by setting coords on the 'dotCoords' array.
- You can set the dot speed on the 'dotSpeed' var.
- And, of course, you can change terrain colors by editing the 'type' bidimensional array.

Some comments about material properties:
- The 'sea' and the 'deepsea' squares force a shape toggle.
- The 'mountain' squares are obstacles. You can not pass.
- The 'hole' squares are tunnels to some random place.

-->
              
            
!

CSS

              
                /*
================================

     ___
 ___| ↑ |___
| ← | ↓ | → |

Use your keyboard arrows to move,
and discover the pixel world!


================================
*/
              
            
!

JS

              
                /*
===============================================================

Hi! Welcome to my little playground!

My name is Tobias Bogliolo. 'Open source' by default and always 'responsive',
I'm a publicist, visual designer and frontend developer based in Barcelona. 

Here you will find some of my personal experiments. Sometimes usefull,
sometimes simply for fun. You are free to use them for whatever you want 
but I would appreciate an attribution from my work. I hope you enjoy it.

===============================================================
*/
//=====================
//=====================
//Set up:
//=====================
//=====================

//Land:
var container, sqr;
var terrain = [25, 20]; // => [lenght, scale]
var coords = [];

var type = [
    ['deepsea', '#6FBAC7'],  //0
    ['sea', '#9ED4DD'],      //1
    ['coast', '#F3EBA9'],    //2
    ['land', '#C6DB9A'],     //3
    ['forest', '#A4B16E'],   //4
    ['mountain', '#9E6D3D'], //5
    ['hole', '#3A2612']      //6
]; // => [type, color]

var pattern = [
    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,1,1,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
    0,0,0,0,1,1,2,2,1,1,1,2,2,2,1,1,1,1,1,1,0,0,0,0,0,
    0,0,0,1,1,2,2,2,2,1,2,2,3,2,2,2,1,1,1,1,1,0,0,0,0,
    0,0,0,0,1,2,2,6,2,2,2,3,3,3,3,2,2,2,1,1,1,1,0,0,0,
    0,0,1,1,1,2,2,3,3,2,2,3,3,3,3,2,2,2,2,2,1,1,0,0,0,
    0,0,1,1,2,2,3,3,3,3,3,3,3,3,3,3,2,2,6,2,2,1,1,0,0,
    0,1,1,2,2,3,3,3,3,3,4,4,4,3,3,3,3,2,2,2,2,1,1,0,0,
    1,1,2,2,2,3,3,4,4,4,5,4,3,3,4,4,3,3,3,3,2,1,1,0,0,
    1,2,2,2,3,3,4,5,5,4,4,4,4,4,4,5,4,3,3,3,2,2,1,1,0,
    1,6,2,3,3,4,4,5,5,5,4,4,4,5,4,4,4,4,3,3,3,2,1,1,0,
    1,2,2,2,3,3,4,5,5,5,4,4,5,5,5,5,4,3,3,3,3,2,2,1,1,
    1,1,2,2,3,3,4,4,5,4,4,4,5,5,5,5,5,4,3,3,3,3,2,1,1,
    0,1,1,2,3,3,3,4,4,3,4,4,5,5,5,5,5,4,4,3,3,3,2,1,1,
    0,1,1,2,2,3,3,3,3,3,3,4,4,5,5,5,5,4,3,3,3,2,2,1,0,
    0,1,1,1,2,2,4,4,3,3,4,5,4,5,5,4,4,3,3,3,2,2,1,1,0,
    0,0,1,1,1,2,3,4,4,3,4,4,5,4,5,4,3,3,3,2,2,1,1,0,0,
    0,0,0,1,2,2,6,3,3,3,3,4,4,4,4,3,3,3,2,2,1,1,1,0,0,
    0,0,0,1,1,2,2,3,3,3,3,3,4,4,3,3,6,2,2,1,1,0,0,0,0,
    0,0,0,0,1,1,2,2,3,2,3,3,3,3,3,2,2,2,1,1,1,0,0,0,0,
    0,0,0,0,1,1,1,2,2,2,2,2,3,3,3,2,2,1,1,1,0,0,0,0,0,
    0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,2,1,1,1,1,0,0,0,0,0,
    0,0,0,0,0,0,0,1,1,1,1,2,1,2,2,6,1,1,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0
];

//Dot:
var dot;
var dotCoords = [13, 7] //Set 'dot' initial position. => [x, y]
var dotSpeed = 200; //ms.
var dotTeleportSpeed = 500; //ms.
var activeTransition = false;

var dotColor = [
  'linear-gradient(45deg, rgba(46,244,205,0) 30%,rgba(198,171,203,0) 60%,rgba(246,148,203,1) 100%), radial-gradient(ellipse at center, rgba(76,110,207,0) 27%,rgba(76,110,207,0.33) 51%,rgba(89,88,179,0.58) 69%,rgba(89,78,183,1) 100%), linear-gradient(80deg, rgba(246,148,203,0) 0%,rgba(70,232,205,.1) 70%,rgba(46,244,205,1) 100%), linear-gradient(45deg, rgba(46,244,205,1) 0%,rgba(76,110,207,0.2) 50%,rgba(76,110,207,0) 60%)',
  '#FFFFFF'
]; // => [default, secondary]


//=====================
//=====================
//Functions:
//=====================
//=====================

//Set coords:
function setCoords(c, x, y, t){
  $(c).attr("data-x", x);
  $(c).attr("data-y", y);
  $(c).attr("title", t);
}; // => setCoords(element, coord-x, coord-y, title)

//Terrain type exceptions:
function exception(x, y, type){
  return $('div[class^="sqr"][data-x="'+x+'"][data-y="'+y+'"]').hasClass(type);
}; // => exception(coord-x, coord-y, type)

//Special material events:
function circumstance(){
  //'Watter' toggle:
  if ( exception(dotCoords[0], dotCoords[1], "sea") || exception(dotCoords[0], dotCoords[1], "deepsea") ){
    $(".dot").css({
      'background': dotColor[1]
    });
    $(".dot").addClass("boat");
  }
  else {
    $(".dot").css({
      'background': dotColor[0]
    });
    $(".dot").removeClass("boat");
  };
  //'Tunnels' teleport:
  if ( exception(dotCoords[0], dotCoords[1], "hole") ){
    teleport();
  };
};

//Teleport:
function teleport(){
  //Select a random 'hole':
  var randNum = Math.floor(Math.random()*$('.hole').length);
  //Update coords:
  dotCoords[0] = $(".hole").eq(randNum).attr("data-x");
  dotCoords[1] = $(".hole").eq(randNum).attr("data-y");
  //Transfer coords:
  setCoords(".dot", dotCoords[0], dotCoords[1], 'dot ['+dotCoords+']');
  //Animate 'dot':
  $(".dot").addClass("teleport-out");
  setTimeout(function(){
    $(".dot").stop().animate(
      {
        left: dotCoords[0]*terrain[1],
        top: dotCoords[1]*terrain[1]
      }, 
      0,
      function(){
        $(".dot").removeClass("teleport-out").addClass("teleport-in");
        setTimeout(function(){
          $(".dot").removeClass("teleport-in"); //Reload the 'wobble' animation.
        }, dotTeleportSpeed);
      }
    );   
  }, dotTeleportSpeed);
  //Debug:
  console.log(dotCoords);
};


//=====================
//=====================
//There we go!
//=====================
//=====================
$(document).ready(function(){

  //=====================
  //=====================
  //Terrain:
  //=====================
  //=====================

  //Constructor:
  container = '<div class="container"></div>';
  $("body").append(container);

  //Styling:
  $(".container").css({
    'box-sizing': 'border-box',
    'position': 'absolute',
    'width': terrain[0]*terrain[1]+'px',
    'height': terrain[0]*terrain[1]+'px',
    'background': type[3][1],
    'overflow': 'hidden',
    'cursor': 'crosshair'
  });


  //=====================
  //=====================
  //Pattern:
  //=====================
  //=====================

  //Loop pattern:
  for (i=0; i<pattern.length; i++) {

    //Get coords:
    coords[0] = i%terrain[0]; //Define rows. => [x]
    coords[1] = Math.floor(i/terrain[0]); //Define cols. => [y]

    //Constructor:
    sqr = '<div class="sqr'+i+'"></div>';
    $(".container").append(sqr);

    //Set coords and material type:
    setCoords(".sqr"+i, coords[0], coords[1], type[pattern[i]][0]+' ['+coords+']');
    $(".sqr"+i).addClass(type[pattern[i]][0]);

    //Styling:
    $(".sqr"+i).css({
      'box-sizing': 'border-box',
      'position': 'absolute',
      'top': coords[1]*terrain[1]+'px',
      'left': coords[0]*terrain[1]+'px',
      'width': terrain[1]+'px',
      'height': terrain[1]+'px',
      'background': type[pattern[i]][1]
    });
    
    //Round corners ('forest'):
    if (
      $(".sqr"+i).hasClass("forest") &&
      pattern[i] != pattern[i-1] &&
      pattern[i] != pattern[i-terrain[0]]
    ){
      $(".sqr"+i).css({
        'border-top-left-radius': terrain[1]/2
      });
    };
    if (
      $(".sqr"+i).hasClass("forest") &&
      pattern[i] != pattern[i+1] &&
      pattern[i] != pattern[i-terrain[0]]
    ){
      $(".sqr"+i).css({
        'border-top-right-radius': terrain[1]/2
      });
    };
    if (
      $(".sqr"+i).hasClass("forest") &&
      pattern[i] != pattern[i+1] &&
      pattern[i] != pattern[i+terrain[0]]
    ){
      $(".sqr"+i).css({
        'border-bottom-right-radius': terrain[1]/2
      });
    };
    if (
      $(".sqr"+i).hasClass("forest") &&
      pattern[i] != pattern[i-1] &&
      pattern[i] != pattern[i+terrain[0]]
    ){
      $(".sqr"+i).css({
        'border-bottom-left-radius': terrain[1]/2
      });
    };

    //Round corners ('mountain'):
    if (
      $(".sqr"+i).hasClass("mountain") &&
      pattern[i] != pattern[i-1] &&
      pattern[i] != pattern[i-terrain[0]]
    ){
      $(".sqr"+i).css({
        'border-top-left-radius': terrain[1]/2
      });
    };
    if (
      $(".sqr"+i).hasClass("mountain") &&
      pattern[i] != pattern[i+1] &&
      pattern[i] != pattern[i-terrain[0]]
    ){
      $(".sqr"+i).css({
        'border-top-right-radius': terrain[1]/2
      });
    };
    if (
      $(".sqr"+i).hasClass("mountain") &&
      pattern[i] != pattern[i+1] &&
      pattern[i] != pattern[i+terrain[0]]
    ){
      $(".sqr"+i).css({
        'border-bottom-right-radius': terrain[1]/2
      });
    };
    if (
      $(".sqr"+i).hasClass("mountain") &&
      pattern[i] != pattern[i-1] &&
      pattern[i] != pattern[i+terrain[0]]
    ){
      $(".sqr"+i).css({
        'border-bottom-left-radius': terrain[1]/2
      });
    };

  };


  //=====================
  //=====================
  //Dot:
  //=====================
  //=====================

  //Constructor:
  dot = '<div class="dot"></div>';
  $(".container").append(dot);
  
  //Set coords:
  setCoords(".dot", dotCoords[0], dotCoords[1], 'dot ['+dotCoords+']');

  //Styling:
  $(".dot").css({
    'box-sizing': 'border-box',
    'position': 'absolute',
    'width': terrain[1]+'px',
    'height': terrain[1]+'px',
    'top': dotCoords[1]*terrain[1]+'px',
    'left': dotCoords[0]*terrain[1]+'px',
    'border-radius': '100%',
    'box-shadow': '0px 4px 4px -2px rgba(0, 0, 0, .2)',
    'z-index': '1'
  });

  //Special material events:
  circumstance();
  
  //'Dot' animate:
  $(document).keydown(function(e){
    if ( !activeTransition ){
      activeTransition = true;
      switch ( e.which ){

        //Up:
        case 38:
          if ( dotCoords[1] > 0 && !exception(dotCoords[0], parseInt(dotCoords[1])-1, "mountain" ) ){
            //Get new position:
            dotCoords[1] = parseInt(dotCoords[1])-1;
            setCoords(".dot", dotCoords[0], dotCoords[1], 'dot ['+dotCoords+']');
            //Move:
            $(".dot").stop().animate(
              {
                top: dotCoords[1]*terrain[1]
              },
              dotSpeed
            );
          }; //Scenario limits and 'mountain' blocking.
        break;

        //Forward:
        case 39:
          if ( dotCoords[0] < coords[0] && !exception(parseInt(dotCoords[0])+1,dotCoords[1], "mountain" ) ){
            //Get new position:
            dotCoords[0] = parseInt(dotCoords[0])+1;
            setCoords(".dot", dotCoords[0], dotCoords[1], 'dot ['+dotCoords+']');
            //Move:
            $(".dot").stop().animate(
              {
                left: dotCoords[0]*terrain[1]
              },
              dotSpeed
            );
          }; //Scenario limits and 'mountain' blocking.
        break;

        //Down:
        case 40:
        if ( dotCoords[1] < coords[1] && !exception(dotCoords[0], parseInt(dotCoords[1])+1, "mountain" ) ){
          //Get new position:
          dotCoords[1] = parseInt(dotCoords[1])+1;
          setCoords(".dot", dotCoords[0], dotCoords[1], 'dot ['+dotCoords+']');
          //Move:
          $(".dot").stop().animate(
            {
              top: dotCoords[1]*terrain[1]
            },
            dotSpeed
          );
        }; //Scenario limits and 'mountain' blocking.
        break;

        //Back:
        case 37:
          if ( dotCoords[0] > 0 && !exception(parseInt(dotCoords[0])-1, dotCoords[1], "mountain" ) ){
            //Get new position:
            dotCoords[0] = parseInt(dotCoords[0])-1;
            setCoords(".dot", dotCoords[0], dotCoords[1], 'dot ['+dotCoords+']');
            //Move:
            $(".dot").stop().animate(
              {
                left: dotCoords[0]*terrain[1]
              },
              dotSpeed
            );
          }; //Scenario limits and 'mountain' blocking.
        break;

      };
      activeTransition = false;
    };

    //Special material events:
    circumstance();

    //Debug:
    console.log("x:"+dotCoords[0]+" y:"+dotCoords[1]);
  });
  

  //=====================
  //=====================
  //Styles:
  //=====================
  //=====================

  //Global styles:
  var globalStyles = $('<style>\
  /* Global */\
  body {\
    background: #1c1b1b;\
  }\
  .container {\
    top: 50%;\
    left: 50%;\
    -webkit-transform: translate(-50%, -50%);\
    -ms-transform: translate(-50%, -50%);\
    -o-transform: translate(-50%, -50%);\
    transform: translate(-50%, -50%);\
  }\
  .hole {\
    box-shadow: inset 0px 0px 5px 6px #F3EBA9;\
  }\
  </style>')
  $('html > head').append(globalStyles);

  //'Dot' wobble:
  var dotWobble = $('<style>\
  .dot {\
    animation: wobble 1.5s cubic-bezier(.30,0,.6,1) infinite;\
  }\
  @keyframes wobble {\
    0% {\
      transform: translateY(0);\
    }\
    50% {\
      transform: translateY(-4px);\
    }\
    100% {\
      transform: translateY(0);\
    }\
  }\
  </style>')
  $('html > head').append(dotWobble);

  //'Boat' toggle:
  var boatToggle = $('<style>\
  .boat {\
    background: #EE6648 !important;\
    border-radius: 0 0 5px 5px !important;\
    width: 18px !important;\
    height: 5px !important;\
    margin-top: 10px !important;\
    box-shadow: none !important;\
  }\
  .boat:before,\
  .boat:after {\
    content: "";\
    position: absolute;\
    border-style: solid;\
  }\
  .boat:before {\
    top: -14px;\
    left: 10px;\
    border-width: 12px 0 0 8px;\
    border-color: transparent transparent transparent #FFF8ED;\
  }\
  .boat:after {\
    top: -11px;\
    left: 0px;\
    border-width: 0 0 9px 8px;\
    border-color: transparent transparent #FFF8ED transparent;\
  }\
  </style>')
  $('html > head').append(boatToggle);

  //'Teleport' animation:
  var teleportAnimation = $('<style>\
  .teleport-out {\
    transform-origin: 50% 50%;\
    transform: scale(1.1);\
    animation: teleportout .4s 1 cubic-bezier(.84,-0.54,.7,.99) forwards;\
  }\
  @keyframes teleportout {\
    0%  {\
      transform: scale(1.1);\
    }\
    100%  {\
      transform: scale(0.0);\
    }\
  }\
  .teleport-in {\
    transform-origin: 50% 50%;\
    transform: scale(0.0);\
    animation: teleportin .4s 1 cubic-bezier(.45,.08,.46,1.63) forwards;\
  }\
  @keyframes teleportin {\
    0%  {\
      transform: scale(0.0);\
    }\
    100%  {\
      transform: scale(1.1);\
    }\
  }\
  </style>')
  $('html > head').append(teleportAnimation);

}); //Document ready ends! :(
              
            
!
999px

Console