mirror of
https://github.com/lospec/pixel-editor.git
synced 2023-08-10 21:12:51 +03:00
Almost finished recangle tool
There's a Math.round bug, sometimes the opacity is off.
This commit is contained in:
90
js/_rectangle.js
Normal file
90
js/_rectangle.js
Normal file
@@ -0,0 +1,90 @@
|
||||
var rectangleSize = 1;
|
||||
var drawMode = 'empty';
|
||||
var isDrawingRect = false;
|
||||
|
||||
let startRectX;
|
||||
let startRectY;
|
||||
let endRectX;
|
||||
let endRectY;
|
||||
|
||||
|
||||
function startRectDrawing(mouseEvent) {
|
||||
// Putting the vfx layer on top of everything
|
||||
VFXCanvas.style.zIndex = MAX_Z_INDEX;
|
||||
// Updating flag
|
||||
isDrawingRect = true;
|
||||
|
||||
// Saving the start coords of the rect
|
||||
let cursorPos = getCursorPosition(mouseEvent);
|
||||
startRectX = Math.round(cursorPos[0] / zoom) + 0.5;
|
||||
startRectY = Math.round(cursorPos[1] / zoom) + 0.5;
|
||||
|
||||
drawRectangle(startRectX, startRectY);
|
||||
}
|
||||
|
||||
function updateRectDrawing(mouseEvent) {
|
||||
let pos = getCursorPosition(mouseEvent);
|
||||
|
||||
// Drawing the rect
|
||||
drawRectangle(Math.round(pos[0] / zoom) + 0.5, Math.round(pos[1] / zoom) + 0.5);
|
||||
}
|
||||
|
||||
function endRectDrawing(mouseEvent) {
|
||||
// Getting the end position
|
||||
let currentPos = getCursorPosition(mouseEvent);
|
||||
let vfxContext = VFXCanvas.getContext("2d");
|
||||
|
||||
endRectX = Math.round(currentPos[0] / zoom);
|
||||
endRectY = Math.round(currentPos[1] / zoom);
|
||||
|
||||
// Inverting end and start (start must always be the top left corner)
|
||||
if (endRectX < startRectX) {
|
||||
let tmp = endRectX;
|
||||
endRectX = startRectX;
|
||||
startRectX = tmp;
|
||||
}
|
||||
// Same for the y
|
||||
if (endRectY < startRectY) {
|
||||
let tmp = endRectY;
|
||||
endRectY = startRectY;
|
||||
startRectY = tmp;
|
||||
}
|
||||
|
||||
// Resetting this
|
||||
isDrawingRect = false;
|
||||
// Drawing the final rectangle
|
||||
currentLayer.context.lineWidth = rectangleSize;
|
||||
currentLayer.context.strokeStyle = currentGlobalColor;
|
||||
|
||||
// Drawing the rect
|
||||
currentLayer.context.beginPath();
|
||||
currentLayer.context.rect(startRectX, startRectY, endRectX - startRectX + 0.5, endRectY - startRectY + 0.5);
|
||||
currentLayer.context.setLineDash([]);
|
||||
|
||||
currentLayer.context.stroke();
|
||||
}
|
||||
|
||||
function drawRectangle(x, y) {
|
||||
// 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.lineWidth = rectangleSize;
|
||||
vfxContext.strokeStyle = currentGlobalColor;
|
||||
console.log("color: " + currentGlobalColor);
|
||||
// Drawing the rect
|
||||
vfxContext.beginPath();
|
||||
vfxContext.rect(startRectX, startRectY, x - startRectX, y - startRectY);
|
||||
vfxContext.setLineDash([]);
|
||||
|
||||
vfxContext.stroke();
|
||||
|
||||
// TODO: make the rect blink from black to white in case of dark backgrounds
|
||||
}
|
||||
|
||||
function applyChanges() {
|
||||
VFXCanvas.style.zIndex = MIN_Z_INDEX;
|
||||
}
|
Reference in New Issue
Block a user