js/index.js
/*
  calculate bounding path of an image
*/
calculate bounding path of an image
calculate bounding path of an image
function calcPath(url, callback) {
  var AMASK = 0x000000ff 
  var AMIN = 5 
  var canvas = document.getElementById('meas')
  if (!canvas) {
    canvas = document.createElement("canvas")
    canvas.width = 10
    canvas.height = 10
    canvas.style = "display:none;"
    document.body.appendChild(canvas)
  }
  var img = new Image(),
      w = canvas.width,
      h = canvas.height,
      ctx = canvas.getContext('2d');
  img.crossOrigin = ''
  img.onload = getBounds
  img.src = url
  function getBounds() {
    canvas.width = w = this.width
    canvas.height = h = this.height
    ctx.clearRect(0, 0, w, h)
    try{
      ctx.drawImage(this, 0, 0, w, h)
    }catch(e){
      console.warn("image could not be drawn for measurement");
      return
    }
    var idata = ctx.getImageData(0, 0, w, h),
        buffer = idata.data,
        buffer32 = new Uint32Array(buffer.buffer),
        x,
        y,
        pts = [];
    function hit(x,y){
      return idata.data[4*(x+y*w)+3] > 0xf0;
    }
    for (x = 0; x < w; x++) {
      for (y = 0; y < h; y++) {
        if (hit(x,y)) {
            pts.push({x:x,y:y-1});
          break;
        }
      }
    }
    for (x = w; x >= 0; x--) {
      for (y = h; y >= 0; y--) {
        if (hit(x,y)) {
            pts.push({x:x,y:y+1})
          break
        }
      }
    }
    pts.push(pts[0])
    pts = simplify(pts,2)
    pts.shift()
    return callback(null,pts)
  }
}
function calcPath ( url , callback ) { var AMASK = 0x000000ff var AMIN = 5 var canvas = document . getElementById ( 'meas' ) if ( ! canvas ) { canvas = document . createElement ( "canvas" ) canvas . width = 10 canvas . height = 10 canvas . style = "display:none;" document . body . appendChild ( canvas ) } var img = new Image ( ) , w = canvas . width , h = canvas . height , ctx = canvas . getContext ( '2d' ) ; img . crossOrigin = '' img . onload = getBounds img . src = url function getBounds ( ) { canvas . width = w = this . width canvas . height = h = this . height ctx . clearRect ( 0 , 0 , w , h ) try { ctx . drawImage ( this , 0 , 0 , w , h ) } catch ( e ) { console . warn ( "image could not be drawn for measurement" ) ; return } var idata = ctx . getImageData ( 0 , 0 , w , h ) , buffer = idata . data , buffer32 = new Uint32Array ( buffer . buffer ) , x , y , pts = [ ] ; function hit ( x , y ) { return idata . data [ 4 * ( x + y * w ) + 3 ] > 0xf0 ; } for ( x = 0 ; x < w ; x ++ ) { for ( y = 0 ; y < h ; y ++ ) { if ( hit ( x , y ) ) { pts . push ( { x : x , y : y - 1 } ) ; break ; } } } for ( x = w ; x >= 0 ; x -- ) { for ( y = h ; y >= 0 ; y -- ) { if ( hit ( x , y ) ) { pts . push ( { x : x , y : y + 1 } ) break } } } pts . push ( pts [ 0 ] ) pts = simplify ( pts , 2 ) pts . shift ( ) return callback ( null , pts ) } }
calculate bounding path of an image
calculate bounding path of an image
// alpha mask
 // alpha minimum to count as part of the image
 // this part is synchronous, so although it uses just 1 canvas, single-threading guarantees it will be ok
 // have to clear in case canvas is being re-used
 //return buffer32[x+y*w] > 0;
 // get top, ltr
 // get bottom, rtl
 // re-add the first as the last point, for better path optimization
 // pop the first off since it's the same
