Created
April 18, 2014 15:25
-
-
Save forresto/11049790 to your computer and use it in GitHub Desktop.
noflo-ccv to cropping bounds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var noflo = require('noflo'); | |
exports.getComponent = function () { | |
var c = new noflo.Component(); | |
c.icon = "circle-o" | |
c.inPorts.add('faces'); | |
c.outPorts.add('circles'); | |
c.outPorts.add('dimensions'); | |
c.inPorts.faces.on('data', function(data){ | |
var dims = { | |
minX: Infinity, | |
minY: Infinity, | |
maxX: -Infinity, | |
maxY: -Infinity, | |
width: 0, | |
height: 0, | |
ratio: 1 | |
}; | |
var circles = data.map(function(face){ | |
if (dims.minX > face.x) { | |
dims.minX = face.x | |
} | |
if (dims.minY > face.y) { | |
dims.minY = face.y | |
} | |
if (dims.maxX < face.x+face.width) { | |
dims.maxX = face.x+face.width | |
} | |
if (dims.maxY < face.y+face.height) { | |
dims.maxY = face.y+face.height | |
} | |
var circle = [ | |
face.x + face.width/2, | |
face.y + face.height/2, | |
face.height, | |
0, | |
Math.PI*2 | |
]; | |
circle.type = 'arc'; | |
return circle; | |
}); | |
if (c.outPorts.circles.isAttached()) { | |
c.outPorts.circles.send(circles); | |
} | |
if (isFinite(dims.minX) && isFinite(dims.minY) && isFinite(dims.maxX) && isFinite(dims.maxY)) { | |
dims.minX = Math.round(dims.minX); | |
dims.minY = Math.round(dims.minY); | |
dims.maxX = Math.round(dims.maxX); | |
dims.maxY = Math.round(dims.maxY); | |
dims.width = dims.maxX - dims.minX; | |
dims.height = dims.maxY - dims.minY; | |
dims.ratio = dims.width/dims.height; | |
if (c.outPorts.dimensions.isAttached()) { | |
c.outPorts.dimensions.send(dims); | |
} | |
} else { | |
// No faces | |
} | |
}); | |
return c; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment