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

Tree @master (Download .tar.gz)

stack.html @masterraw · history · blame

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

<h1>Using yoob.Tape as a Stack Demo</h1>

<div>
  <button id="push_0">Push 0</button>
  <button id="push_1">Push 1</button>
  <button id="pop">Pop</button>
  <button id="peek">Peek</button>
  <button id="size">Size?</button>
</div>

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

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

</body>
<script>
  var tos = new yoob.Cursor().init();
  var stack = new yoob.Tape().init({ cursors: [tos] });

  var canvasView = new yoob.TapeCanvasView().init({
      tape: stack,
      canvas: document.getElementById('canvas_view'),
      cellWidth: 20,
      cellHeight: 20
  });

  var htmlView = new yoob.TapeHTMLView().init({
      tape: stack,
      element: document.getElementById('html_view')
  });

  document.getElementById('push_0').onclick = function(e) {
      stack.push("0");
      canvasView.draw();
      htmlView.draw();
  };

  document.getElementById('push_1').onclick = function(e) {
      stack.push("1");
      canvasView.draw();
      htmlView.draw();
  };

  document.getElementById('pop').onclick = function(e) {
      var v = stack.pop();
      v = (v === undefined ? "Stack underflow" : v); 
      alert(v);
      canvasView.draw();
      htmlView.draw();
  };

  document.getElementById('peek').onclick = function(e) {
      var v = stack.peek();
      v = (v === undefined ? "Empty stack" : v); 
      alert(v);
      canvasView.draw();
      htmlView.draw();
  };

  document.getElementById('size').onclick = function(e) {
      alert(stack.getSize());
  };
</script>