From 81cc4c890006d9866a8370147771f92c8a26ef7f Mon Sep 17 00:00:00 2001 From: unsettledgames <47360416+unsettledgames@users.noreply.github.com> Date: Tue, 21 Jul 2020 22:30:46 +0200 Subject: [PATCH] Moved setCanvasOffset to layer --- js/_addColor.js | 8 -------- js/_changeZoom.js | 4 ++-- js/_getCursorPosition.js | 22 +--------------------- js/_layer.js | 25 +++++++++++++++++++++++++ js/_setCanvasOffset.js | 23 ----------------------- 5 files changed, 28 insertions(+), 54 deletions(-) delete mode 100644 js/_setCanvasOffset.js diff --git a/js/_addColor.js b/js/_addColor.js index d8e5b62..ac95af0 100644 --- a/js/_addColor.js +++ b/js/_addColor.js @@ -4,7 +4,6 @@ let currentPalette = []; //input hex color string //returns list item element function addColor (newColor) { - //add # at beginning if not present if (newColor.charAt(0) != '#') newColor = '#' + newColor; @@ -19,13 +18,6 @@ function addColor (newColor) { button.addEventListener('mouseup', clickedColor); listItem.appendChild(button); - /* - //create input to hold color value - var colorValue = document.createElement("input"); - colorValue.classList.add("color-value"); - listItem.appendChild(colorValue); - */ - //insert new listItem element at the end of the colors menu (right before add button) colorsMenu.insertBefore(listItem, colorsMenu.children[colorsMenu.children.length-1]); diff --git a/js/_changeZoom.js b/js/_changeZoom.js index d05a5b1..4dc9eff 100644 --- a/js/_changeZoom.js +++ b/js/_changeZoom.js @@ -11,7 +11,7 @@ function changeZoom (layer, direction, cursorLocation) { newHeight = canvasSize[1] * zoom; //adjust canvas position - setCanvasOffset(layer.canvas, layer.canvas.offsetLeft + (oldWidth - newWidth) *cursorLocation[0]/oldWidth, layer.canvas.offsetTop + (oldHeight - newHeight) *cursorLocation[1]/oldWidth) + layer.setCanvasOffset(layer.canvas.offsetLeft + (oldWidth - newWidth) *cursorLocation[0]/oldWidth, layer.canvas.offsetTop + (oldHeight - newHeight) *cursorLocation[1]/oldWidth); } //if you want to zoom in else if (direction == 'in' && zoom + Math.ceil(zoom/10) < window.innerHeight/4){ @@ -20,7 +20,7 @@ function changeZoom (layer, direction, cursorLocation) { newHeight = canvasSize[1] * zoom; //adjust canvas position - setCanvasOffset(layer.canvas, layer.canvas.offsetLeft - Math.round((newWidth - oldWidth)*cursorLocation[0]/oldWidth), layer.canvas.offsetTop - Math.round((newHeight - oldHeight)*cursorLocation[1]/oldHeight)) + layer.setCanvasOffset(layer.canvas.offsetLeft - Math.round((newWidth - oldWidth)*cursorLocation[0]/oldWidth), layer.canvas.offsetTop - Math.round((newHeight - oldHeight)*cursorLocation[1]/oldHeight)); } //resize canvas diff --git a/js/_getCursorPosition.js b/js/_getCursorPosition.js index da23fd6..3c28662 100644 --- a/js/_getCursorPosition.js +++ b/js/_getCursorPosition.js @@ -16,24 +16,4 @@ function getCursorPosition(e) { y -= canvas.offsetTop; return [x,y]; -} - -//get cursor position relative to canvas -function getCursorPositionRelative(e, layer) { - var x; - var y; - - if (e.pageX != undefined && e.pageY != undefined) { - x = e.pageX; - y = e.pageY; - } - else { - x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; - y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; - } - - x -= layer.canvas.offsetLeft; - y -= layer.canvas.offsetTop; - - return [x,y]; -} +} \ No newline at end of file diff --git a/js/_layer.js b/js/_layer.js index a01c15e..1ca409b 100644 --- a/js/_layer.js +++ b/js/_layer.js @@ -160,6 +160,31 @@ class Layer { this.canvas.style.width = newWidth; this.canvas.style.height = newHeight; } + + setCanvasOffset (offsetLeft, offsetTop) { + //horizontal offset + var minXOffset = -this.canvasSize[0] * zoom + 164; + var maxXOffset = window.innerWidth - 148; + + if (offsetLeft < minXOffset) + this.canvas.style.left = minXOffset +'px'; + else if (offsetLeft > maxXOffset) + this.canvas.style.left = maxXOffset +'px'; + else + this.canvas.style.left = offsetLeft +'px'; + + //vertical offset + var minYOffset = -this.canvasSize[1] * zoom + 164; + var maxYOffset = window.innerHeight - 100; + + if (offsetTop < minYOffset) + this.canvas.style.top = minYOffset +'px'; + else if (offsetTop > maxYOffset) + this.canvas.style.top = maxYOffset +'px'; + else + this.canvas.style.top = offsetTop +'px'; + } + // Copies the otherCanvas' position and size copyData(otherCanvas) { this.canvas.style.width = otherCanvas.canvas.style.width; diff --git a/js/_setCanvasOffset.js b/js/_setCanvasOffset.js deleted file mode 100644 index 695551b..0000000 --- a/js/_setCanvasOffset.js +++ /dev/null @@ -1,23 +0,0 @@ -function setCanvasOffset (canvas, offsetLeft, offsetTop) { - //horizontal offset - var minXOffset = -canvasSize[0]*zoom+ 164; - var maxXOffset = window.innerWidth - 148; - - if (offsetLeft < minXOffset) - canvas.style.left = minXOffset +'px'; - else if (offsetLeft > maxXOffset) - canvas.style.left = maxXOffset +'px'; - else - canvas.style.left = offsetLeft +'px'; - - //vertical offset - var minYOffset = -canvasSize[1]*zoom + 164; - var maxYOffset = window.innerHeight-100; - - if (offsetTop < minYOffset) - canvas.style.top = minYOffset +'px'; - else if (offsetTop > maxYOffset) - canvas.style.top = maxYOffset +'px'; - else - canvas.style.top = offsetTop +'px'; -}