 drawMessageText = (ctx, x, y, body) ->
   width = ctx.measureText(body).width
   ctx.fillStyle = "white"
-  ctx.fillRect(x - width / 2, y + 3, width, 10)
+  ctx.fillRect(x - width / 2, y , width, 12)
   ctx.fillStyle = "black"
   ctx.font = "12px Sans-Serif"
   ctx.textAlign = "center"
   ctx.textBaseline = "top"
   ctx.fillText(body, x, y)
 
-getMessageRect = (ctx, obj) ->
-  result = new Object()
-  pt = getIntersectionPoint(obj.from.order, obj.order)
-  result.x1 = pt.x
-  result.y1 = pt.y
-  pt = getIntersectionPoint(obj.to.order, obj.order)
-  result.x2 = pt.x
-  result.y2 = pt.y
-  result.centerX = getMiddlePos(result.x1, result.x2)
-  result.centerY = getMiddlePos(result.y1, result.y2)
-  return result
-
-getMiddlePos = (a, b) ->
-  return a + (b - a) / 2
-
-getIntersectionPoint = (instanceOrder, messageOrder) ->
-  result = new Object()
-  rect = getInstanceRect(instanceOrder)
-  result.x = rect.centerX
-  result.y = rect.y + rect.height + (messageOrder + 1) * MESSSAGE_OFFSET
-  return result
-
 # ------------------------------
 
 drawInstances = (ctx, instances, bottom) ->
-  drawInstance(ctx, obj, bottom) for obj in instances
-
-drawInstance = (ctx, obj, bottom) ->
-  rect = getInstanceRect(obj.order)
-  drawInstanceRect(ctx, rect)
-  drawInstanceText(ctx, rect, obj.name)
-  drawInstanceLine(ctx, rect, bottom)
-
-getInstanceRect = (order) ->
-  result = new Object()
-  result.x = order * (INSTANCE_WIDTH + INSTANCE_OFFSET) + CANVAS_PADDING
-  result.y = 0 + CANVAS_PADDING
-  result.width = INSTANCE_WIDTH
-  result.height = INSTANCE_HEIGHT
-  result.centerX = result.x + result.width / 2
-  result.centerY = result.y + result.height / 2
-  return result
+  for obj in instances
+    drawInstanceRect(ctx, obj.rect)
+    drawInstanceText(ctx, obj.rect, obj.name)
+    drawInstanceLine(ctx, obj.rect, bottom)
+  return
 
 drawInstanceRect = (ctx, rect) ->
   ctx.strokeRect(rect.x, rect.y, rect.width, rect.height)

drawObjects = (instances, messages) ->
  canvas = document.getElementById("diagram")
  return unless canvas
  canvas.height = calcCanvasHeight(messages)
  canvas.width = calcCanvasWidth(instances)
  ctx = canvas.getContext("2d")
  fillBackground(ctx, canvas.width, canvas.height)
  drawInstances(ctx, instances, canvas.height - MESSSAGE_OFFSET - CANVAS_PADDING)
  drawMessages(ctx, messages)

calcCanvasHeight = (messages) ->
  last = _.max(messages, (obj) -> obj.order)
  height = INSTANCE_HEIGHT + MESSSAGE_OFFSET * (last.order + 2)
  height += CANVAS_PADDING * 2
  return height

calcCanvasWidth = (instances) ->
  last = _.max(instances, (obj) -> obj.order)
  width = (last.order + 1) * (INSTANCE_WIDTH + INSTANCE_OFFSET)
  width -= INSTANCE_OFFSET
  width += CANVAS_PADDING * 2
  return width

fillBackground = (ctx, width, height) ->

  ctx.lineTo(rect.centerX, rect.centerY + bottom)
  ctx.closePath()
  ctx.stroke()

getInstanceRectByOrder = (instances, instanceOrder) ->
  obj = _.find(instances, (obj) -> obj.order is instanceOrder)
  return obj.rect

# ------------------------------

