marching ants during image generation

i uh also decided to useTabs in prettier and it now looks like i rewrote the entire application again :badpokerface:
This commit is contained in:
tim h 2022-11-20 17:10:03 -06:00
parent bef6b214f4
commit 959bb4f8b1
4 changed files with 896 additions and 873 deletions

View file

@ -14,6 +14,6 @@
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false,
"useTabs": true,
"vueIndentScriptAndStyle": false
}

View file

@ -2,124 +2,124 @@
* Command pattern to allow for editing history
*/
const commands = {
current: -1,
history: [],
undo(n = 1) {
for (var i = 0; i < n && this.current > -1; i++) {
this.history[this.current--].undo();
}
},
redo(n = 1) {
for (var i = 0; i < n && this.current + 1 < this.history.length; i++) {
this.history[++this.current].redo();
}
},
current: -1,
history: [],
undo(n = 1) {
for (var i = 0; i < n && this.current > -1; i++) {
this.history[this.current--].undo();
}
},
redo(n = 1) {
for (var i = 0; i < n && this.current + 1 < this.history.length; i++) {
this.history[++this.current].redo();
}
},
/**
* These are basic commands that can be done/undone
*
* They must contain a 'run' method that performs the action the first time,
* a 'undo' method that undoes that action and a 'redo' method that does the
* action again, but without requiring parameters. 'redo' is by default the
* same as 'run'.
*
* The 'run' and 'redo' functions will receive a 'options' parameter which will be
* forwarded directly to the operation, and a 'state' parameter that
* can be used to store state for undoing things.
*
* The 'state' object will be passed to the 'undo' function as well.
*/
createCommand(name, run, undo, redo = run) {
const command = function runWrapper(options) {
// Create copy of options and state object
const copy = {};
Object.assign(copy, options);
const state = {};
/**
* These are basic commands that can be done/undone
*
* They must contain a 'run' method that performs the action the first time,
* a 'undo' method that undoes that action and a 'redo' method that does the
* action again, but without requiring parameters. 'redo' is by default the
* same as 'run'.
*
* The 'run' and 'redo' functions will receive a 'options' parameter which will be
* forwarded directly to the operation, and a 'state' parameter that
* can be used to store state for undoing things.
*
* The 'state' object will be passed to the 'undo' function as well.
*/
createCommand(name, run, undo, redo = run) {
const command = function runWrapper(options) {
// Create copy of options and state object
const copy = {};
Object.assign(copy, options);
const state = {};
// Attempt to run command
try {
run(copy, state);
} catch (e) {
console.warn(`Error while running command '${name}' with options:`);
console.warn(copy);
console.warn(e);
return;
}
// Attempt to run command
try {
run(copy, state);
} catch (e) {
console.warn(`Error while running command '${name}' with options:`);
console.warn(copy);
console.warn(e);
return;
}
const undoWrapper = () => {
console.debug(`Undoing ${name}, currently ${commands.current}`);
undo(state);
};
const redoWrapper = () => {
console.debug(`Redoing ${name}, currently ${commands.current}`);
redo(copy, state);
};
const undoWrapper = () => {
console.debug(`Undoing ${name}, currently ${commands.current}`);
undo(state);
};
const redoWrapper = () => {
console.debug(`Redoing ${name}, currently ${commands.current}`);
redo(copy, state);
};
// Add to history
if (commands.history.length > commands.current + 1)
commands.history.splice(commands.current + 1);
commands.history.push({undo: undoWrapper, redo: redoWrapper});
commands.current++;
};
// Add to history
if (commands.history.length > commands.current + 1)
commands.history.splice(commands.current + 1);
commands.history.push({undo: undoWrapper, redo: redoWrapper});
commands.current++;
};
this.types[name] = command;
this.types[name] = command;
return command;
},
runCommand(name, options) {
this.types[name](options);
},
types: {},
return command;
},
runCommand(name, options) {
this.types[name](options);
},
types: {},
};
/**
* Draw Image Command, used to draw a Image to a context
*/
commands.createCommand(
"drawImage",
(options, state) => {
if (
!options ||
options.image === undefined ||
options.x === undefined ||
options.y === undefined
)
throw "Command drawImage requires options in the format: {image, x, y, ctx?}";
"drawImage",
(options, state) => {
if (
!options ||
options.image === undefined ||
options.x === undefined ||
options.y === undefined
)
throw "Command drawImage requires options in the format: {image, x, y, ctx?}";
// Check if we have state
if (!state.context) {
const context = options.ctx || imgCtx;
state.context = context;
// Check if we have state
if (!state.context) {
const context = options.ctx || imgCtx;
state.context = context;
// Saving what was in the canvas before the command
const imgData = context.getImageData(
options.x,
options.y,
options.image.width,
options.image.height
);
state.box = {
x: options.x,
y: options.y,
w: options.image.width,
h: options.image.height,
};
// Create Image
const cutout = document.createElement("canvas");
cutout.width = state.box.w;
cutout.height = state.box.h;
cutout.getContext("2d").putImageData(imgData, 0, 0);
state.original = new Image();
state.original.src = cutout.toDataURL();
}
// Saving what was in the canvas before the command
const imgData = context.getImageData(
options.x,
options.y,
options.image.width,
options.image.height
);
state.box = {
x: options.x,
y: options.y,
w: options.image.width,
h: options.image.height,
};
// Create Image
const cutout = document.createElement("canvas");
cutout.width = state.box.w;
cutout.height = state.box.h;
cutout.getContext("2d").putImageData(imgData, 0, 0);
state.original = new Image();
state.original.src = cutout.toDataURL();
}
// Apply command
state.context.drawImage(options.image, state.box.x, state.box.y);
},
(state) => {
// Clear destination area
state.context.clearRect(state.box.x, state.box.y, state.box.w, state.box.h);
// Undo
state.context.drawImage(state.original, state.box.x, state.box.y);
}
// Apply command
state.context.drawImage(options.image, state.box.x, state.box.y);
},
(state) => {
// Clear destination area
state.context.clearRect(state.box.x, state.box.y, state.box.w, state.box.h);
// Undo
state.context.drawImage(state.original, state.box.x, state.box.y);
}
);

