This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
SA3/hw3/Claudio_Maggioni/scripts/undo.js

38 lines
723 B
JavaScript
Raw Normal View History

2019-10-06 18:22:53 +00:00
// vim: set ts=2 sw=2 et tw=80:
2019-10-04 15:24:57 +00:00
const history = {
2019-10-06 18:22:53 +00:00
paths: []
}
2019-10-07 10:17:28 +00:00
history.pop = () => {
2019-10-06 18:22:53 +00:00
if (history.paths.length == 0) return;
2019-10-07 10:17:28 +00:00
return history.paths.pop();
2019-10-06 18:22:53 +00:00
};
history.initializeNewPath = () => {
history.paths.push([]);
};
history.push = (stroke) => {
2019-10-07 10:17:28 +00:00
if (!stroke || !stroke instanceof Stroke) {
2019-10-06 18:22:53 +00:00
throw new Error(JSON.stringify(stroke) + ' is not a Stroke instance');
}
history.paths[history.paths.length - 1].push(stroke);
2019-10-07 10:17:28 +00:00
return history.paths[history.paths.length - 1];
2019-10-06 18:22:53 +00:00
}
history.clear = () => {
history.paths = [];
};
2019-10-04 15:24:57 +00:00
2019-10-06 18:22:53 +00:00
class Stroke {
2019-10-07 09:48:24 +00:00
constructor(brushName, strokeStyle, x, y) {
2019-10-06 18:22:53 +00:00
this.brushName = brushName;
2019-10-07 09:48:24 +00:00
this.strokeStyle = strokeStyle;
2019-10-06 18:22:53 +00:00
this.offsetX = x;
this.offsetY = y;
}
}