Finished basic canvas resizing

Must test pivots for negative values
This commit is contained in:
unsettledgames 2020-09-15 11:56:05 +02:00
parent 592283321a
commit 5e2a3e46e1
3 changed files with 195 additions and 50 deletions

View File

@ -959,19 +959,20 @@ svg {
flex-wrap:wrap; flex-wrap:wrap;
vertical-align:middle; vertical-align:middle;
text-align:center; text-align:center;
width:150px; width:130px;
float:left;
button { button {
margin-right:10px; margin-right:10px;
margin-bottom:10px; margin-bottom:10px;
position:relative; position:relative;
width:40px; width:32px;
height:40px; height:32px;
background-color:color(base, background); background:$basehover;
border:none; border:none;
path { path {
fill:color(menu, foreground); fill:$basehovericon;
} }
transition: background 150ms ease-in-out, transition: background 150ms ease-in-out,
transform 150ms ease; transform 150ms ease;
@ -980,11 +981,12 @@ svg {
} }
button:hover, button:hover,
button:focus { button:focus,
button.rc-selected-pivot {
cursor:pointer; cursor:pointer;
background-color: color(base, foreground); background-color: $baseicon;
path { path {
fill:color(base, foreground, bold); fill:$basehovericonhover;
} }
border: 2px solid color(base, foreground); border: 2px solid color(base, foreground);
} }
@ -993,13 +995,11 @@ svg {
} }
} }
#borders-menu { #borders-menu, #rc-size-menu {
display:flex; display:flex;
position:relative; position:relative;
flex-wrap: wrap; flex-wrap: wrap;
width:250px; width:250px;
float:right;
vertical-align:middle;
font-size:15px; font-size:15px;
left:10px; left:10px;
text-align:center; text-align:center;
@ -1007,8 +1007,9 @@ svg {
input { input {
position:relative; position:relative;
margin-left:10px; margin-left:10px;
height:10px; height:15px;
width:20px; width:40px;
padding:8px;
} }
input::-webkit-outer-spin-button, input::-webkit-outer-spin-button,
@ -1021,18 +1022,36 @@ svg {
-moz-appearance: textfield; -moz-appearance: textfield;
} }
h2 {
position:relative;
display:inline-block;
}
span { span {
padding-right:10px; padding-right:10px;
float:left; float:left;
position:relative; position:relative;
display: flex;
vertical-align:middle; vertical-align:middle;
height:40px; height:40px;
width:100px;
display: inline-flex;
align-items: center;
}
}
#rc-size-menu {
span {
width:115px;
}
}
#borders-menu {
width:400px;
justify-content: center;
div {
float: none;
width: 330px;
padding-left:50px;
span {
padding-right:20px;
}
} }
} }

View File

