My current project involves creating a paint program in JavaScript, and I am aiming to implement an undo function rather than just an eraser. How can I store events in an array and then enable the deletion of each event individually?
I have a dropdown menu for selecting tools (but only four are functional at the moment). I have also added an undo button with an ID. Despite spending hours (and even days) trying to figure it out, I believe I need to utilize both push and an empty array to make progress.
Below is the code snippet for tool selection and the button:
<label>
Object type:
<select id="selectTool">
<option value="line">Line</option>
<option value="pencil">Pencil</option>
<option value="rect">Rectangle</option>
<option value="circle">Circle</option>
<option value="oval">Oval</option>
<option value="polygon">Polygon</option>
</select>
Shape drawn:
<select id="shapeDrawn">
<option value=""></option>
</select>
<input type="button" id="cmbDelete" value="Undo last action">
</label>
The potential structure of the undo function might resemble this, although adjustments are needed:
var shapes = [];
shapes.push(newShape);
function cmbDeleteClick(){
if(shapes.length > 0){
var selectedShapeIndex = selectShape.selectedIndex;
shapes.splice(selectedShapeIndex,1);
selectShape.options.remove(selectedShapeIndex);
selectShape.selectedIndex = selectShape.options.length - 1;
}
cmbDelete = document.getElementById("cmbDelete");
cmbDelete.addEventListener("click",cmbDeleteClick, false);
fillSelectShapeTypes();
drawCanvas();
}
In an ideal scenario, everything painted on the canvas should be listed in a dropdown menu and be deletable by clicking a button. The "working" version of the code can be accessed here JS Bin