Difference between revisions of "Turtle bot"

From JSXGraph Wiki
Jump to: navigation, search
Line 64: Line 64:
  
 
// Event handlers:
 
// Event handlers:
document.getElementById("btn_forward").addEventListener("click", function(evt) {
+
document.getElementById("btn_forward").addEventListener('click', function(evt) {
 
   if (is_recording) {
 
   if (is_recording) {
 
     program.push('turtle.forward(stepsize);');
 
     program.push('turtle.forward(stepsize);');
Line 73: Line 73:
 
});  
 
});  
  
document.getElementById("btn_back").addEventListener("click", function(evt) {
+
document.getElementById('btn_back').addEventListener('click', function(evt) {
 
   if (is_recording) {
 
   if (is_recording) {
 
     program.push('turtle.back(stepsize);');
 
     program.push('turtle.back(stepsize);');
     if (document.getElementById("show_record").checked) { turtle.back(100); }
+
     if (document.getElementById('show_record').checked) { turtle.back(100); }
 
   } else {
 
   } else {
 
     turtle.back(stepsize);
 
     turtle.back(stepsize);
Line 82: Line 82:
 
});  
 
});  
  
document.getElementById("btn_left").addEventListener("click", function(evt) {
+
document.getElementById('btn_left').addEventListener('click', function(evt) {
 
   if (is_recording) {
 
   if (is_recording) {
 
     program.push('turtle.left(angle);');
 
     program.push('turtle.left(angle);');
     if (document.getElementById("show_record").checked) { turtle.left(45); }
+
     if (document.getElementById('show_record').checked) { turtle.left(45); }
 
   } else {
 
   } else {
 
     turtle.left(angle);
 
     turtle.left(angle);
Line 91: Line 91:
 
});  
 
});  
  
document.getElementById("btn_right").addEventListener("click", function(evt) {
+
document.getElementById('btn_right').addEventListener('click', function(evt) {
 
   if (is_recording) {
 
   if (is_recording) {
 
     program.push('turtle.right(angle);');
 
     program.push('turtle.right(angle);');
     if (document.getElementById("show_record").checked) { turtle.right(45); }
+
     if (document.getElementById('show_record').checked) { turtle.right(45); }
 
   } else {
 
   } else {
 
     turtle.right(angle);
 
     turtle.right(angle);
Line 100: Line 100:
 
});  
 
});  
  
document.getElementById("btn_clear").addEventListener("click", function(evt) {
+
document.getElementById('btn_clear').addEventListener('click', function(evt) {
 
   turtle.clearScreen();
 
   turtle.clearScreen();
 
});  
 
});  
  
document.getElementById("btn_record").addEventListener("click", function(evt) {
+
document.getElementById('btn_record').addEventListener('click', function(evt) {
 
   if (!is_recording) {
 
   if (!is_recording) {
 
     is_recording = true;
 
     is_recording = true;
     document.getElementById("btn_record").innerHTML = "stop";
+
     document.getElementById('btn_record').innerHTML = 'stop';
     document.getElementById("btn_record").classList.add('record');  
+
     document.getElementById('btn_record').classList.add('record');  
 
     program = [];
 
     program = [];
 
     board.defaultAxes.x.setAttribute({visible:true});
 
     board.defaultAxes.x.setAttribute({visible:true});
Line 115: Line 115:
 
   } else {
 
   } else {
 
     is_recording = false;
 
     is_recording = false;
     document.getElementById("btn_record").innerHTML = "record";
+
     document.getElementById('btn_record').innerHTML = 'record';
     document.getElementById("btn_record").classList.remove('record');  
+
     document.getElementById('btn_record').classList.remove('record');  
     board.defaultAxes.x.setAttribute({visible:false});
+
      
    board.defaultAxes.y.setAttribute({visible:false});
+
    if (!document.getElementById('show_grid').checked) {
 +
      board.defaultAxes.x.setAttribute({visible:false});
 +
      board.defaultAxes.y.setAttribute({visible:false});
 +
    }
 +
 
 
     turtle.clearScreen();
 
     turtle.clearScreen();
 
   }
 
   }
Line 130: Line 134:
 
   }
 
   }
 
}
 
}
document.getElementById("btn_play").addEventListener("click", function(evt) {
+
document.getElementById('btn_play').addEventListener('click', function(evt) {
 
   step = 0;
 
   step = 0;
 
   do_step();     
 
   do_step();     
 
});  
 
});  
 +
 +
document.getElementById('show_grid').addEventListener('change', function(evt) {
 +
  if (document.getElementById('show_grid').checked) {
 +
    board.defaultAxes.x.setAttribute({visible:true});
 +
    board.defaultAxes.y.setAttribute({visible:true});
 +
  } else {
 +
    board.defaultAxes.x.setAttribute({visible:false});
 +
    board.defaultAxes.y.setAttribute({visible:false});
 +
  }
 +
});
 +
 
</jsxgraph>
 
</jsxgraph>
  

Revision as of 11:38, 13 June 2019

Show recording: Show grid:

The underlying JavaScript code

HTML code:

<style>
button {
  min-width:100px;
}
.record {
  background-color: #ffaaaa;
}
</style>
Show recording: <input type="checkbox" id="show_record"><br/>
<button id="btn_record">record</button>
<button id="btn_play">play</button>
<button id="btn_clear">clear screen</button>

<table>
<tr>
  <td></td>
  <td><button id="btn_forward">forward</button></td>
  <td></td>
</tr>
<tr>
  <td><button id="btn_left">left</button></td>
  <td></td>
  <td><button id="btn_right">right</button></td>
</tr>
<tr>
  <td></td>
  <td><button id="btn_back">back</button></td>
  <td></td></tr>
</table>

JavaScript code:

const board = JXG.JSXGraph.initBoard('turtlebox', { 
  boundingbox: [-500, 500, 500, -500], 
  axis:true,
  defaultAxes: {
    x: {
      visible:false,
      ticks: {
        label: {
          visible: false
        }
      }
    }, 
    y: {
      visible:false,
      ticks: {
        label: {
          visible: false
        }
      }
    } 
  }
});

// Global variables:
var turtle = board.create('turtle', [0,0]);
var is_recording = false;
var program = [];
var step;
var angle = 90;
var stepsize = 100;

// Event handlers:
document.getElementById("btn_forward").addEventListener("click", function(evt) {
  if (is_recording) {
    program.push('turtle.forward(stepsize);');
    if (document.getElementById("show_record").checked) { turtle.forward(100); }
  } else {
    turtle.forward(stepsize);
  }  
}); 

document.getElementById("btn_back").addEventListener("click", function(evt) {
  if (is_recording) {
    program.push('turtle.back(stepsize);');
    if (document.getElementById("show_record").checked) { turtle.back(100); }
  } else {
    turtle.back(stepsize);
  }
}); 

document.getElementById("btn_left").addEventListener("click", function(evt) {
  if (is_recording) {
    program.push('turtle.left(angle);');
    if (document.getElementById("show_record").checked) { turtle.left(45); }
  } else {
    turtle.left(angle);
  }
}); 

document.getElementById("btn_right").addEventListener("click", function(evt) {
  if (is_recording) {
    program.push('turtle.right(angle);');
    if (document.getElementById("show_record").checked) { turtle.right(45); }
  } else {
    turtle.right(angle);
  }
}); 

document.getElementById("btn_clear").addEventListener("click", function(evt) {
  turtle.clearScreen();
}); 

document.getElementById("btn_record").addEventListener("click", function(evt) {
  if (!is_recording) {
    is_recording = true;
    document.getElementById("btn_record").innerHTML = "stop";
    document.getElementById("btn_record").classList.add('record'); 
    program = [];
    board.defaultAxes.x.setAttribute({visible:true});
    board.defaultAxes.y.setAttribute({visible:true});
    turtle.clearScreen();
  } else {
    is_recording = false;
    document.getElementById("btn_record").innerHTML = "record";
    document.getElementById("btn_record").classList.remove('record'); 
    board.defaultAxes.x.setAttribute({visible:false});
    board.defaultAxes.y.setAttribute({visible:false});
    turtle.clearScreen();
  }
}); 

var do_step = function() {
  if (step < program.length) {
    eval(program[step]);
    step++;
    setTimeout(do_step, 500);
  }
}
document.getElementById("btn_play").addEventListener("click", function(evt) {
  step = 0;
  do_step();    
});