This commit is contained in:
2019-01-02 19:16:16 +01:00
commit e76aafc35a
34 changed files with 13596 additions and 0 deletions

48
src/nodes/Resize.js Normal file
View File

@@ -0,0 +1,48 @@
import Node from './Node';
import {createCanvas, mediaSize, paintToCanvas} from './canvas';
export default class Resize extends Node {
constructor(options) {
super({
image: 'Image',
width: 'Number',
height: 'Number',
}, {
image: 'Image'
});
this.options = options;
}
get width() {
return this.__in.width.value || this.options.width;
}
get height() {
return this.__in.height.value || this.options.height;
}
__update() {
const media = this.__in.image.value;
const canvas = createCanvas(this.width, this.height);
const {width: srcWidth, height: srcHeight} = mediaSize(media);
const {width: destWidth, height: destHeight} = mediaSize(canvas);
const srcAspectRatio = (srcWidth / srcHeight);
const destAspectRatio = (destWidth / destHeight);
const newImage = {};
if (srcAspectRatio > destAspectRatio) {
newImage.width = destHeight * srcAspectRatio;
newImage.height = destHeight;
} else {
newImage.width = destWidth;
newImage.height = destWidth / srcAspectRatio;
}
newImage.top = (destHeight - newImage.height) / 2;
newImage.left = (destWidth - newImage.width) / 2;
paintToCanvas(canvas, media, newImage);
this.__out.image.value = canvas;
}
}