@ -1,5 +1,6 @@
let resizeCanvasContainer = document.getElementById("resize-canvas"); let resizeCanvasContainer = document.getElementById("resize-canvas");
let pivot = "middle"; let pivot = "middle";
let currentPivotObject;
let resizeCanvasInitialized = false; let resizeCanvasInitialized = false;
let borders = {left: 0, right: 0, top: 0, bottom: 0}; let borders = {left: 0, right: 0, top: 0, bottom: 0};
@ -16,14 +17,61 @@ function initResizeCanvasInputs() {
for (let i=0; i<buttons.length; i++) { for (let i=0; i<buttons.length; i++) {
buttons[i].addEventListener("click", changePivot); buttons[i].addEventListener("click", changePivot);
if (buttons[i].getAttribute("value").includes("middle")) {
currentPivotObject = buttons[i];
}
} }
document.getElementById("rc-width").value = layers[0].canvasSize[0];
document.getElementById("rc-height").value = layers[0].canvasSize[1];
document.getElementById("rc-border-left").addEventListener("change", changedBorder);
document.getElementById("rc-border-right").addEventListener("change", changedBorder);
document.getElementById("rc-border-top").addEventListener("change", changedBorder);
document.getElementById("rc-border-bottom").addEventListener("change", changedBorder);
document.getElementById("rc-width").addEventListener("change", changedSize);
document.getElementById("rc-height").addEventListener("change", changedSize);
document.getElementById("resize-canvas-confirm").addEventListener("click", resizeCanvas); document.getElementById("resize-canvas-confirm").addEventListener("click", resizeCanvas);
console.log("Pivot selezionato: " + currentPivotObject);
}
function changedBorder(event) {
rcUpdateBorders();
document.getElementById("rc-width").value = parseInt(layers[0].canvasSize[0]) + borders.left + borders.right;
document.getElementById("rc-height").value = parseInt(layers[0].canvasSize[1]) + borders.top + borders.bottom;
}
function changedSize(event) {
let widthOffset = document.getElementById("rc-width").value - layers[0].canvasSize[0];
let heightOffset = document.getElementById("rc-height").value - layers[0].canvasSize[1];
let left = Math.round(widthOffset / 2);
let right = widthOffset - left;
let top = Math.round(heightOffset / 2);
let bottom = heightOffset - top;
document.getElementById("rc-border-left").value = left;
document.getElementById("rc-border-right").value = right;
document.getElementById("rc-border-top").value = top;
document.getElementById("rc-border-bottom").value = bottom;
borders.left = left;
borders.right = right;
borders.top = top;
borders.bottom = bottom;
} }
function resizeCanvas(event) { function resizeCanvas(event) {
let imageDatas = []; let imageDatas = [];
let leftOffset = 0;
let topOffset = 0;
let copiedDataIndex = 0;
rcUpdateBorders(); rcUpdateBorders();
changedSize();
// Save all imageDatas // Save all imageDatas
for (let i=0; i<layers.length; i++) { for (let i=0; i<layers.length; i++) {
@ -34,21 +82,79 @@ function resizeCanvas(event) {
// Resize the canvases // Resize the canvases
for (let i=0; i<layers.length; i++) { for (let i=0; i<layers.length; i++) {
layers[i].canvasSize[0] += borders.left + borders.right; layers[i].canvasSize[0] = parseInt(layers[i].canvasSize[0]) + borders.left + borders.right;
layers[i].canvasSize[1] += borders.top + borders.bottom; layers[i].canvasSize[1] = parseInt(layers[i].canvasSize[1]) + borders.top + borders.bottom;
layers[i].canvas.width = layers[i].canvasSize[0]; layers[i].canvas.width = layers[i].canvasSize[0];
layers[i].canvas.height = layers[i].canvasSize[1]; layers[i].canvas.height = layers[i].canvasSize[1];
layers[i].resize();
layers[i].context.fillStyle = currentGlobalColor;
} }
// Clear the canvases
// Regenerate the checkerboard
fillCheckerboard();
// Put the imageDatas in the right position // Put the imageDatas in the right position
// Must crop the imageDatas if the canvas has being reduced switch (pivot)
{
case 'topleft':
leftOffset = 0;
topOffset = 0;
break;
case 'top':
leftOffset = (borders.left + borders.right) / 2;
topOffset = 0;
break;
case 'topright':
leftOffset = borders.left + borders.right;
topOffset = 0;
break;
case 'left':
leftOffset = 0;
topOffset = (borders.top + borders.bottom) / 2;
break;
case 'middle':
leftOffset = (borders.left + borders.right) / 2;
topOffset = (borders.top + borders.bottom) / 2;
break;
case 'right':
leftOffset = borders.left + borders.right;
topOffset = (borders.top + borders.bottom) / 2;
break;
case 'bottomleft':
leftOffset = 0;
topOffset = borders.top + borders.bottom;
break;
case 'bottom':
leftOffset = (borders.left + borders.right) / 2;
topOffset = borders.top + borders.bottom;
break;
case 'bottomright':
leftOffset = borders.left + borders.right;
topOffset = borders.top + borders.bottom;
break;
default:
console.log('now thats a very weird pivot');
break;
}
/*
leftOffset = Math.abs(leftOffset);
topOffset = Math.abs(topOffset);
*/
for (let i=0; i<layers.length; i++) {
if (layers[i].menuEntry != null) {
layers[i].context.putImageData(imageDatas[copiedDataIndex], leftOffset, topOffset);
copiedDataIndex++;
}
}
closeDialogue();
} }
function rcUpdateBorders() { function rcUpdateBorders() {
console.log(document.getElementById("rc-border-left").value);
// Getting input // Getting input
borders.left = document.getElementById("rc-border-left").value; borders.left = document.getElementById("rc-border-left").value;
borders.right = document.getElementById("rc-border-right").value; borders.right = document.getElementById("rc-border-right").value;
@ -60,11 +166,14 @@ function rcUpdateBorders() {
borders.right == "" ? borders.right = 0 : borders.right = Math.round(parseInt(borders.right)); borders.right == "" ? borders.right = 0 : borders.right = Math.round(parseInt(borders.right));
borders.top == "" ? borders.top = 0 : borders.top = Math.round(parseInt(borders.top)); borders.top == "" ? borders.top = 0 : borders.top = Math.round(parseInt(borders.top));
borders.bottom == "" ? borders.bottom = 0 : borders.bottom = Math.round(parseInt(borders.bottom)); borders.bottom == "" ? borders.bottom = 0 : borders.bottom = Math.round(parseInt(borders.bottom));
console.log(borders);
} }
function changePivot(event) { function changePivot(event) {
pivot = event.target.getAttribute("value"); pivot = event.target.getAttribute("value");
// Setting the selected class
currentPivotObject.classList.remove("rc-selected-pivot");
currentPivotObject = event.target;
currentPivotObject.classList.add("rc-selected-pivot");
} }

View File

@ -260,14 +260,31 @@
<button class="pivot-button" value="top">{{svg "arrows/top.svg" width="20" height="20"}}</button> <button class="pivot-button" value="top">{{svg "arrows/top.svg" width="20" height="20"}}</button>
<button class="pivot-button" value="topright">{{svg "arrows/topright.svg" width="20" height="20"}}</button> <button class="pivot-button" value="topright">{{svg "arrows/topright.svg" width="20" height="20"}}</button>
<button class="pivot-button" value="left">{{svg "arrows/left.svg" width="20" height="20"}}</button> <button class="pivot-button" value="left">{{svg "arrows/left.svg" width="20" height="20"}}</button>
<button class="pivot-button" value="middle">{{svg "arrows/middle.svg" width="20" height="20"}}</button> <button class="pivot-button rc-selected-pivot" value="middle">{{svg "arrows/middle.svg" width="20" height="20"}}</button>
<button class="pivot-button" value="right">{{svg "arrows/right.svg" width="20" height="20"}}</button> <button class="pivot-button" value="right">{{svg "arrows/right.svg" width="20" height="20"}}</button>
<button class="pivot-button" value="bottomleft">{{svg "arrows/bottomleft.svg" width="20" height="20"}}</button> <button class="pivot-button" value="bottomleft">{{svg "arrows/bottomleft.svg" width="20" height="20"}}</button>
<button class="pivot-button" value="bottom">{{svg "arrows/bottom.svg" width="20" height="20"}}</button> <button class="pivot-button" value="bottom">{{svg "arrows/bottom.svg" width="20" height="20"}}</button>
<button class="pivot-button" value="bottomright">{{svg "arrows/bottomright.svg" width="20" height="20"}}</button> <button class="pivot-button" value="bottomright">{{svg "arrows/bottomright.svg" width="20" height="20"}}</button>
</span> </span>
<!-- SIZE-->
<span id = "rc-size-menu">
<h2>Size</h2>
<div>
<span>
Width: <input id="rc-width" type="number" default="0" step="1"
value="{{#if border}}{{border}}{{else}}0{{/if}}" autocomplete="off"/>
</span>
<span>
Height: <input id="rc-height" default="0" step="1" type="number"
value="{{#if border}}{{border}}{{else}}0{{/if}}" autocomplete="off"/>
</span>
</div>
</span>
<!--BORDERS--> <!--BORDERS-->
<span id = "borders-menu"> <span id = "borders-menu">
<h2>Borders offsets</h2>
<div>
<span> <span>
Left: <input id="rc-border-left" type="number" default="0" step="1" Left: <input id="rc-border-left" type="number" default="0" step="1"
value="{{#if border}}{{border}}{{else}}0{{/if}}" autocomplete="off"/> value="{{#if border}}{{border}}{{else}}0{{/if}}" autocomplete="off"/>
@ -287,7 +304,7 @@
Bottom: <input id="rc-border-bottom" default="0" step="1" type="number" Bottom: <input id="rc-border-bottom" default="0" step="1" type="number"
value="{{#if border}}{{border}}{{else}}0{{/if}}" autocomplete="off"/> value="{{#if border}}{{border}}{{else}}0{{/if}}" autocomplete="off"/>
</span> </span>
</div>
<button id = "resize-canvas-confirm">Resize canvas</button> <button id = "resize-canvas-confirm">Resize canvas</button>
</span> </span>
</div> </div>