2019-09-27 23:45:52 +00:00
|
|
|
import { IAnimationData } from './Interfaces/IAnimationData';
|
|
|
|
import { ICanvasData } from './Interfaces/ICanvasData';
|
|
|
|
|
2019-09-27 00:45:28 +00:00
|
|
|
// I display the canvas and am clickable
|
2019-09-26 01:59:16 +00:00
|
|
|
export class CanvasHandler {
|
2019-09-27 23:45:52 +00:00
|
|
|
private canvasImage: HTMLCanvasElement;
|
|
|
|
private imageElement: HTMLImageElement;
|
|
|
|
private animationData: IAnimationData;
|
|
|
|
private canvasData: ICanvasData;
|
|
|
|
private orginInfo: HTMLElement;
|
2019-09-26 01:59:16 +00:00
|
|
|
|
2019-09-28 01:35:27 +00:00
|
|
|
private targetImageSize: number = 256;
|
|
|
|
|
2019-09-27 23:45:52 +00:00
|
|
|
constructor(
|
|
|
|
animationData: IAnimationData,
|
|
|
|
canvasData: ICanvasData,
|
|
|
|
canvasImage: HTMLCanvasElement,
|
|
|
|
imageElement: HTMLImageElement,
|
|
|
|
originInfo: HTMLElement
|
|
|
|
) {
|
|
|
|
this.animationData = animationData;
|
|
|
|
this.canvasData = canvasData;
|
|
|
|
this.canvasImage = canvasImage;
|
|
|
|
this.imageElement = imageElement;
|
|
|
|
this.orginInfo = originInfo;
|
2019-09-26 22:36:27 +00:00
|
|
|
|
2019-09-28 01:35:27 +00:00
|
|
|
this.ResizeCanvas();
|
2019-09-27 23:45:52 +00:00
|
|
|
this.UpdateCanvasDataSize();
|
|
|
|
const canvasContext: CanvasRenderingContext2D = this.canvasImage.getContext('2d')!;
|
2019-09-28 01:35:27 +00:00
|
|
|
canvasContext.fillRect(0, 0, this.targetImageSize, this.targetImageSize);
|
|
|
|
canvasContext.imageSmoothingEnabled = false;
|
|
|
|
|
|
|
|
this.canvasImage.addEventListener('click', this.mouseDown);
|
|
|
|
}
|
2019-09-26 22:05:37 +00:00
|
|
|
|
2019-09-28 01:35:27 +00:00
|
|
|
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();
|
2019-09-26 22:05:37 +00:00
|
|
|
}
|
2019-09-27 23:45:52 +00:00
|
|
|
|
|
|
|
private UpdateCanvasDataSize() {
|
|
|
|
this.canvasData.width = this.canvasImage.width;
|
|
|
|
this.canvasData.height = this.canvasImage.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
private mouseDown = (event: MouseEvent) => {
|
|
|
|
// get position
|
|
|
|
const ratioWidth: number = this.canvasImage.width / this.imageElement.width;
|
|
|
|
const ratioHeight: number = this.canvasImage.height / this.imageElement.height;
|
|
|
|
// get origin in pixels
|
|
|
|
const pixelX: number = Math.floor(event.offsetX / ratioWidth);
|
|
|
|
const pixelY: number = Math.floor(event.offsetY / ratioHeight);
|
|
|
|
console.log('CLICK X:' + pixelX + ' Y:' + pixelY);
|
|
|
|
// update animation data
|
|
|
|
this.animationData.originX = pixelX;
|
|
|
|
this.animationData.originY = pixelY;
|
|
|
|
// update canvas data
|
|
|
|
this.canvasData.widthRatio = ratioWidth;
|
|
|
|
this.canvasData.heightRatio = ratioHeight;
|
|
|
|
// update origin number display
|
|
|
|
this.orginInfo.innerText = 'Origin X: ' + this.animationData.originX + ' Y: ' + this.animationData.originY;
|
|
|
|
};
|
2019-09-26 01:59:16 +00:00
|
|
|
}
|