2021-07-22 20:26:40 +03:00
|
|
|
// RECT TOOL
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// Saving the empty rect svg
|
2021-04-28 23:35:26 +03:00
|
|
|
var emptyRectangleSVG = document.getElementById("rectangle-empty-button-svg");
|
2020-12-31 18:47:56 +03:00
|
|
|
// and the full rect svg so that I can change them when the user changes rect modes
|
2021-04-28 23:35:26 +03:00
|
|
|
var fullRectangleSVG = document.getElementById("rectangle-full-button-svg");
|
2020-03-07 18:49:01 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// The start mode is empty rectangle
|
2021-04-28 23:35:26 +03:00
|
|
|
var rectangleDrawMode = 'empty';
|
2020-12-31 18:47:56 +03:00
|
|
|
// I'm not drawing a rectangle at the beginning
|
2020-03-07 01:21:42 +03:00
|
|
|
var isDrawingRect = false;
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// Rect coordinates
|
2020-03-07 01:21:42 +03:00
|
|
|
let startRectX;
|
|
|
|
let startRectY;
|
|
|
|
let endRectX;
|
|
|
|
let endRectY;
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Starts drawing the rect, saves the start coordinates
|
|
|
|
*
|
|
|
|
* @param {*} mouseEvent
|
|
|
|
*/
|
2020-03-07 01:21:42 +03:00
|
|
|
function startRectDrawing(mouseEvent) {
|
2021-07-22 19:02:19 +03:00
|
|
|
// Putting the tmp layer on top of everything
|
2021-07-22 19:11:55 +03:00
|
|
|
TMPLayer.canvas.style.zIndex = parseInt(currentLayer.canvas.style.zIndex, 10) + 1;
|
2020-04-04 10:41:56 +03:00
|
|
|
// 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
|
2021-07-22 20:05:58 +03:00
|
|
|
let cursorPos = Input.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
|
|
|
}
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Updates the rectangle preview depending on the position of the mouse
|
|
|
|
*
|
|
|
|
* @param {*} mouseEvent The mouseEvent from which we'll get the mouse position
|
|
|
|
*/
|
2020-03-07 01:21:42 +03:00
|
|
|
function updateRectDrawing(mouseEvent) {
|
2021-07-22 20:05:58 +03:00
|
|
|
let pos = Input.getCursorPosition(mouseEvent);
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// Drawing the rect at the right position
|
2021-07-01 00:06:55 +03:00
|
|
|
drawRectangle(Math.floor(pos[0] / zoom) + 0.5, Math.floor(pos[1] / zoom) + 0.5);
|
2020-03-07 01:21:42 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Finishes drawing the rect, decides the end coordinates and moves the preview rectangle to the
|
|
|
|
* current layer
|
|
|
|
*
|
|
|
|
* @param {*} mouseEvent event from which we'll get the mouse position
|
|
|
|
*/
|
2020-03-07 01:21:42 +03:00
|
|
|
function endRectDrawing(mouseEvent) {
|
|
|
|
// Getting the end position
|
2021-07-22 20:05:58 +03:00
|
|
|
let currentPos = Input.getCursorPosition(mouseEvent);
|
2021-07-22 19:11:55 +03:00
|
|
|
let tmpContext = TMPLayer.context;
|
2020-03-07 01:21:42 +03:00
|
|
|
|
2021-07-01 00:06:55 +03:00
|
|
|
endRectX = Math.floor(currentPos[0] / zoom) + 0.5;
|
|
|
|
endRectY = Math.floor(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-12-31 18:47:56 +03:00
|
|
|
// Setting the correct linewidth and colour
|
2020-04-15 03:01:31 +03:00
|
|
|
currentLayer.context.lineWidth = tool.rectangle.brushSize;
|
2020-03-07 18:49:01 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// Drawing the rect using 4 lines
|
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-12-31 18:47:56 +03:00
|
|
|
// If I have to fill it, I do so
|
2021-04-28 23:35:26 +03:00
|
|
|
if (rectangleDrawMode == 'fill') {
|
2020-03-08 00:34:12 +03:00
|
|
|
currentLayer.context.fillRect(startRectX, startRectY, endRectX - startRectX, endRectY - startRectY);
|
|
|
|
}
|
2020-03-07 01:21:42 +03:00
|
|
|
|
2021-07-22 19:02:19 +03:00
|
|
|
// Clearing the tmp canvas
|
2021-07-22 19:11:55 +03:00
|
|
|
tmpContext.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height);
|
2020-03-07 17:37:30 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2021-07-22 19:02:19 +03:00
|
|
|
/** Draws a rectangle with end coordinates given by x and y on the tmp layer (draws
|
2020-12-31 18:47:56 +03:00
|
|
|
* the preview for the rectangle tool)
|
|
|
|
*
|
|
|
|
* @param {*} x The current end x of the rectangle
|
|
|
|
* @param {*} y The current end y of the rectangle
|
|
|
|
*/
|
2020-03-07 01:21:42 +03:00
|
|
|
function drawRectangle(x, y) {
|
2021-07-22 19:02:19 +03:00
|
|
|
// Getting the tmp context
|
2021-07-22 19:11:55 +03:00
|
|
|
let tmpContext = TMPLayer.context;
|
2020-03-07 01:21:42 +03:00
|
|
|
|
2021-07-22 19:02:19 +03:00
|
|
|
// Clearing the tmp canvas
|
2021-07-22 19:11:55 +03:00
|
|
|
tmpContext.clearRect(0, 0, TMPLayer.canvas.width, TMPLayer.canvas.height);
|
2020-03-07 01:21:42 +03:00
|
|
|
|
|
|
|
// Drawing the rect
|
2021-07-22 19:02:19 +03:00
|
|
|
tmpContext.lineWidth = tool.rectangle.brushSize;
|
2020-03-07 17:37:30 +03:00
|
|
|
|
2020-03-07 01:21:42 +03:00
|
|
|
// Drawing the rect
|
2021-07-22 19:02:19 +03:00
|
|
|
tmpContext.beginPath();
|
2020-04-15 03:01:31 +03:00
|
|
|
if ((tool.rectangle.brushSize % 2 ) == 0) {
|
2021-07-22 19:02:19 +03:00
|
|
|
tmpContext.rect(startRectX - 0.5, startRectY - 0.5, x - startRectX, y - startRectY);
|
2020-03-15 18:32:48 +03:00
|
|
|
}
|
|
|
|
else {
|
2021-07-22 19:02:19 +03:00
|
|
|
tmpContext.rect(startRectX, startRectY, x - startRectX, y - startRectY);
|
2020-03-15 18:32:48 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2021-07-22 19:02:19 +03:00
|
|
|
tmpContext.setLineDash([]);
|
|
|
|
tmpContext.stroke();
|
2020-03-07 01:21:42 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Sets the correct tool icon depending on its mode
|
|
|
|
*
|
|
|
|
*/
|
2020-03-08 00:34:12 +03:00
|
|
|
function setRectToolSvg() {
|
2021-04-28 23:35:26 +03:00
|
|
|
if (rectangleDrawMode == 'empty') {
|
|
|
|
emptyRectangleSVG.setAttribute('display', 'visible');
|
|
|
|
fullRectangleSVG.setAttribute('display', 'none');
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|
|
|
|
else {
|
2021-04-28 23:35:26 +03:00
|
|
|
emptyRectangleSVG.setAttribute('display', 'none');
|
|
|
|
fullRectangleSVG.setAttribute('display', 'visible');
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|
2021-07-22 19:11:55 +03:00
|
|
|
}
|