aspect ratio resizing

master
Beau Blyth 2019-09-27 18:35:27 -07:00
parent 10dd54aed5
commit 2f9cbdc335
5 changed files with 24 additions and 30 deletions

View File

@ -3,36 +3,45 @@ import { ICanvasData } from './Interfaces/ICanvasData';
// I display the canvas and am clickable // I display the canvas and am clickable
export class CanvasHandler { export class CanvasHandler {
private currentImageDiv: HTMLElement;
private canvasImage: HTMLCanvasElement; private canvasImage: HTMLCanvasElement;
private imageElement: HTMLImageElement; private imageElement: HTMLImageElement;
private animationData: IAnimationData; private animationData: IAnimationData;
private canvasData: ICanvasData; private canvasData: ICanvasData;
private orginInfo: HTMLElement; private orginInfo: HTMLElement;
private targetImageSize: number = 256;
constructor( constructor(
animationData: IAnimationData, animationData: IAnimationData,
canvasData: ICanvasData, canvasData: ICanvasData,
canvasImage: HTMLCanvasElement, canvasImage: HTMLCanvasElement,
currentImageDiv: HTMLElement,
imageElement: HTMLImageElement, imageElement: HTMLImageElement,
originInfo: HTMLElement originInfo: HTMLElement
) { ) {
this.animationData = animationData; this.animationData = animationData;
this.canvasData = canvasData; this.canvasData = canvasData;
this.canvasImage = canvasImage; this.canvasImage = canvasImage;
this.currentImageDiv = currentImageDiv;
this.imageElement = imageElement; this.imageElement = imageElement;
this.orginInfo = originInfo; this.orginInfo = originInfo;
//setup canvas this.ResizeCanvas();
this.canvasImage.width = 256;
this.canvasImage.height = 256;
this.UpdateCanvasDataSize(); this.UpdateCanvasDataSize();
const canvasContext: CanvasRenderingContext2D = this.canvasImage.getContext('2d')!; const canvasContext: CanvasRenderingContext2D = this.canvasImage.getContext('2d')!;
canvasContext.fillRect(0, 0, 256, 256); canvasContext.fillRect(0, 0, this.targetImageSize, this.targetImageSize);
canvasContext.imageSmoothingEnabled = false;
this.currentImageDiv.addEventListener('click', this.mouseDown); this.canvasImage.addEventListener('click', this.mouseDown);
}
public ResizeCanvas() {
// get image ratio, then scale default width by it
const hwratio = this.imageElement.height / this.imageElement.width;
const newWidth = this.targetImageSize / hwratio;
const newHeight = this.targetImageSize;
console.log('hwratio = ' + hwratio);
this.canvasImage.width = newWidth;
this.canvasImage.height = newHeight;
this.UpdateCanvasDataSize();
} }
private UpdateCanvasDataSize() { private UpdateCanvasDataSize() {

View File

@ -33,7 +33,6 @@ export class FrameHandler {
this.frameNumberDiv = frameNumberDiv; this.frameNumberDiv = frameNumberDiv;
window.requestAnimationFrame(this.windowAnimationUpdate); window.requestAnimationFrame(this.windowAnimationUpdate);
this.imageElement = imageElement; this.imageElement = imageElement;
this.canvasContext.imageSmoothingEnabled = false;
} }
public GetCurrentFrame(): number { public GetCurrentFrame(): number {
@ -74,6 +73,7 @@ export class FrameHandler {
this.frameNumberDiv.innerText = 'No images uploaded yet'; this.frameNumberDiv.innerText = 'No images uploaded yet';
} else { } else {
this.canvasContext.clearRect(0, 0, this.htmlCanvasElement.width, this.htmlCanvasElement.height); this.canvasContext.clearRect(0, 0, this.htmlCanvasElement.width, this.htmlCanvasElement.height);
this.canvasContext.imageSmoothingEnabled = false;
this.imageElement.src = this.filenames[this.currentFrame]; this.imageElement.src = this.filenames[this.currentFrame];
// draw sprite // draw sprite
this.canvasContext.drawImage( this.canvasContext.drawImage(
@ -93,7 +93,6 @@ export class FrameHandler {
this.canvasContext.moveTo(originX - originCursorSize, originY); this.canvasContext.moveTo(originX - originCursorSize, originY);
this.canvasContext.lineTo(originX + originCursorSize, originY); this.canvasContext.lineTo(originX + originCursorSize, originY);
this.canvasContext.stroke(); this.canvasContext.stroke();
this.frameNumberDiv.className = 'instruction'; this.frameNumberDiv.className = 'instruction';
this.frameNumberDiv.innerText = this.frameNumberDiv.innerText =
'Frame ' + (this.currentFrame + 1).toString() + ' / ' + this.filenames.length.toString(); 'Frame ' + (this.currentFrame + 1).toString() + ' / ' + this.filenames.length.toString();

View File

@ -58,7 +58,6 @@ export class Page {
this.animationData, this.animationData,
this.canvasData, this.canvasData,
canvasElement, canvasElement,
document.getElementById('currentImage') as HTMLElement,
imageElement, imageElement,
document.getElementById('originInfo') as HTMLElement document.getElementById('originInfo') as HTMLElement
); );
@ -172,7 +171,9 @@ export class Page {
this.frameHandler.GoToFrame(0); this.frameHandler.GoToFrame(0);
this.frameHandler.StopPlayingAnimation(); this.frameHandler.StopPlayingAnimation();
this.frameHandler.TogglePlayingAnimation(); this.frameHandler.TogglePlayingAnimation();
console.log(this.animationData);
this.canvasHandler.ResizeCanvas();
//set framedata initialized to true //set framedata initialized to true
}; };

View File

@ -38,21 +38,7 @@ body {
height: 100%; height: 100%;
} }
#imageCanvas { #canvasImage {
border: 2px solid #6b7578; border: 2px solid #6b7578;
width: 256;
height: 256;
-ms-interpolation-mode: nearest-neighbor;
image-rendering: pixelated; image-rendering: pixelated;
} }
#currentImage {
border: 2px solid #6b7578;
object-position: center top;
width: 256;
height: 256;
min-width: 256;
min-height: 256;
-ms-interpolation-mode: nearest-neighbor;
image-rendering: crisp-edges;
}

5
dist/index.html vendored
View File

@ -18,10 +18,9 @@
<p></p> <p></p>
</div> </div>
<!-- canvas --> <!-- canvas -->
<div id="currentImage"> <div id="canvasStyle">
<canvas id="canvasImage" alt="No images uploaded"></canvas> <canvas id="canvasImage" alt=""></canvas>
</div> </div>
<div id="settings"> <div id="settings">
Name: <input type = "text" id="filename"><br> Name: <input type = "text" id="filename"><br>
<div id = "originInfo">Click image to set Origin</div> <div id = "originInfo">Click image to set Origin</div>