Added canvas preview to layer menu

Decided to apply the same width / height ratio of the canvas to the preview.
This commit is contained in:
unsettledgames 2020-06-19 15:16:22 +02:00
parent 066582e309
commit c498a495d5
5 changed files with 45 additions and 19 deletions

View File

@ -130,9 +130,9 @@ body {
canvas { canvas {
display:inline-block; display:inline-block;
height:100%; height:50px;
width:50px; width:50px;
background-color:red; background-color:lightgrey;
left:4px; left:4px;
} }

View File

@ -13,6 +13,8 @@ function HistoryStateEditCanvas () {
this.canvas = currentCanvas; this.canvas = currentCanvas;
redoStates.push(this); redoStates.push(this);
currentLayer.updateLayerPreview();
}; };
this.redo = function () { this.redo = function () {
@ -21,6 +23,8 @@ function HistoryStateEditCanvas () {
this.canvas = currentCanvas; this.canvas = currentCanvas;
undoStates.push(this); undoStates.push(this);
currentLayer.updateLayerPreview();
}; };
//add self to undo array //add self to undo array

View File

@ -34,6 +34,10 @@
2 - Add a Replace feature so that people can replace a colour without editing the one in the palette 2 - Add a Replace feature so that people can replace a colour without editing the one in the palette
(right click->replace colour in layers? in that case we'd have to implement multiple layers selection) (right click->replace colour in layers? in that case we'd have to implement multiple layers selection)
KNOWN BUGS:
1 - Must delete existing layers when creating a new pixel
THINGS TO TEST: THINGS TO TEST:
1 - Undo / redo 1 - Undo / redo
@ -221,6 +225,32 @@ class Layer {
this.menuEntry.getElementsByClassName("default-icon")[1].style.display = "none"; this.menuEntry.getElementsByClassName("default-icon")[1].style.display = "none";
this.menuEntry.getElementsByClassName("edited-icon")[1].style.display = "inline-block"; this.menuEntry.getElementsByClassName("edited-icon")[1].style.display = "inline-block";
} }
updateLayerPreview() {
// Getting the canvas
let destination = this.menuEntry.getElementsByTagName("canvas")[0];
let widthRatio = this.canvasSize[0] / this.canvasSize[1];
let heightRatio = this.canvasSize[1] / this.canvasSize[0];
// Computing width and height for the preview image
let previewWidth = destination.width;
let previewHeight = destination.height;
// If the sprite is rectangular, I apply the ratio to the preview as well
if (widthRatio < 1) {
previewWidth = destination.width * widthRatio;
}
else if (widthRatio > 1) {
previewHeight = destination.height * heightRatio;
}
// La appiccico sulla preview
destination.getContext('2d').clearRect(0, 0, destination.width, destination.height);
destination.getContext('2d').drawImage(this.canvas,
// This is necessary to center the preview in the canvas
(destination.width - previewWidth) / 2, (destination.height - previewHeight) / 2,
previewWidth, previewHeight);
}
} }
// Finds a layer given its name // Finds a layer given its name

View File

@ -107,7 +107,7 @@ window.addEventListener("mouseup", function (mouseEvent) {
} }
} }
else if (currentTool.name == 'fill' && mouseEvent.target.className == 'drawingCanvas') { else if (currentTool.name == 'fill' && mouseEvent.target.className == 'drawingCanvas') {
console.log('filling') //console.log('filling')
//get cursor postion //get cursor postion
var cursorLocation = getCursorPosition(mouseEvent); var cursorLocation = getCursorPosition(mouseEvent);
@ -116,8 +116,9 @@ window.addEventListener("mouseup", function (mouseEvent) {
cursorLocation[0] += 2; cursorLocation[0] += 2;
cursorLocation[1] += 12; cursorLocation[1] += 12;
//fill starting at the location //fill starting at the location
fill(cursorLocation); fill(cursorLocation);
currentLayer.updateLayerPreview();
} }
else if (currentTool.name == 'zoom' && mouseEvent.target.className == 'drawingCanvas') { else if (currentTool.name == 'zoom' && mouseEvent.target.className == 'drawingCanvas') {
let mode; let mode;
@ -125,21 +126,6 @@ window.addEventListener("mouseup", function (mouseEvent) {
mode = "in"; mode = "in";
} }
} }
else if (currentTool == 'fill' && mouseEvent.target.className == 'drawingCanvas') {
//console.log('filling');
//if you clicked on anything but the canvas, do nothing
if (!mouseEvent.target == currentLayer.canvas) return;
//get cursor postion
var cursorLocation = getCursorPosition(mouseEvent);
//offset to match cursor point
cursorLocation[0] += 2;
cursorLocation[1] += 12;
//fill starting at the location
fill(cursorLocation);
}
else if (currentTool == 'zoom' && mouseEvent.target.className == 'drawingCanvas') { else if (currentTool == 'zoom' && mouseEvent.target.className == 'drawingCanvas') {
let mode; let mode;
if (mouseEvent.which == 1){ if (mouseEvent.which == 1){
@ -160,6 +146,7 @@ window.addEventListener("mouseup", function (mouseEvent) {
} }
else if (currentTool.name == 'rectangle') { else if (currentTool.name == 'rectangle') {
endRectDrawing(mouseEvent); endRectDrawing(mouseEvent);
currentLayer.updateLayerPreview();
} }
dragging = false; dragging = false;
@ -228,6 +215,8 @@ function draw (mouseEvent) {
//for the darkest 50% of colors, change the brush preview to dark mode //for the darkest 50% of colors, change the brush preview to dark mode
if (colorLightness>127) brushPreview.classList.remove('dark'); if (colorLightness>127) brushPreview.classList.remove('dark');
else brushPreview.classList.add('dark'); else brushPreview.classList.add('dark');
currentLayer.updateLayerPreview();
} }
// Decided to write a different implementation in case of differences between the brush and the eraser tool // Decided to write a different implementation in case of differences between the brush and the eraser tool
else if (currentTool.name == 'eraser') { else if (currentTool.name == 'eraser') {
@ -249,6 +238,8 @@ function draw (mouseEvent) {
lastPos = cursorLocation; lastPos = cursorLocation;
} }
} }
currentLayer.updateLayerPreview();
} }
else if (currentTool.name == 'rectangle') else if (currentTool.name == 'rectangle')
{ {

View File

@ -84,6 +84,7 @@ function endSelection() {
isPasting = false; isPasting = false;
isCutting = false; isCutting = false;
lastMovePos = undefined; lastMovePos = undefined;
currentLayer.updateLayerPreview();
new HistoryStateEditCanvas(); new HistoryStateEditCanvas();
} }