Skip to content

Instantly share code, notes, and snippets.

@jurikern
Created May 7, 2013 10:58
Show Gist options
  • Save jurikern/5531825 to your computer and use it in GitHub Desktop.
Save jurikern/5531825 to your computer and use it in GitHub Desktop.
Image Crop from Canvas
detectCanvas: =>
if @canvas
@canvas.replaceWith("<canvas class='picture_canvas' data-block='canvas'></canvas>")
@canvas = @container.find("*[data-block='canvas']")
@ctx = @canvas.get(0).getContext("2d")
handleImage: (e) =>
@detectCanvas()
reader = new FileReader()
reader.onload = (event) =>
img = new Image()
img.onload = =>
MAX_WIDTH = 320
MAX_HEIGHT = 320
if img.width > img.height
if img.width > MAX_WIDTH
img.height *= MAX_WIDTH / img.width
img.width = MAX_WIDTH
else
if img.height > MAX_HEIGHT
img.width *= MAX_HEIGHT / img.height
img.height = MAX_HEIGHT
@canvas.attr(width: img.width, height: img.height)
@ctx.drawImage(img, 0, 0, img.width, img.height)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment