pixel-editor/js/_move.js

86 lines
3.0 KiB
JavaScript
Raw Normal View History

var imageDataToMove;
2020-03-15 19:11:00 +03:00
var originalDataPosition;
2020-03-05 18:13:23 +03:00
var canMoveSelection = false;
var lastMovePos;
var selectionCanceled = true;
var firstTimeMove = true;
2020-03-05 18:13:23 +03:00
// TODO: move with arrows
function updateMovePreview(mousePosition) {
2020-04-04 10:41:56 +03:00
selectionCanceled = false;
2020-04-04 10:41:56 +03:00
if (firstTimeMove) {
cutSelection(mousePosition);
2020-04-04 10:41:56 +03:00
}
2020-04-04 10:41:56 +03:00
firstTimeMove = false;
lastMousePos = mousePosition;
2020-04-04 10:41:56 +03:00
// clear the entire tmp layer
TMPLayer.context.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height);
// put the image data with offset
TMPLayer.context.putImageData(
imageDataToMove,
Math.round(lastMousePos[0] / zoom) - imageDataToMove.width / 2,
Math.round(lastMousePos[1] / zoom) - imageDataToMove.height / 2);
2020-03-05 18:13:23 +03:00
2020-04-04 10:41:56 +03:00
lastMovePos = lastMousePos;
moveSelection(lastMousePos[0] / zoom, lastMousePos[1] / zoom, imageDataToMove.width, imageDataToMove.height);
2020-03-05 18:13:23 +03:00
}
function endSelection() {
2020-04-04 10:41:56 +03:00
TMPLayer.context.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height);
VFXLayer.context.clearRect(0, 0, VFXLayer.canvas.width, VFXLayer.canvas.height);
let cleanImageData = new ImageData(endX - startX, endY - startY);
2020-03-05 18:13:23 +03:00
2020-04-04 10:41:56 +03:00
if (imageDataToMove !== undefined) {
// Saving the current clipboard before editing it in order to merge it with the current layer
cleanImageData.data.set(imageDataToMove.data);
2020-04-04 10:41:56 +03:00
let underlyingImageData = currentLayer.context.getImageData(startX, startY, endX - startX, endY - startY);
2020-04-04 10:41:56 +03:00
for (let i=0; i<underlyingImageData.data.length; i+=4) {
let currentMovePixel = [
imageDataToMove.data[i], imageDataToMove.data[i+1],
imageDataToMove.data[i+2], imageDataToMove.data[i+3]
];
2020-04-04 10:41:56 +03:00
let currentUnderlyingPixel = [
underlyingImageData.data[i], underlyingImageData.data[i+1],
underlyingImageData.data[i+2], underlyingImageData.data[i+3]
];
2020-04-04 10:41:56 +03:00
if (isPixelEmpty(currentMovePixel)) {
if (!isPixelEmpty(underlyingImageData)) {
imageDataToMove.data[i] = currentUnderlyingPixel[0];
imageDataToMove.data[i+1] = currentUnderlyingPixel[1];
imageDataToMove.data[i+2] = currentUnderlyingPixel[2];
imageDataToMove.data[i+3] = currentUnderlyingPixel[3];
}
}
}
2020-04-04 10:41:56 +03:00
if (lastMovePos !== undefined) {
currentLayer.context.putImageData(
imageDataToMove,
Math.round(lastMovePos[0] / zoom) - imageDataToMove.width / 2,
Math.round(lastMovePos[1] / zoom) - imageDataToMove.height / 2);
2020-04-04 10:41:56 +03:00
}
else {
console.log("yo");
currentLayer.context.putImageData(
imageDataToMove,
copiedStartX,
copiedStartY);
}
imageDataToMove.data.set(cleanImageData.data);
2020-04-04 10:41:56 +03:00
}
2020-04-04 10:41:56 +03:00
selectionCanceled = true;
isRectSelecting = false;
firstTimeMove = true;
imageDataToMove = undefined;
isPasting = false;
isCutting = false;
}