diff --git a/src/nodes/Canvas2d/Flip.js b/src/nodes/Canvas2d/Flip.js new file mode 100644 index 0000000..c5ef97e --- /dev/null +++ b/src/nodes/Canvas2d/Flip.js @@ -0,0 +1,33 @@ +import Canvas2d from './Canvas2d'; +import {createCanvas, mediaSize, paintToCanvas} from '../canvas'; + +export default class Flip extends Canvas2d { + + constructor() { + super('Flip', { + horizontal: { + type: 'Boolean', + default: false, + }, + vertical: { + type: 'Boolean', + default: false, + }, + }, {}); + this._update(); + } + + async render({image, horizontal, vertical}, canvas, ctx) { + if (!image) return; + const horizontalModifier = horizontal ? -1 : 1; + const verticalModifier = vertical ? -1 : 1; + const {width, height} = mediaSize(image); + canvas.width = width; + canvas.height = height; + ctx.save(); + ctx.scale(horizontalModifier, verticalModifier); + ctx.drawImage(image, 0, 0, width * horizontalModifier, height * verticalModifier); + ctx.restore(); + return canvas; + } +} \ No newline at end of file diff --git a/src/nodes/index.js b/src/nodes/index.js index a92d6a7..f6791b4 100644 --- a/src/nodes/index.js +++ b/src/nodes/index.js @@ -3,6 +3,7 @@ export {default as Webcam} from './Webcam'; export {default as Resize} from './Canvas2d/Resize'; export {default as Compose} from './Canvas2d/Compose'; export {default as Fill} from './Canvas2d/Fill'; +export {default as Flip} from './Canvas2d/Flip'; // export {default as ImageIdentity} from './Canvas2d/ImageIdentity'; export {default as Text} from './Canvas2d/Text'; // WebGL