Added history states for canvas resizing

Must fix bug that happens when creating a new layer after resizing the canvas
This commit is contained in:
unsettledgames
2020-09-15 13:06:31 +02:00
parent 663b714b46
commit b7d5f603b1
6 changed files with 71 additions and 12 deletions

View File

@ -64,14 +64,21 @@ function changedSize(event) {
borders.bottom = bottom;
}
function resizeCanvas(event) {
function resizeCanvas(event, size) {
let imageDatas = [];
let leftOffset = 0;
let topOffset = 0;
let copiedDataIndex = 0;
// If I'm undoing, I manually put the values in the window
if (size !== undefined) {
document.getElementById("rc-width").value = size.x;
document.getElementById("rc-height").value = size.y;
changedSize();
}
rcUpdateBorders();
changedSize();
// Save all imageDatas
for (let i=0; i<layers.length; i++) {
@ -80,6 +87,19 @@ function resizeCanvas(event) {
}
}
// Saving the history only if I'm not already undoing or redoing
if (size == undefined) {
// Saving history
new HistoryStateResizeCanvas(
{x: parseInt(layers[0].canvasSize[0]) + borders.left + borders.right,
y: parseInt(layers[0].canvasSize[1]) + borders.top + borders.bottom},
{x: layers[0].canvasSize[0],
y: layers[0].canvasSize[1]},
imageDatas
);
}
// Resize the canvases
for (let i=0; i<layers.length; i++) {
layers[i].canvasSize[0] = parseInt(layers[i].canvasSize[0]) + borders.left + borders.right;
@ -135,13 +155,14 @@ function resizeCanvas(event) {
topOffset = borders.top + borders.bottom;
break;
default:
console.log('now thats a very weird pivot');
console.log('Pivot does not exist, please report an issue at https://github.com/lospec/pixel-editor');
break;
}
for (let i=0; i<layers.length; i++) {
if (layers[i].menuEntry != null) {
layers[i].context.putImageData(imageDatas[copiedDataIndex], leftOffset, topOffset);
layers[i].updateLayerPreview();
copiedDataIndex++;
}
}