Almost finished recangle tool

There's a Math.round bug, sometimes the opacity is off.
This commit is contained in:
unsettledgames
2020-03-06 23:21:42 +01:00
parent 0786f99d76
commit fd37518e55
11 changed files with 149 additions and 19 deletions

90
js/_rectangle.js Normal file
View 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;
}