empty space node

This commit is contained in:
2019-12-05 17:24:50 +01:00
parent a8d76d43c8
commit 08e1cb221f
3 changed files with 71 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "graphfx", "name": "graphfx",
"version": "0.0.13", "version": "0.0.15",
"description": "Graph image processing pipeline", "description": "Graph image processing pipeline",
"main": "src/index.js", "main": "src/index.js",
"scripts": { "scripts": {

View File

@@ -0,0 +1,69 @@
import Canvas2d from './Canvas2d';
import {createCanvas, mediaSize, paintToCanvas} from '../canvas';
export default class EmptySpace extends Canvas2d {
constructor() {
super('EmptySpace', {
}, {
w: {
type: 'Number',
},
h: {
type: 'Number',
},
top: {
type: 'Number',
},
right: {
type: 'Number',
},
bottom: {
type: 'Number',
},
left: {
type: 'Number',
}
});
this._update();
}
async render({image}, canvas, ctx) {
if (!image) {
return;
}
let left = Infinity;
let right = -Infinity;
let top = Infinity;
let bottom = -Infinity;
const {width, height} = mediaSize(image);
canvas.width = width;
canvas.height = height;
ctx.drawImage(image, 0, 0);
const imageData = ctx.getImageData(0, 0, width, height)
for (let offset=0; offset<imageData.data.length; offset+=4) {
const x = (offset/4) % width;
const y = Math.floor((offset/4) / width)
const [r,g,b,a] = imageData.data.slice(offset, offset+4);
if (a < 1) {
top = Math.min(top, y);
right = Math.max(right, x);
bottom = Math.max(bottom, y);
left = Math.min(left, x);
}
imageData.data[offset] = a;
imageData.data[offset + 1] = a;
imageData.data[offset + 2] = a;
imageData.data[offset + 3] = a;
}
ctx.putImageData(imageData, 0, 0);
this.out.top.value = top;
this.out.right.value = right;
this.out.bottom.value = bottom;
this.out.left.value = left;
this.out.w.value = right - left;
this.out.h.value = bottom - top;
return canvas;
}
}

View File

@@ -4,6 +4,7 @@ export {default as Resize} from './Canvas2d/Resize';
export {default as Compose} from './Canvas2d/Compose'; export {default as Compose} from './Canvas2d/Compose';
export {default as Fill} from './Canvas2d/Fill'; export {default as Fill} from './Canvas2d/Fill';
export {default as Flip} from './Canvas2d/Flip'; export {default as Flip} from './Canvas2d/Flip';
export {default as EmptySpace} from './Canvas2d/EmptySpace';
// export {default as ImageIdentity} from './Canvas2d/ImageIdentity'; // export {default as ImageIdentity} from './Canvas2d/ImageIdentity';
export {default as Text} from './Canvas2d/Text'; export {default as Text} from './Canvas2d/Text';
// WebGL // WebGL