Code coverage report for code/utils/resize-image.coffee

Statements: 19.23% (5 / 26)      Branches: 12.5% (1 / 8)      Functions: 50% (2 / 4)      Lines: 19.23% (5 / 26)      Ignored: none     

All files » code/utils/ » resize-image.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 341   8 8   8                                                     8  
module.exports = (src, callback) ->
 
  fail = ->
    callback src
 
  Iif document?
    maxWidth = 100
    maxHeight = 100
 
    img = document.createElement 'img'
    img.setAttribute 'crossOrigin', 'anonymous'
    img.src = src
    img.onload = ->
      canvas = document.createElement 'canvas'
      {width, height} = img
      if width > height
        if width > maxWidth
          height *= maxWidth / width
          width = maxWidth
      else
        if height > maxHeight
          width *= maxHeight / height
          height = maxHeight
      canvas.width = width
      canvas.height = height
      canvas.getContext('2d').drawImage img, 0, 0, width, height
 
      callback canvas.toDataURL 'image/png'
 
    img.onerror = (e) ->
      fail()
  else
    fail()