2020-04-15 03:01:31 +03:00
|
|
|
|
2020-03-08 00:34:12 +03:00
|
|
|
var emptySVG = document.getElementById("empty-button-svg");
|
|
|
|
var fullSVG = document.getElementById("full-button-svg");
|
2020-03-07 18:49:01 +03:00
|
|
|
|
2020-03-07 01:21:42 +03:00
|
|
|
var drawMode = 'empty';
|
|
|
|
var isDrawingRect = false;
|
|
|
|
|
|
|
|
let startRectX;
|
|
|
|
let startRectY;
|
|
|
|
let endRectX;
|
|
|
|
let endRectY;
|
|
|
|
|
|
|
|
|
|
|
|
function startRectDrawing(mouseEvent) {
|
2020-04-04 10:41:56 +03:00
|
|
|
// Putting the vfx layer on top of everything
|
|
|
|
VFXCanvas.style.zIndex = MAX_Z_INDEX;
|
|
|
|
// Updating flag
|
|
|
|
isDrawingRect = true;
|
2020-03-07 01:21:42 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
// Saving the start coords of the rect
|
|
|
|
let cursorPos = getCursorPosition(mouseEvent);
|
2020-04-12 12:39:37 +03:00
|
|
|
startRectX = Math.floor(cursorPos[0] / zoom) + 0.5;
|
|
|
|
startRectY = Math.floor(cursorPos[1] / zoom) + 0.5;
|
2020-03-07 01:21:42 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
drawRectangle(startRectX, startRectY);
|
2020-03-07 01:21:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateRectDrawing(mouseEvent) {
|
|
|
|
let pos = getCursorPosition(mouseEvent);
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-03-07 01:21:42 +03:00
|
|
|
// 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");
|
|
|
|
|
2020-03-07 17:37:30 +03:00
|
|
|
endRectX = Math.round(currentPos[0] / zoom) + 0.5;
|
|
|
|
endRectY = Math.round(currentPos[1] / zoom) + 0.5;
|
2020-03-07 01:21:42 +03:00
|
|
|
|
|
|
|
// 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;
|
2020-03-07 18:49:01 +03:00
|
|
|
// Drawing the rect
|
|
|
|
startRectY -= 0.5;
|
|
|
|
endRectY -= 0.5;
|
|
|
|
endRectX -= 0.5;
|
|
|
|
startRectX -= 0.5;
|
|
|
|
|
2020-04-15 03:01:31 +03:00
|
|
|
currentLayer.context.lineWidth = tool.rectangle.brushSize;
|
2020-03-07 18:49:01 +03:00
|
|
|
currentLayer.context.fillStyle = currentGlobalColor;
|
|
|
|
|
2020-04-15 03:01:31 +03:00
|
|
|
line(startRectX, startRectY, endRectX, startRectY, tool.rectangle.brushSize);
|
|
|
|
line(endRectX, startRectY, endRectX, endRectY, tool.rectangle.brushSize);
|
|
|
|
line(endRectX, endRectY, startRectX, endRectY, tool.rectangle.brushSize);
|
|
|
|
line(startRectX, endRectY, startRectX, startRectY, tool.rectangle.brushSize);
|
2020-03-07 18:49:01 +03:00
|
|
|
|
2020-03-08 00:34:12 +03:00
|
|
|
if (drawMode == 'fill') {
|
|
|
|
currentLayer.context.fillRect(startRectX, startRectY, endRectX - startRectX, endRectY - startRectY);
|
|
|
|
}
|
2020-03-07 01:21:42 +03:00
|
|
|
|
2020-03-07 17:37:30 +03:00
|
|
|
// Clearing the vfx canvas
|
|
|
|
vfxContext.clearRect(0, 0, VFXCanvas.width, VFXCanvas.height);
|
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-03-07 01:21:42 +03:00
|
|
|
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
|
2020-04-15 03:01:31 +03:00
|
|
|
vfxContext.lineWidth = tool.rectangle.brushSize;
|
2020-03-07 01:21:42 +03:00
|
|
|
vfxContext.strokeStyle = currentGlobalColor;
|
2020-03-07 17:37:30 +03:00
|
|
|
|
2020-03-07 01:21:42 +03:00
|
|
|
// Drawing the rect
|
|
|
|
vfxContext.beginPath();
|
2020-04-15 03:01:31 +03:00
|
|
|
if ((tool.rectangle.brushSize % 2 ) == 0) {
|
2020-03-15 18:32:48 +03:00
|
|
|
vfxContext.rect(startRectX - 0.5, startRectY - 0.5, x - startRectX, y - startRectY);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vfxContext.rect(startRectX, startRectY, x - startRectX, y - startRectY);
|
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-03-07 01:21:42 +03:00
|
|
|
vfxContext.setLineDash([]);
|
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
vfxContext.stroke();
|
2020-03-07 01:21:42 +03:00
|
|
|
}
|
|
|
|
|
2020-03-08 00:34:12 +03:00
|
|
|
function setRectToolSvg() {
|
2020-04-04 10:41:56 +03:00
|
|
|
if (drawMode == 'empty') {
|
|
|
|
emptySVG.setAttribute('display', 'visible');
|
|
|
|
fullSVG.setAttribute('display', 'none');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
emptySVG.setAttribute('display', 'none');
|
|
|
|
fullSVG.setAttribute('display', 'visible');
|
|
|
|
}
|
2020-03-08 00:34:12 +03:00
|
|
|
}
|
|
|
|
|
2020-03-07 01:21:42 +03:00
|
|
|
function applyChanges() {
|
2020-04-04 10:41:56 +03:00
|
|
|
VFXCanvas.style.zIndex = MIN_Z_INDEX;
|
2020-04-12 12:39:37 +03:00
|
|
|
}
|