 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)

  else
    drawNormalMessageLine(ctx, obj)

checkPresenceOfToFromObjects = (obj) ->
  return false unless obj.from?
  return false unless obj.to?
  return true

isSelfMessage = (obj) -> return obj.to is obj.from

isNote = (obj) -> return obj.is_note

drawSelfMessageLine = (ctx, obj) ->
  rect = getSelfMessageRect(ctx, obj)
  headPt = getSelfMessageEndPoint(rect)
  bodyPt = getSelfMessageBodyPoint(rect)
  drawSelfMessagePath(ctx, rect)
  drawMessageArrowhead(ctx, headPt, true)
  drawMessageText(ctx, bodyPt.x, bodyPt.y, obj.body)

getSelfMessageEndPoint = (rect) ->
  pt = new Object()
  pt.x = rect.x1
  pt.y = rect.y2
  return pt

getSelfMessageBodyPoint = (rect) ->
  pt = new Object()
  pt.x = rect.x1
  pt.y = rect.y1 + MESSSAGE_BODY_OFFSET
  return pt

getSelfMessageRect = (ctx, obj) ->
  result = new Object()
  pt = getIntersectionPoint(obj.from.order, obj.order)
  result.x1 = pt.x
  result.y1 = pt.y
  result.x2 = pt.x + MESSSAGE_SELF_WIDTH
  result.y2 = pt.y + MESSSAGE_SELF_HEIGHT
  return result

drawSelfMessagePath = (ctx, rect) ->
  ctx.beginPath()

MESSSAGE_OFFSET = 40
MESSSAGE_BODY_OFFSET = -18
MESSSAGE_SELF_WIDTH = 28
MESSSAGE_SELF_HEIGHT = 17
MESSSAGE_ARROW_WIDTH = 5
MESSSAGE_ARROW_HEIGHT = -8

INSTANCE_OFFSET = 40
INSTANCE_PADDING = 10
INSTANCE_WIDTH = 70
INSTANCE_HEIGHT = 30

