git @ Cat's Eye Technologies yoob.js / master eg / playfield.html
master

Tree @master (Download .tar.gz)

playfield.html @masterraw · history · blame

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <title>yoob.Playfield Demo</title>
  <script src="../src/yoob/playfield.js"></script>
  <script src="../src/yoob/cursor.js"></script>
  <script src="../src/yoob/playfield-canvas-view.js"></script>
  <script src="../src/yoob/playfield-html-view.js"></script>
  <style>
#canvas_view { border: 1px solid blue; }
#html_view { border: 1px solid red; font-size: 20px; }
  </style>
</head>
<body>

<h1>yoob.Playfield Demo</h1>

<div>
  <button id="load">Load</button>
  <button id="step">Step</button>
</div>

<canvas id="canvas_view" width="100" height="100">
Your browser doesn't support displaying an HTML5 canvas.
</canvas>

<pre id="html_view"></pre>

<textarea id="program" rows="10" cols="40">
   #
#$ @#
    @#


     @
</textarea>

</body>
<script>
  var ip = (new yoob.Cursor()).init({ dx: 1, dy: 0 });
  var pf = new yoob.Playfield().init({
      defaultValue: ' ',
      cursors: [ip]
  });

  var canvasView = new yoob.PlayfieldCanvasView().init({
      playfield: pf,
      canvas: document.getElementById('canvas_view'),
      cellWidth: 20,
      cellHeight: 20
  });

  var htmlView = new yoob.PlayfieldHTMLView().init({
      playfield: pf,
      element: document.getElementById('html_view')
  });

  var textarea = document.getElementById('program');
  document.getElementById('load').onclick = function(e) {
    pf.load(0, 0, textarea.value, function(c) {
        if (c === '#')
            return '%';
        else
            return c;
    });
    pf.foreach(function (x, y, value) {
        if (value === '$') {
            ip.x = x;
            ip.y = y;
            return '*';
        }
    });
    ip.dx = 1;
    ip.dy = 0;
    canvasView.draw();
    htmlView.draw();
  };

  document.getElementById('step').onclick = function(e) {
    ip.advance();
    canvasView.draw();
    htmlView.draw();
  };
</script>