From a9d380ec1d5d37bc4795493c7f3afacb9be33a71 Mon Sep 17 00:00:00 2001 From: npalomba Date: Sun, 31 Mar 2019 16:32:49 +0200 Subject: [PATCH] Implemented eraser tool. Must move and resize layers at the same time. Signed-off-by: npalomba --- README.md | 6 +++--- css/pixel-editor.scss | 4 ++-- js/_canvas.js | 1 + js/_checkerboard.js | 2 ++ js/_clickedColor.js | 2 +- js/_createColorPalette.js | 6 +++--- js/_deleteColor.js | 2 +- js/_drawLine.js | 4 ++-- js/_history.js | 30 +++++++++++++------------- js/_jscolor.js | 18 ++++++++-------- js/_loadImage.js | 4 ++-- js/_mouseEvents.js | 44 +++++++++++++++++++-------------------- js/_newPixel.js | 15 +++++++------ js/_replaceAllOfColor.js | 4 ++-- js/_updateCursor.js | 2 +- js/_variables.js | 9 ++++++-- views/pixel-editor.hbs | 1 + 17 files changed, 83 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index d243e2e..680b966 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,9 @@ Suggestions / Planned features: - Selections - New selection tool - - New canvas layer above the drawing layer + - New currentLayer.canvas layer above the drawing layer - Move when click and drag - - Merge with canvas when click outside + - Merge with currentLayer.canvas when click outside - Copy/paste - Add as selection @@ -35,7 +35,7 @@ Suggestions / Planned features: - Palette option remove unused colors - Pixel Grid - - Another canvas + - Another currentLayer.canvas - Must be rescaled each zoom - Possibly add collaborate function using together.js diff --git a/css/pixel-editor.scss b/css/pixel-editor.scss index 10a8c76..a8cce52 100644 --- a/css/pixel-editor.scss +++ b/css/pixel-editor.scss @@ -54,11 +54,11 @@ canvas { } #checkerboard { - z-index:1000; + z-index:1; } #pixel-canvas { - z-index:1; + z-index:2; background:transparent; } diff --git a/js/_canvas.js b/js/_canvas.js index cd39dd4..b9cb556 100644 --- a/js/_canvas.js +++ b/js/_canvas.js @@ -1,6 +1,7 @@ function Canvas(width, height, canvas) { this.canvasSize = [width, height], this.canvas = canvas, + this.context = canvas.getContext("2d"), this.initialize = function() { var maxHorizontalZoom = Math.floor(window.innerWidth/this.canvasSize[0]*0.75); var maxVerticalZoom = Math.floor(window.innerHeight/this.canvasSize[1]*0.75); diff --git a/js/_checkerboard.js b/js/_checkerboard.js index 97435cd..2deaef9 100644 --- a/js/_checkerboard.js +++ b/js/_checkerboard.js @@ -6,6 +6,8 @@ var nSquaresFilled = 0; */ function fillCheckerboard() { + var context = checkerBoard.context; + for (var i=0; i -canvasSize[0]*zoom*0.75 + 64) + currentLayer.canvas.offsetLeft + (cursorLocation[0] - lastPos[0]) > -canvasSize[0]*zoom*0.75 + 64) canvas.style.left = canvas.offsetLeft + (cursorLocation[0] - lastPos[0]) +'px'; if ( //bottom canvas.offsetTop + (cursorLocation[1] - lastPos[1]) < window.innerHeight-canvasSize[1]*zoom*0.25 && //top - canvas.offsetTop + (cursorLocation[1] - lastPos[1]) > -canvasSize[0]*zoom*0.75 + 48) - canvas.style.top = canvas.offsetTop + (cursorLocation[1] - lastPos[1]) +'px'; + currentLayer.canvas.offsetTop + (cursorLocation[1] - lastPos[1]) > -canvasSize[0]*zoom*0.75 + 48) + currentLayer.canvas.style.top = currentLayer.canvas.offsetTop + (cursorLocation[1] - lastPos[1]) +'px'; */ } - else if (currentTool == 'eyedropper' && dragging && mouseEvent.target == canvas) { + else if (currentTool == 'eyedropper' && dragging && mouseEvent.target == currentLayer.canvas) { var selectedColor = context.getImageData(Math.floor(cursorLocation[0]/zoom),Math.floor(cursorLocation[1]/zoom),1,1).data; eyedropperPreview.style.borderColor = '#'+rgbToHex(selectedColor[0],selectedColor[1],selectedColor[2]); eyedropperPreview.style.display = 'block'; - eyedropperPreview.style.left = cursorLocation[0] + canvas.offsetLeft - 30 + 'px'; - eyedropperPreview.style.top = cursorLocation[1] + canvas.offsetTop - 30 + 'px'; + eyedropperPreview.style.left = cursorLocation[0] + currentLayer.canvas.offsetLeft - 30 + 'px'; + eyedropperPreview.style.top = cursorLocation[1] + currentLayer.canvas.offsetTop - 30 + 'px'; var colorLightness = Math.max(selectedColor[0],selectedColor[1],selectedColor[2]); @@ -213,8 +213,8 @@ function draw (mouseEvent) { brushSize = Math.max(1,newBrushSize); //fix offset so the cursor stays centered - brushPreview.style.left = lastPos[0] + canvas.offsetLeft - brushSize * zoom / 2 + 'px'; - brushPreview.style.top = lastPos[1] + canvas.offsetTop - brushSize * zoom / 2 + 'px'; + brushPreview.style.left = lastPos[0] + currentLayer.canvas.offsetLeft - brushSize * zoom / 2 + 'px'; + brushPreview.style.top = lastPos[1] + currentLayer.canvas.offsetTop - brushSize * zoom / 2 + 'px'; updateCursor(); } diff --git a/js/_newPixel.js b/js/_newPixel.js index 4177b37..0fc6f5e 100644 --- a/js/_newPixel.js +++ b/js/_newPixel.js @@ -1,9 +1,12 @@ function newPixel (width, height, palette) { - var main = new Canvas(width, height, canvas); - main.initialize(); + currentLayer = new Canvas(width, height, canvas); + currentLayer.initialize(); - canvasSize = main.canvasSize; + checkerBoard = new Canvas(width, height, checkerBoard); + checkerBoard.initialize(); + + canvasSize = currentLayer.canvasSize; //remove current palette colors = document.getElementsByClassName('color-button'); while (colors.length > 0) { @@ -40,14 +43,14 @@ function newPixel (width, height, palette) { //fill background of canvas with bg color fillCheckerboard(); /* - context.fillStyle = '#'+defaultBackgroundColor; - context.fillRect(0, 0, canvasSize[0], canvasSize[1]); + currentLayer.context.fillStyle = '#'+defaultBackgroundColor; + currentLayer.context.fillRect(0, 0, canvasSize[0], canvasSize[1]); console.log('#'+defaultBackgroundColor) */ //set current drawing color as foreground color - context.fillStyle = '#'+defaultForegroundColor; + currentLayer.context.fillStyle = '#'+defaultForegroundColor; selectedPalette = 'none'; } diff --git a/js/_replaceAllOfColor.js b/js/_replaceAllOfColor.js index d7c9f3a..b6c7b6d 100644 --- a/js/_replaceAllOfColor.js +++ b/js/_replaceAllOfColor.js @@ -7,7 +7,7 @@ function replaceAllOfColor (oldColor, newColor) { if (typeof newColor === 'string') newColor = hexToRgb(newColor); //create temporary image from canvas to search through - var tempImage = context.getImageData(0, 0, canvasSize[0], canvasSize[1]); + var tempImage = currentLayer.context.getImageData(0, 0, canvasSize[0], canvasSize[1]); //loop through all pixels for (var i=0;i +