2020-03-05 15:34:29 +03:00
|
|
|
var currentMouseEvent;
|
2020-07-21 17:01:00 +03:00
|
|
|
var lastMouseMovePos;
|
2019-03-27 02:20:54 +03:00
|
|
|
|
|
|
|
//mousedown - start drawing
|
|
|
|
window.addEventListener("mousedown", function (mouseEvent) {
|
2020-03-05 15:34:29 +03:00
|
|
|
// Saving the event in case something else needs it
|
|
|
|
currentMouseEvent = mouseEvent;
|
2020-04-04 16:56:44 +03:00
|
|
|
canDraw = true;
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-06-19 13:57:07 +03:00
|
|
|
//if no document has been created yet, or this is a dialog open, or the currentLayer is locked
|
2020-06-25 13:10:34 +03:00
|
|
|
if (!documentCreated || dialogueOpen || currentLayer.isLocked || !currentLayer.isVisible) return;
|
2019-03-27 02:20:54 +03:00
|
|
|
//prevent right mouse clicks and such, which will open unwanted menus
|
|
|
|
//mouseEvent.preventDefault();
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-07-21 17:01:00 +03:00
|
|
|
lastMouseClickPos = getCursorPosition(mouseEvent);
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
dragging = true;
|
2020-03-07 18:49:01 +03:00
|
|
|
//left or right click ?
|
2019-03-27 02:20:54 +03:00
|
|
|
if (mouseEvent.which == 1) {
|
2020-04-15 03:01:31 +03:00
|
|
|
if (spacePressed)
|
|
|
|
currentTool = tool.pan;
|
|
|
|
else if (mouseEvent.altKey)
|
|
|
|
currentTool = tool.eyedropper;
|
|
|
|
else if (mouseEvent.target.className == 'drawingCanvas' &&
|
|
|
|
(currentTool.name == 'pencil' || currentTool.name == 'eraser' || currentTool.name == 'rectangle'))
|
2019-03-27 02:20:54 +03:00
|
|
|
new HistoryStateEditCanvas();
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'moveselection') {
|
2020-06-06 22:44:52 +03:00
|
|
|
if (!cursorInSelectedArea() &&
|
|
|
|
((mouseEvent.target.id == 'canvas-view') || mouseEvent.target.className == 'drawingCanvas')) {
|
2020-04-15 03:01:31 +03:00
|
|
|
tool.pencil.switchTo();
|
2020-04-04 16:56:44 +03:00
|
|
|
canDraw = false;
|
2020-03-15 19:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 19:10:10 +03:00
|
|
|
|
2020-04-15 03:01:31 +03:00
|
|
|
currentTool.updateCursor();
|
|
|
|
|
2020-06-25 13:10:34 +03:00
|
|
|
if (!currentLayer.isLocked && !currentLayer.isVisible && canDraw) {
|
2020-04-04 16:56:44 +03:00
|
|
|
draw(mouseEvent);
|
|
|
|
}
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'pencil' && mouseEvent.which == 3) {
|
|
|
|
currentTool = tool.resizebrush;
|
|
|
|
tool.pencil.previousBrushSize = tool.pencil.brushSize;
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'eraser' && mouseEvent.which == 3) {
|
|
|
|
currentTool = tool.resizeeraser;
|
|
|
|
tool.eraser.previousBrushSize = tool.eraser.brushSize;
|
2019-04-02 20:42:28 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'rectangle' && mouseEvent.which == 3) {
|
|
|
|
currentTool = tool.resizerectangle;
|
|
|
|
tool.rectangle.previousBrushSize = tool.rectangle.brushSize;
|
2020-03-07 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 03:01:31 +03:00
|
|
|
if (currentTool.name == 'eyedropper' && mouseEvent.target.className == 'drawingCanvas')
|
2019-03-27 02:20:54 +03:00
|
|
|
eyedropperPreview.style.display = 'block';
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
return false;
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//mouseup - end drawing
|
|
|
|
window.addEventListener("mouseup", function (mouseEvent) {
|
2020-03-05 15:34:29 +03:00
|
|
|
// Saving the event in case something else needs it
|
|
|
|
currentMouseEvent = mouseEvent;
|
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
closeMenu();
|
2020-06-23 00:07:40 +03:00
|
|
|
|
2020-06-23 18:34:03 +03:00
|
|
|
if (currentLayer != null && !isChildOfByClass(mouseEvent.target, "layers-menu-entry")) {
|
2020-06-23 00:07:40 +03:00
|
|
|
currentLayer.closeOptionsMenu();
|
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-06-25 13:10:34 +03:00
|
|
|
if (!documentCreated || dialogueOpen || !currentLayer.isVisible || currentLayer.isLocked) return;
|
2020-04-15 03:01:31 +03:00
|
|
|
|
|
|
|
if (currentTool.name == 'eyedropper' && mouseEvent.target.className == 'drawingCanvas') {
|
2019-03-27 02:20:54 +03:00
|
|
|
var cursorLocation = getCursorPosition(mouseEvent);
|
2020-06-20 00:31:36 +03:00
|
|
|
var selectedColor = getEyedropperColor(cursorLocation);
|
|
|
|
var newColor = rgbToHex(selectedColor[0],selectedColor[1],selectedColor[2]);
|
2020-03-07 01:21:42 +03:00
|
|
|
|
|
|
|
currentGlobalColor = "#" + newColor;
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-06-20 00:31:36 +03:00
|
|
|
for (let i=1; i<layers.length - 1; i++) {
|
|
|
|
layers[i].context.fillStyle = currentGlobalColor;
|
|
|
|
}
|
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
var colors = document.getElementsByClassName('color-button');
|
2020-03-04 16:36:40 +03:00
|
|
|
for (var i = 0; i < colors.length; i++) {
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-03-04 16:36:40 +03:00
|
|
|
//if picked color matches this color
|
|
|
|
if (newColor == colors[i].jscolor.toString()) {
|
|
|
|
//remove current color selection
|
|
|
|
var selectedColor = document.querySelector("#colors-menu li.selected")
|
|
|
|
if (selectedColor) selectedColor.classList.remove("selected");
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-03-04 16:36:40 +03:00
|
|
|
//set current color
|
2020-06-19 13:57:07 +03:00
|
|
|
|
2020-06-26 18:38:39 +03:00
|
|
|
for (let i=2; i<layers.length; i++) {
|
|
|
|
layers[i].context.fillStyle = '#' + newColor;
|
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-03-04 16:36:40 +03:00
|
|
|
//make color selected
|
|
|
|
colors[i].parentElement.classList.add('selected');
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-03-04 16:36:40 +03:00
|
|
|
//hide eyedropper
|
|
|
|
eyedropperPreview.style.display = 'none';
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'fill' && mouseEvent.target.className == 'drawingCanvas') {
|
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//get cursor postion
|
|
|
|
var cursorLocation = getCursorPosition(mouseEvent);
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//offset to match cursor point
|
|
|
|
cursorLocation[0] += 2;
|
|
|
|
cursorLocation[1] += 12;
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-06-19 16:16:22 +03:00
|
|
|
//fill starting at the location
|
2019-03-27 02:20:54 +03:00
|
|
|
fill(cursorLocation);
|
2020-06-19 16:16:22 +03:00
|
|
|
currentLayer.updateLayerPreview();
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'zoom' && mouseEvent.target.className == 'drawingCanvas') {
|
2019-03-31 18:15:03 +03:00
|
|
|
let mode;
|
|
|
|
if (mouseEvent.which == 1){
|
|
|
|
mode = "in";
|
|
|
|
}
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|
|
|
|
else if (currentTool == 'zoom' && mouseEvent.target.className == 'drawingCanvas') {
|
|
|
|
let mode;
|
|
|
|
if (mouseEvent.which == 1){
|
|
|
|
mode = 'in';
|
|
|
|
}
|
|
|
|
else if (mouseEvent.which == 3){
|
|
|
|
mode = 'out';
|
2019-03-31 18:15:03 +03:00
|
|
|
}
|
|
|
|
|
2019-03-31 19:41:08 +03:00
|
|
|
changeZoom(layers[0], mode, getCursorPosition(mouseEvent));
|
|
|
|
|
|
|
|
for (let i=1; i<layers.length; i++) {
|
2020-04-04 10:41:56 +03:00
|
|
|
layers[i].copyData(layers[0]);
|
2019-03-31 18:15:03 +03:00
|
|
|
}
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'rectselect' && isRectSelecting) {
|
2020-03-04 17:46:25 +03:00
|
|
|
endRectSelection(mouseEvent);
|
2020-03-04 16:36:40 +03:00
|
|
|
}
|
2020-07-21 15:42:25 +03:00
|
|
|
else if (currentTool.name == 'rectangle' && isDrawingRect) {
|
2020-03-07 01:21:42 +03:00
|
|
|
endRectDrawing(mouseEvent);
|
2020-06-19 16:16:22 +03:00
|
|
|
currentLayer.updateLayerPreview();
|
2020-03-07 01:21:42 +03:00
|
|
|
}
|
2019-03-27 02:20:54 +03:00
|
|
|
|
|
|
|
dragging = false;
|
|
|
|
currentTool = currentToolTemp;
|
|
|
|
|
2020-04-15 03:01:31 +03:00
|
|
|
currentTool.updateCursor();
|
|
|
|
|
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
}, false);
|
|
|
|
|
2020-06-19 13:57:07 +03:00
|
|
|
// TODO: Make it snap to the pixel grid
|
2020-04-04 21:48:54 +03:00
|
|
|
function setPreviewPosition(preview, cursor, size){
|
|
|
|
preview.style.left = (
|
|
|
|
currentLayer.canvas.offsetLeft
|
|
|
|
+ Math.floor(cursor[0]/zoom) * zoom
|
|
|
|
- Math.floor(size / 2) * zoom
|
|
|
|
) + 'px';
|
|
|
|
preview.style.top = (
|
|
|
|
currentLayer.canvas.offsetTop
|
|
|
|
+ Math.floor(cursor[1]/zoom) * zoom
|
|
|
|
- Math.floor(size / 2) * zoom
|
|
|
|
) + 'px';
|
|
|
|
}
|
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
|
2020-04-09 17:48:19 +03:00
|
|
|
// OPTIMIZABLE: redundant || mouseEvent.target.className in currentTool ifs
|
|
|
|
|
2020-04-15 03:01:31 +03:00
|
|
|
//mouse is moving on canvas
|
2019-03-27 02:20:54 +03:00
|
|
|
window.addEventListener("mousemove", draw, false);
|
|
|
|
function draw (mouseEvent) {
|
2020-07-21 17:01:00 +03:00
|
|
|
lastMouseMovePos = getCursorPosition(mouseEvent);
|
2020-03-05 15:34:29 +03:00
|
|
|
// Saving the event in case something else needs it
|
|
|
|
currentMouseEvent = mouseEvent;
|
|
|
|
|
2020-07-21 17:01:00 +03:00
|
|
|
var cursorLocation = lastMouseMovePos;
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-06-19 13:57:07 +03:00
|
|
|
//if a document hasnt yet been created or the current layer is locked, exit this function
|
2020-06-25 13:10:34 +03:00
|
|
|
if (!documentCreated || dialogueOpen || !currentLayer.isVisible || currentLayer.isLocked) return;
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-07-21 17:01:00 +03:00
|
|
|
// Moving brush preview
|
|
|
|
currentTool.moveBrushPreview(cursorLocation);
|
|
|
|
// Hiding eyedropper, will be shown if it's needed
|
2019-03-27 02:20:54 +03:00
|
|
|
eyedropperPreview.style.display = 'none';
|
2020-04-15 03:01:31 +03:00
|
|
|
|
|
|
|
if (currentTool.name == 'pencil') {
|
2019-03-27 02:20:54 +03:00
|
|
|
//hide brush preview outside of canvas / canvas view
|
2020-03-05 18:13:23 +03:00
|
|
|
if (mouseEvent.target.className == 'drawingCanvas'|| mouseEvent.target.className == 'drawingCanvas')
|
2019-03-27 02:20:54 +03:00
|
|
|
brushPreview.style.visibility = 'visible';
|
|
|
|
else
|
|
|
|
brushPreview.style.visibility = 'hidden';
|
|
|
|
|
|
|
|
//draw line to current pixel
|
|
|
|
if (dragging) {
|
2020-03-05 18:13:23 +03:00
|
|
|
if (mouseEvent.target.className == 'drawingCanvas' || mouseEvent.target.className == 'drawingCanvas') {
|
2020-07-21 17:01:00 +03:00
|
|
|
line(Math.floor(lastMouseClickPos[0]/zoom),Math.floor(lastMouseClickPos[1]/zoom),Math.floor(cursorLocation[0]/zoom),Math.floor(cursorLocation[1]/zoom), tool.pencil.brushSize);
|
|
|
|
lastMouseClickPos = cursorLocation;
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//get lightness value of color
|
2020-07-21 17:01:00 +03:00
|
|
|
var selectedColor = currentLayer.context.getImageData(Math.floor(cursorLocation[0]/zoom),Math.floor(cursorLocation[1]/zoom),1,1).data;
|
2019-03-27 02:20:54 +03:00
|
|
|
var colorLightness = Math.max(selectedColor[0],selectedColor[1],selectedColor[2])
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//for the darkest 50% of colors, change the brush preview to dark mode
|
|
|
|
if (colorLightness>127) brushPreview.classList.remove('dark');
|
|
|
|
else brushPreview.classList.add('dark');
|
2020-06-19 16:16:22 +03:00
|
|
|
|
|
|
|
currentLayer.updateLayerPreview();
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
2019-03-31 14:28:46 +03:00
|
|
|
// Decided to write a different implementation in case of differences between the brush and the eraser tool
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'eraser') {
|
2019-03-31 14:28:46 +03:00
|
|
|
//hide brush preview outside of canvas / canvas view
|
2020-03-05 18:13:23 +03:00
|
|
|
if (mouseEvent.target.className == 'drawingCanvas' || mouseEvent.target.className == 'drawingCanvas')
|
2019-03-31 14:28:46 +03:00
|
|
|
brushPreview.style.visibility = 'visible';
|
|
|
|
else
|
|
|
|
brushPreview.style.visibility = 'hidden';
|
|
|
|
|
|
|
|
//draw line to current pixel
|
|
|
|
if (dragging) {
|
2020-03-05 18:13:23 +03:00
|
|
|
if (mouseEvent.target.className == 'drawingCanvas' || mouseEvent.target.className == 'drawingCanvas') {
|
2020-07-21 17:01:00 +03:00
|
|
|
line(Math.floor(lastMouseClickPos[0]/zoom),Math.floor(lastMouseClickPos[1]/zoom),Math.floor(cursorLocation[0]/zoom),Math.floor(cursorLocation[1]/zoom), currentTool.brushSize);
|
|
|
|
lastMouseClickPos = cursorLocation;
|
2019-03-31 14:28:46 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-19 16:16:22 +03:00
|
|
|
|
|
|
|
currentLayer.updateLayerPreview();
|
2019-03-31 14:28:46 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'rectangle')
|
2020-03-07 01:21:42 +03:00
|
|
|
{
|
2020-03-07 18:49:01 +03:00
|
|
|
//hide brush preview outside of canvas / canvas view
|
|
|
|
if (mouseEvent.target.className == 'drawingCanvas'|| mouseEvent.target.className == 'drawingCanvas')
|
|
|
|
brushPreview.style.visibility = 'visible';
|
|
|
|
else
|
|
|
|
brushPreview.style.visibility = 'hidden';
|
|
|
|
|
2020-03-07 01:21:42 +03:00
|
|
|
if (!isDrawingRect && dragging) {
|
|
|
|
startRectDrawing(mouseEvent);
|
|
|
|
}
|
|
|
|
else if (dragging){
|
|
|
|
updateRectDrawing(mouseEvent);
|
|
|
|
}
|
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'pan' && dragging) {
|
2019-04-01 22:37:53 +03:00
|
|
|
// Setting first layer position
|
2020-07-21 17:01:00 +03:00
|
|
|
setCanvasOffset(layers[0].canvas, layers[0].canvas.offsetLeft + (cursorLocation[0] - lastMouseClickPos[0]), layers[0].canvas.offsetTop + (cursorLocation[1] - lastMouseClickPos[1]));
|
2020-04-04 10:41:56 +03:00
|
|
|
// Copying that position to the other layers
|
2019-04-01 22:37:53 +03:00
|
|
|
for (let i=1; i<layers.length; i++) {
|
2019-03-31 19:48:15 +03:00
|
|
|
layers[i].copyData(layers[0]);
|
2019-03-31 18:15:03 +03:00
|
|
|
}
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'eyedropper' && dragging && mouseEvent.target.className == 'drawingCanvas') {
|
2020-06-20 00:31:36 +03:00
|
|
|
let selectedColor = getEyedropperColor(cursorLocation);
|
2020-03-07 01:21:42 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
eyedropperPreview.style.borderColor = '#'+rgbToHex(selectedColor[0],selectedColor[1],selectedColor[2]);
|
|
|
|
eyedropperPreview.style.display = 'block';
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-31 17:32:49 +03:00
|
|
|
eyedropperPreview.style.left = cursorLocation[0] + currentLayer.canvas.offsetLeft - 30 + 'px';
|
|
|
|
eyedropperPreview.style.top = cursorLocation[1] + currentLayer.canvas.offsetTop - 30 + 'px';
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
var colorLightness = Math.max(selectedColor[0],selectedColor[1],selectedColor[2]);
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//for the darkest 50% of colors, change the eyedropper preview to dark mode
|
2020-04-04 10:41:56 +03:00
|
|
|
if (colorLightness>127) eyedropperPreview.classList.remove('dark');
|
|
|
|
else eyedropperPreview.classList.add('dark');
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'resizebrush' && dragging) {
|
2019-03-27 02:20:54 +03:00
|
|
|
//get new brush size based on x distance from original clicking location
|
2020-07-21 17:01:00 +03:00
|
|
|
var distanceFromClick = cursorLocation[0] - lastMouseClickPos[0];
|
2019-03-27 02:20:54 +03:00
|
|
|
//var roundingAmount = 20 - Math.round(distanceFromClick/10);
|
|
|
|
//this doesnt work in reverse... because... it's not basing it off of the brush size which it should be
|
|
|
|
var brushSizeChange = Math.round(distanceFromClick/10);
|
2020-04-15 03:01:31 +03:00
|
|
|
var newBrushSize = tool.pencil.previousBrushSize + brushSizeChange;
|
2019-03-27 02:20:54 +03:00
|
|
|
|
2019-04-02 20:42:28 +03:00
|
|
|
//set the brush to the new size as long as its bigger than 1
|
2020-04-15 03:01:31 +03:00
|
|
|
tool.pencil.brushSize = Math.max(1,newBrushSize);
|
2019-04-02 20:42:28 +03:00
|
|
|
|
|
|
|
//fix offset so the cursor stays centered
|
2020-07-21 17:01:00 +03:00
|
|
|
tool.pencil.moveBrushPreview(lastMouseClickPos);
|
2020-04-15 03:01:31 +03:00
|
|
|
currentTool.updateCursor();
|
2019-04-02 20:42:28 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'resizeeraser' && dragging) {
|
2019-04-02 20:42:28 +03:00
|
|
|
//get new brush size based on x distance from original clicking location
|
2020-07-21 17:01:00 +03:00
|
|
|
var distanceFromClick = cursorLocation[0] - lastMouseClickPos[0];
|
2019-04-02 20:42:28 +03:00
|
|
|
//var roundingAmount = 20 - Math.round(distanceFromClick/10);
|
|
|
|
//this doesnt work in reverse... because... it's not basing it off of the brush size which it should be
|
|
|
|
var eraserSizeChange = Math.round(distanceFromClick/10);
|
2020-04-15 03:01:31 +03:00
|
|
|
var newEraserSizeChange = tool.eraser.previousBrushSize + eraserSizeChange;
|
2019-04-02 20:42:28 +03:00
|
|
|
|
|
|
|
//set the brush to the new size as long as its bigger than 1
|
2020-07-21 17:01:00 +03:00
|
|
|
tool.eraser.brushSize = Math.max(1,newEraserSizeChange);
|
|
|
|
|
|
|
|
//fix offset so the cursor stays centered
|
|
|
|
tool.eraser.moveBrushPreview(lastMouseClickPos);
|
2020-04-15 03:01:31 +03:00
|
|
|
currentTool.updateCursor();
|
2019-04-02 20:42:28 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'resizerectangle' && dragging) {
|
2020-03-07 18:49:01 +03:00
|
|
|
//get new brush size based on x distance from original clicking location
|
2020-07-21 17:01:00 +03:00
|
|
|
var distanceFromClick = cursorLocation[0] - lastMouseClickPos[0];
|
2020-03-07 18:49:01 +03:00
|
|
|
//var roundingAmount = 20 - Math.round(distanceFromClick/10);
|
|
|
|
//this doesnt work in reverse... because... it's not basing it off of the brush size which it should be
|
|
|
|
var rectangleSizeChange = Math.round(distanceFromClick/10);
|
2020-04-15 03:01:31 +03:00
|
|
|
var newRectangleSize = tool.rectangle.previousBrushSize + rectangleSizeChange;
|
2020-03-07 18:49:01 +03:00
|
|
|
|
|
|
|
//set the brush to the new size as long as its bigger than 1
|
2020-04-15 03:01:31 +03:00
|
|
|
tool.rectangle.brushSize = Math.max(1,newRectangleSize);
|
2020-03-07 18:49:01 +03:00
|
|
|
|
2020-07-21 17:01:00 +03:00
|
|
|
//fix offset so the cursor stays centered
|
|
|
|
tool.rectangle.moveBrushPreview(lastMouseClickPos);
|
2020-04-15 03:01:31 +03:00
|
|
|
currentTool.updateCursor();
|
2020-03-07 18:49:01 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'rectselect') {
|
2020-04-09 17:48:19 +03:00
|
|
|
if (dragging && !isRectSelecting && mouseEvent.target.className == 'drawingCanvas') {
|
2020-03-04 16:36:40 +03:00
|
|
|
isRectSelecting = true;
|
2020-03-04 17:46:25 +03:00
|
|
|
startRectSelection(mouseEvent);
|
2020-03-04 16:36:40 +03:00
|
|
|
}
|
|
|
|
else if (dragging && isRectSelecting) {
|
2020-03-04 17:46:25 +03:00
|
|
|
updateRectSelection(mouseEvent);
|
2020-03-04 16:36:40 +03:00
|
|
|
}
|
2020-04-09 17:48:19 +03:00
|
|
|
else if (isRectSelecting) {
|
|
|
|
endRectSelection();
|
|
|
|
}
|
2020-03-04 16:36:40 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
else if (currentTool.name == 'moveselection') {
|
2020-03-05 18:13:23 +03:00
|
|
|
// Updating the cursor (move if inside rect, cross if not)
|
2020-04-15 03:01:31 +03:00
|
|
|
currentTool.updateCursor();
|
2020-03-05 18:13:23 +03:00
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
// If I'm dragging, I move the preview
|
|
|
|
if (dragging && cursorInSelectedArea()) {
|
2020-04-20 17:55:34 +03:00
|
|
|
updateMovePreview(getCursorPosition(mouseEvent));
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|
2020-03-05 15:34:29 +03:00
|
|
|
}
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
|
|
|
|
2020-07-21 17:01:00 +03:00
|
|
|
//mousewheel scroll
|
2019-03-27 02:20:54 +03:00
|
|
|
canvasView.addEventListener("wheel", function(mouseEvent){
|
2020-07-21 17:01:00 +03:00
|
|
|
let mode;
|
|
|
|
if (mouseEvent.deltaY < 0){
|
|
|
|
mode = 'in';
|
|
|
|
}
|
|
|
|
else if (mouseEvent.deltaY > 0) {
|
|
|
|
mode = 'out';
|
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-07-21 17:01:00 +03:00
|
|
|
// Changing zoom and position of the first layer
|
|
|
|
changeZoom(layers[0], mode, getCursorPosition(mouseEvent));
|
2019-03-31 19:41:08 +03:00
|
|
|
|
2020-07-21 17:01:00 +03:00
|
|
|
for (let i=1; i<layers.length; i++) {
|
|
|
|
// Copying first layer's data into the other layers
|
|
|
|
layers[i].copyData(layers[0]);
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
2020-06-20 00:31:36 +03:00
|
|
|
});
|