Skip to content

Instantly share code, notes, and snippets.

@gnufied
Created August 5, 2011 05:09
Show Gist options
  • Save gnufied/1126948 to your computer and use it in GitHub Desktop.
Save gnufied/1126948 to your computer and use it in GitHub Desktop.
class @ShowTime.YAImageEditor
constructor: (@imageJson,@containerElement) ->
_.bindAll(this,'drawEnded','drawStarted',"saveImage","mouseMoved")
_.bindAll(this,'move')
@canvasContainer = @containerElement.find('.image_lightbox')
@toolbarContainer = @containerElement.find('.paint_toolbar')
@canvasWidth = 810
@canvasHeight = 490
@paper = Raphael(@canvasContainer[0],@canvasWidth, @canvasHeight)
@canvas = $(@paper.canvas)
@loadImage()
@svg_objects = []
@drawingBox = null
@tempObjects = []
$(".image_actions button").click(@saveImage)
@draw_flag = false
loadImage: ->
imageUrl = @imageJson.medium_path
image = new Image()
image.src = imageUrl
image.onload = =>
width = image.width
height = image.height
[xpos,ypos] = @imageLocation(width,height)
@image = @paper.image(imageUrl,xpos,ypos,width,height)
@image.attr({cursor: 'crosshair'})
@image.mousemove(@mouseMoved)
@image.drag(@move,@drawStarted,@drawEnded)
move: (dx,dy) ->
if @drawingBox
## this does not remove the drawingBox
@drawingBox.remove()
@rectPath()
@drawingBox = @paper.path(@positionArray)
@setStrokeColor()
imageLocation: (width, height) ->
left = @canvasWidth - width
top = @canvasHeight - height
[left/2,top/2]
rectPath: ->
@positionArray.push(["L",@startPoint.x,@startPoint.y])
@positionArray.push(["L",@startPoint.x,@currentPos.y])
@positionArray.push(["L",@currentPos.x,@currentPos.y])
@positionArray.push(["L",@currentPos.x,@startPoint.y])
getPoint: (event) ->
new ShowTime.Point(event.offsetX,event.offsetY)
setStrokeColor: ->
@drawingBox.attr({stroke: "#fbcc0a", 'stroke-width': '2px'});
mouseMoved: (event) ->
@currentPos = @getPoint(event)
saveImage: (event) ->
console.info("saving image ... ")
drawStarted: (event) ->
@positionArray = []
@startPoint = @currentPos
@positionArray.push(["M",@currentPos.x,@currentPos.y])
drawEnded: (event) ->
console.info("Setting draw_flag to false")
@drawingBox = null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment