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

              
                #app
  canvas#canvas
  .center
    button#reset.btn reset
    button#drop1.btn.btn-drop drop 1
    button#drop2.btn.btn-drop drop 2
              
            
!

CSS

              
                body
  margin: 0;
  background: #f4f4f4;
#app
  display: flex;
  align-items: flex-start;
  justify-content: center;
  margin: 0 auto;
  padding: 1rem;
  background: #fff;
  max-width: 48em;

#canvas
  display: block;
  margin: 0 1rem 0 0;
  border: 1px solid #bbb;
  width: 20px * 15;
  
.center
  text-align: center;
  margin-top: 1rem;
  .btn
    margin-bottom: 1rem;
  
.btn
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  text-align: center;
  text-transform: uppercase;
  display: inlinr-block;
  padding: .5em 1.5em;
  border: none;
  background-color: #eee;
  border-radius: .25em;
   
.btn-drop
  color: #fff;
  background: #2ba4bb;
              
            
!

JS

              
                const ROWS = 20;
const COLS = 15;
const SIZE = 20;

const validPosY = ({y}) => {
  return y < ROWS - 1;
}

const curry1 = (func, checkFunc) => {
  return (arg, callback) => {
    if (!checkFunc(arg)) {
      return arg;
    }
    const res = func(arg);
    console.log(res.y, checkFunc(res));
    setTimeout(
      function() { callback(res, callback) },
      30
    );
  }
};

const curry2 = (func, checkFunc) => {
  return function(arg) {
    if (!checkFunc(arg)) {
      return arg;
    }
    const res = func(arg);
    console.log(res.y, checkFunc(res));
    setTimeout(
      function() { curry2(func, checkFunc)(res) },
      30
    );
  }
};

function Block(_ctx) {
  const ctx = _ctx;
  const pos = {
    x: Math.floor(Math.random() * COLS),
    y: Math.floor(Math.random() * (ROWS - 1)),
  };
  
  const move = ({x, y}) => {
    pos.x += x;
    pos.y += y;
  };
  
  const draw = () => {
    ctx.fillStyle = '#fbcb62';
    ctx.fillRect(pos.x, pos.y, 1, 1);
  }
  
  return {
    pos,
    draw,
    move,
  };
}


function clearCanvas(ctx) {
  return ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}

function Board(_ctx) {
  const ctx = _ctx;
  ctx.canvas.width = COLS * SIZE;
  ctx.canvas.height = ROWS * SIZE;
  ctx.scale(SIZE, SIZE);
  
  const props = {
    block: null,
  }
  
  const moveAndDraw = ({x, y}) => {
    const block = props.block;
    block.move({x:0, y:1});
    draw();
    return block.pos;
  }
  
  const onDrop1 = curry1(moveAndDraw, validPosY);
  const onDrop2 = curry2(moveAndDraw, validPosY);
  
  function drop(type) {
    const block = props.block;
    if (type) {
      onDrop1(block.pos, onDrop1);
    } else {
      onDrop2(block.pos);
    }
  }
  
  function draw() {
    clearCanvas(ctx);
    props.block.draw();
  }
  
  function reset() {
    clearCanvas(ctx);
    props.block = new Block(ctx);
    props.block.draw();
  }
  
  return {
    props,
    reset,
    drop,
  };
}


function init() {
  const canvas = document.getElementById('canvas');
  const ctx = canvas.getContext('2d');
  const board = new Board(ctx);
  console.log(board);
  board.reset();
  
  const resetBtn = document.getElementById('reset');
  const dropBtn1 = document.getElementById('drop1');
  const dropBtn2 = document.getElementById('drop2');
  
  // event
  resetBtn.addEventListener('click', () => board.reset());
  dropBtn1.addEventListener('click', () => board.drop(true));
  dropBtn2.addEventListener('click', () => board.drop(false));
}

init();
              
            
!
999px

Console