pixel-editor/js/_rectSelect.js
unsettledgames 8c94a951e2 Started implementing preview rect
At the moment there's a bug in the getCursorPos function, may have something to do with offsets. The rect is correctly drawn, but with a weird offset.

Added vfx canvas for tools previews.
2020-03-04 15:46:25 +01:00

45 lines
1.1 KiB
JavaScript

var isRectSelecting = false;
let startX;
let startY;
function startRectSelection(mouseEvent) {
// Putting the vfx layer on top of everything
VFXCanvas.style.zIndex = MAX_Z_INDEX;
console.log("Started selecting");
// Saving the start coords of the rect
cursorPos = getCursorPositionRelative(mouseEvent, VFXCanvas)
startX = cursorPos[0];
startY = cursorPos[1];
// Drawing the rect
drawRect(startX, startY);
}
function updateRectSelection(mouseEvent) {
pos = getCursorPositionRelative(mouseEvent, VFXCanvas);
// Drawing the rect
drawRect(pos[0], pos[1]);
}
function endRectSelection(mouseEvent) {
console.log("Finished selecting");
// Putting the vfx layer behind everything
//VFXCanvas.style.zIndex = MIN_Z_INDEX;
isRectSelecting = false;
}
function drawRect(x, y) {
console.log("Currently selecting");
// Getting the vfx context
let vfxContext = VFXCanvas.getContext("2d");
// Clearing the vfx canvas
vfxContext.clearRect(0, 0, VFXCanvas.width, VFXCanvas.height);
// Drawing the rect
vfxContext.beginPath();
vfxContext.rect(startX, startY, x - startX, y - startY);
vfxContext.stroke();
}
// TODO: esc to exit selection mode