Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have implemented a basic Isometric tile engine that can be explored by dragging the map with the mouse. Please see the fiddle below and drag away:

The code broken down is (CoffeeScript):

The draw function

draw = ->
  requestAnimFrame draw
  canvas.width = canvas.width

  for row in [0..width]
    for col in [0..height]
      xpos = (row - col) * tileHeight + width
      xpos += (canvas.width / 2) - (tileWidth / 2) + scrollPosition.x
      ypos = (row + col) * (tileHeight / 2) + height + scrollPosition.y
      context.drawImage(defaultTile, Math.round(xpos), Math.round(ypos), tileWidth, tileHeight)

Mouse drag-scrolling control

scrollPosition =
  x: 0
  y: 0

dragHelper = 
  active: false
  x: 0
  y: 0

window.addEventListener 'mousedown', (e) =>
, false

window.addEventListener 'mousemove', (e) =>
, false

window.addEventListener 'mouseup', (e) =>
, false

handleDrag = (e) =>
    x = e.clientX
    y = e.clientY
    scrollPosition.x -= Math.round((dragHelper.x - x) / 28)
    scrollPosition.y -= Math.round((dragHelper.y - y) / 28)

handleMouseUp = (e) =>
  e.preventDefault() = false

handleMouseDown = (e) =>
  x = e.clientX
  y = e.clientY = true
  dragHelper.x = x
  dragHelper.y = y

The Problem

As you can see from the fiddle the dragging action is ok but not perfect. How would I change the code to make the dragging action more smooth? What I would like is the point of the map you click on to stay under the mouse point whilst you drag; the same as they have done here:

share|improve this question
up vote 7 down vote accepted

To move smoother, change

scrollPosition.x -= Math.round((dragHelper.x - x) / 28)
scrollPosition.y -= Math.round((dragHelper.y - y) / 28)


scrollPosition.x = Math.round((x - dragHelper.x))
scrollPosition.y = Math.round((y - dragHelper.y))

To prevent jumping when first clicking on it, change

 dragHelper.x = x
 dragHelper.y = y


 dragHelper.x = x - scrollPosition.x  
 dragHelper.y = y - scrollPosition.y

And you should be fine.

Not sure why the / 28, did you want it snap to grid?

share|improve this answer
Very good, works perfectly. Thank you very much :) The / 28 was to reduce the speed of the grid movement. If only I had > 15 rep then I could up arrow you. Somebody up arrow user18474's answer :p – Jamie Fearon Aug 30 '12 at 20:31
Upped the Q. Now you can thank him yourself. – Bloodyaugust Aug 30 '12 at 23:08
Thanks Bloodyaugust. – Jamie Fearon Aug 31 '12 at 8:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.