mirror of
https://github.com/lospec/pixel-editor.git
synced 2023-08-10 21:12:51 +03:00
Finished preview rectangle
When the rectangle selection tool is selected, a rect preview is drawn on the vfx layer. There's also a function that gets the selected pixels, next step is moving them around.
This commit is contained in:
parent
8c94a951e2
commit
c714f5c004
@ -20,6 +20,8 @@ function getCursorPosition(e) {
|
|||||||
|
|
||||||
// TODO: apply the function below to every getCursorPosition call
|
// TODO: apply the function below to every getCursorPosition call
|
||||||
|
|
||||||
|
// TODO: FIX THIS BELOW
|
||||||
|
|
||||||
//get cursor position relative to canvas
|
//get cursor position relative to canvas
|
||||||
function getCursorPositionRelative(e, layer) {
|
function getCursorPositionRelative(e, layer) {
|
||||||
var x;
|
var x;
|
||||||
|
1
js/_move.js
Normal file
1
js/_move.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
var imageDataToMove;
|
@ -1,32 +1,54 @@
|
|||||||
var isRectSelecting = false;
|
var isRectSelecting = false;
|
||||||
let startX;
|
let startX;
|
||||||
let startY;
|
let startY;
|
||||||
|
let workingLayer;
|
||||||
|
let imageData;
|
||||||
|
|
||||||
function startRectSelection(mouseEvent) {
|
function startRectSelection(mouseEvent) {
|
||||||
// Putting the vfx layer on top of everything
|
// Putting the vfx layer on top of everything
|
||||||
VFXCanvas.style.zIndex = MAX_Z_INDEX;
|
VFXCanvas.style.zIndex = MAX_Z_INDEX;
|
||||||
console.log("Started selecting");
|
// Saving the layer the user is working on
|
||||||
|
workingLayer = currentLayer;
|
||||||
|
|
||||||
// Saving the start coords of the rect
|
// Saving the start coords of the rect
|
||||||
cursorPos = getCursorPositionRelative(mouseEvent, VFXCanvas)
|
let cursorPos = getCursorPosition(mouseEvent);
|
||||||
startX = cursorPos[0];
|
startX = Math.floor(cursorPos[0] / zoom) + 0.5;
|
||||||
startY = cursorPos[1];
|
startY = Math.floor(cursorPos[1] / zoom) + 0.5;
|
||||||
|
|
||||||
// Drawing the rect
|
// Drawing the rect
|
||||||
drawRect(startX, startY);
|
drawRect(startX, startY);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateRectSelection(mouseEvent) {
|
function updateRectSelection(mouseEvent) {
|
||||||
pos = getCursorPositionRelative(mouseEvent, VFXCanvas);
|
let pos = getCursorPosition(mouseEvent);
|
||||||
|
|
||||||
// Drawing the rect
|
// Drawing the rect
|
||||||
drawRect(pos[0], pos[1]);
|
drawRect(Math.floor(pos[0] / zoom) + 0.5, Math.floor(pos[1] / zoom) + 0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
function endRectSelection(mouseEvent) {
|
function endRectSelection(mouseEvent) {
|
||||||
console.log("Finished selecting");
|
// Getting the end position
|
||||||
|
let currentPos = getCursorPosition(mouseEvent);
|
||||||
|
let x = currentPos[0] / zoom;
|
||||||
|
let y = currentPos[1] / zoom;
|
||||||
|
|
||||||
// Putting the vfx layer behind everything
|
// Putting the vfx layer behind everything
|
||||||
//VFXCanvas.style.zIndex = MIN_Z_INDEX;
|
|
||||||
isRectSelecting = false;
|
isRectSelecting = false;
|
||||||
|
// Getting the selected pixels
|
||||||
|
imageData = currentLayer.context.createImageData(x - startX, y - startY);
|
||||||
|
|
||||||
|
// NOW
|
||||||
|
// Select the move tool
|
||||||
|
// the move tool moves stuff only if the cursor is on the selected area
|
||||||
|
// the move tool stops working when esc is pressed
|
||||||
|
// when the move tool is disabled, the control is given to the brush tool
|
||||||
|
// on click: start dragging
|
||||||
|
// on drag: render preview by changing the position of the image data and change the position of the selection rect
|
||||||
|
// IMPORTANT: the image data must be deleted from the original layer
|
||||||
|
// the image data must be copied to a temporary layer
|
||||||
|
// the image data is added to the original layer when the move tool is disabled
|
||||||
|
|
||||||
|
workingLayer.context.putImageData(imageData, startX, startY);
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawRect(x, y) {
|
function drawRect(x, y) {
|
||||||
@ -36,10 +58,18 @@ function drawRect(x, y) {
|
|||||||
|
|
||||||
// Clearing the vfx canvas
|
// Clearing the vfx canvas
|
||||||
vfxContext.clearRect(0, 0, VFXCanvas.width, VFXCanvas.height);
|
vfxContext.clearRect(0, 0, VFXCanvas.width, VFXCanvas.height);
|
||||||
|
vfxContext.lineWidth = 1;
|
||||||
|
vfxContext.setLineDash([4]);
|
||||||
|
|
||||||
// Drawing the rect
|
// Drawing the rect
|
||||||
vfxContext.beginPath();
|
vfxContext.beginPath();
|
||||||
vfxContext.rect(startX, startY, x - startX, y - startY);
|
vfxContext.rect(startX, startY, x - startX, y - startY);
|
||||||
|
|
||||||
vfxContext.stroke();
|
vfxContext.stroke();
|
||||||
|
|
||||||
|
// TODO: make the rect blink from black to white in case of dark backgrounds
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: esc to exit selection mode
|
function applyChanges() {
|
||||||
|
VFXCanvas.style.zIndex = MIN_Z_INDEX;
|
||||||
|
}
|
@ -61,6 +61,7 @@
|
|||||||
//=include _fileMenu.js
|
//=include _fileMenu.js
|
||||||
//=include _createButton.js
|
//=include _createButton.js
|
||||||
//=include _rectSelect.js
|
//=include _rectSelect.js
|
||||||
|
//=include _move.js
|
||||||
|
|
||||||
|
|
||||||
/**onload**/
|
/**onload**/
|
||||||
|
Loading…
Reference in New Issue
Block a user