git @ Cat's Eye Technologies yoob.js / 63f5b0a
yoob.Tape.get[Cursored]Extent() as part of fixing eg/tape.html. Chris Pressey 9 years ago
3 changed file(s) with 37 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
33 <title>yoob.Tape Demo</title>
44 <script src="../src/yoob/tape.js"></script>
55 <script src="../src/yoob/cursor.js"></script>
6 <script src="../src/yoob/tape-canvas-view.js"></script>
67 <style>
78 #canvas { border: 1px solid blue; }
89 </style>
1718
1819 <button id="write_0">Write 0</button>
1920 <button id="write_1">Write 1</button>
21 <button id="read">Read</button>
2022 <button id="left">Move Left</button>
2123 <button id="right">Move Right</button>
2224
2325 </body>
2426 <script>
2527 var canvas = document.getElementById('canvas');
26 var tape = new yoob.Tape();
27 var th = new yoob.Cursor(0);
28 th.tape = tape;
28 var head = new yoob.Cursor();
29 var tape = new yoob.Tape().init({ cursors: [head] });
30 var tcv = new yoob.TapeCanvasView().init({
31 tape: tape,
32 canvas: canvas
33 });
2934
3035 document.getElementById('write_0').onclick = function(e) {
31 th.write("0");
32 tape.drawCanvas(canvas, undefined, 20, [th]);
36 tape.write("0");
37 tcv.draw();
3338 };
3439
3540 document.getElementById('write_1').onclick = function(e) {
36 th.write("1");
37 tape.drawCanvas(canvas, undefined, 20, [th]);
41 tape.write("1");
42 tcv.draw();
43 };
44
45 document.getElementById('read').onclick = function(e) {
46 alert(tape.read());
3847 };
3948
4049 document.getElementById('left').onclick = function(e) {
41 th.moveLeft();
42 tape.drawCanvas(canvas, undefined, 20, [th]);
50 head.moveLeft();
51 tcv.draw();
4352 };
4453
4554 document.getElementById('right').onclick = function(e) {
46 th.moveRight();
47 tape.drawCanvas(canvas, undefined, 20, [th]);
55 head.moveRight();
56 tcv.draw();
4857 };
4958
5059 </script>
120120 var cellWidth = this.cellWidth;
121121 var cellHeight = this.cellHeight;
122122
123 var width = this.tape.getCursoredExtent(); // this.max - this.min + 1;
123 var width = this.tape.getCursoredExtent();
124124 var height = 1;
125125
126126 canvas.width = width * cellWidth;
7676 }
7777 };
7878
79 this.getExtent = function() {
80 return this.max - this.min + 1;
81 };
82
83 this.getCursoredExtent = function() {
84 var max_ = this.max;
85 var min_ = this.min;
86 var i;
87 for (i = 0; i < this.cursors.length; i++) {
88 var x = this.cursors[i].getX();
89 if (x > max_) max_ = x;
90 if (x < min_) min_ = x;
91 }
92 return max_ - min_ + 1;
93 };
94
7995 /*
8096 * Cursored read/write interface
8197 */