 
     @isDragging = false
     if @dragOffset
+      cos = Math.cos @dragAngle
+      sin = Math.sin @dragAngle
+
+      # Rotate the grabOffset to calculate translation correction after
+      # changing the transform origin
+      correction =
+        x: @grabOffset.x - (@grabOffset.x * cos - @grabOffset.y * sin)
+        y: @grabOffset.y - (@grabOffset.x * sin + @grabOffset.y * cos)
+
+      @$box.css "transform-origin", ""
+      @$box.css "transform", """
+        translate(#{@dragOffset.x + correction.x}px, #{@dragOffset.y + correction.y}px)
+        rotate(#{@dragAngle}rad)
+      """
+
+      # Rotate back to the closest 90 degree multiple
+      deg =  @dragAngle / Math.PI * 180
+      if deg %% 90 > 45
+        deg += 90 - deg %% 90
+      else
+        deg -= deg %% 90
+
       bounce = new Bounce()
-      bounce.translate
+      bounce.translate(
         stiffness: 1.5
-        from: @dragOffset
+        from:
+          x: @dragOffset.x + correction.x
+          y: @dragOffset.y + correction.y
         to: { x: 0, y: 0 }
+      ).rotate(
+        stiffness: 0.5
+        from: @dragAngle / Math.PI * 180
+        to: deg
+      )
 
       @playAnimation bounceObject: bounce, duration: 600
       _.defer => @$box.css "transform", ""
+      setTimeout (=> @$result.removeClass "animate"), 610
 
-    $(document).off "mousemove.boxdrag"
+    $(document).off ".boxdrag"
     $("body").removeClass "dragging"
 
   boxDrag: (e) =>

      x: e.clientX
      y: e.clientY

    @dragOffset = null

    $(document).on

      x: e.clientX - @dragStartPos.x
      y: e.clientY - @dragStartPos.y

    distance = Math.sqrt(delta.x ** 2 + delta.y ** 2)
    dampenedDistance = 250 * (1 - Math.E ** (-0.002 * distance))

    angle = Math.atan2 delta.y, delta.x

    if @grabDistance > 10
      centerDelta =
        x: e.clientX - @boxStartPos.x
        y: e.clientY - @boxStartPos.y

      centerDistance = Math.sqrt centerDelta.x ** 2 + centerDelta.y ** 2
      dotProduct = centerDelta.x * @grabOffset.x + centerDelta.y * @grabOffset.y
      determinant = centerDelta.x * @grabOffset.y - centerDelta.y * @grabOffset.x

      @dragAngle = -Math.atan2 determinant, dotProduct
    else
      @dragAngle = 0

    @dragOffset =
      x: Math.cos(angle) * dampenedDistance
      y: Math.sin(angle) * dampenedDistance

    @$box.css "transform", """
      translate(#{@dragOffset.x}px, #{@dragOffset.y}px)
      rotate(#{@dragAngle}rad)
    """

module.exports = App