AnimationTool/app/canvas_handler.ts

85 lines
2.6 KiB
TypeScript
Raw Normal View History

2019-09-27 23:45:52 +00:00
import { IAnimationData } from './Interfaces/IAnimationData';
import { IFramePinData } from './Interfaces/IFramePinData';
2019-10-07 17:51:43 +00:00
import { IProjectData } from './Interfaces/IProjectData';
2019-09-27 23:45:52 +00:00
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 projectData: IProjectData;
2019-09-27 23:45:52 +00:00
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: IProjectData,
2019-09-27 23:45:52 +00:00
canvasImage: HTMLCanvasElement,
imageElement: HTMLImageElement,
2019-10-05 02:31:21 +00:00
originInfo: HTMLElement
2019-09-27 23:45:52 +00:00
) {
this.animationData = animationData;
this.projectData = canvasData;
2019-09-27 23:45:52 +00:00
this.canvasImage = canvasImage;
this.imageElement = imageElement;
this.orginInfo = originInfo;
2019-09-26 22:36:27 +00:00
2019-10-08 22:41:37 +00:00
this.ResizeCanvas(256, 256);
2019-09-27 23:45:52 +00:00
this.UpdateCanvasDataSize();
const canvasContext: CanvasRenderingContext2D = this.canvasImage.getContext('2d')!;
2019-10-08 22:41:37 +00:00
canvasContext.clearRect(0, 0, this.targetImageSize, this.targetImageSize);
2019-09-28 01:35:27 +00:00
canvasContext.imageSmoothingEnabled = false;
}
2019-09-26 22:05:37 +00:00
2019-10-08 03:44:23 +00:00
public ResizeCanvas(width: number, height: number) {
2019-09-28 01:35:27 +00:00
// get image ratio, then scale default width by it
2019-10-08 03:44:23 +00:00
const hwratio = height / width;
let newWidth = this.targetImageSize / hwratio;
let newHeight = this.targetImageSize;
if (newWidth > 600) {
newWidth = 600;
newHeight = 600 * hwratio;
}
2019-09-28 01:35:27 +00:00
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
2019-10-07 17:51:43 +00:00
public CanvasMouseDown = (offsetX: number, offsetY: number) => {
2019-09-27 23:45:52 +00:00
// get position
const ratioWidth: number = this.canvasImage.width / this.imageElement.width;
const ratioHeight: number = this.canvasImage.height / this.imageElement.height;
// get origin in pixels
2019-10-07 17:51:43 +00:00
const pixelX: number = Math.floor(offsetX / ratioWidth);
const pixelY: number = Math.floor(offsetY / ratioHeight);
// console.log('CLICK X:' + pixelX + ' Y:' + pixelY);
if (this.projectData.currentlySelectedPin === 0) {
// update animation data
this.animationData.originX = pixelX;
this.animationData.originY = pixelY;
} else {
2019-10-07 17:51:43 +00:00
// console.log('current pin id = ' + this.projectData.currentlySelectedPin);
2019-10-09 01:50:03 +00:00
const newPinData = {
x: pixelX,
y: pixelY
};
2019-10-09 01:50:03 +00:00
this.animationData.frames[this.projectData.currentFrame].pinData[
this.projectData.currentlySelectedPin
] = newPinData;
}
2019-09-27 23:45:52 +00:00
// update canvas data
this.projectData.widthRatio = ratioWidth;
this.projectData.heightRatio = ratioHeight;
2019-09-27 23:45:52 +00:00
};
2019-10-07 17:51:43 +00:00
private UpdateCanvasDataSize() {
this.projectData.width = this.canvasImage.width;
this.projectData.height = this.canvasImage.height;
}
2019-09-26 01:59:16 +00:00
}