File diff suppressed because it is too large Load diff

View file

@ -1,48 +1,48 @@
dragElement(document.getElementById("infoContainer"));
function dragElement(elmnt) {
var p1 = 0,
p2 = 0,
p3 = 0,
p4 = 0;
var draggableElements = document.getElementsByClassName("draggable");
for (var i = 0; i < draggableElements.length; i++) {
draggableElements[i].onmousedown = dragMouseDown;
}
var p1 = 0,
p2 = 0,
p3 = 0,
p4 = 0;
var draggableElements = document.getElementsByClassName("draggable");
for (var i = 0; i < draggableElements.length; i++) {
draggableElements[i].onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e.preventDefault();
p3 = e.clientX;
p4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function dragMouseDown(e) {
e.preventDefault();
p3 = e.clientX;
p4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e.preventDefault();
p1 = p3 - e.clientX;
p2 = p4 - e.clientY;
elmnt.style.top = elmnt.offsetTop - (p4 - e.clientY) + "px";
elmnt.style.left = elmnt.offsetLeft - (p3 - e.clientX) + "px";
p3 = e.clientX;
p4 = e.clientY;
}
function elementDrag(e) {
e.preventDefault();
p1 = p3 - e.clientX;
p2 = p4 - e.clientY;
elmnt.style.top = elmnt.offsetTop - (p4 - e.clientY) + "px";
elmnt.style.left = elmnt.offsetLeft - (p3 - e.clientX) + "px";
p3 = e.clientX;
p4 = e.clientY;
}
function closeDragElement() {
document.onmouseup = null;
document.onmousemove = null;
}
function closeDragElement() {
document.onmouseup = null;
document.onmousemove = null;
}
}
var coll = document.getElementsByClassName("collapsible");
for (var i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function () {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.maxHeight) {
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
coll[i].addEventListener("click", function () {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.maxHeight) {
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
}