Skip to content

Instantly share code, notes, and snippets.

@forresto
Created March 9, 2014 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forresto/9456940 to your computer and use it in GitHub Desktop.
Save forresto/9456940 to your computer and use it in GitHub Desktop.
noflo = require 'noflo'
class Measure extends noflo.AsyncComponent
description: 'Load image from URL and get dimensions'
icon: 'picture-o'
constructor: ->
@inPorts =
url: new noflo.Port 'string'
@outPorts =
dimensions: new noflo.Port 'array'
error: new noflo.Port 'object'
super 'url', 'dimensions'
doAsync: (url, callback) ->
image = new Image()
image.onload = () ->
if (image.naturalWidth? and image.naturalWidth is 0) or image.width is 0
image.onerror()
return
dimensions = [image.width, image.height]
@outPorts.dimensions.beginGroup url
@outPorts.dimensions.send dimensions
@outPorts.dimensions.endGroup()
@outPorts.dimensions.disconnect()
callback null
image.onerror = (err) ->
return callback err
image.src = url
exports.getComponent = -> new Measure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment