Kieren
Kieren

Reputation: 13

Javascript Canvas panning the canvas

I am trying to pan the canvas when the user presses the mouse and moves it but it doesn't seem to be working for some reason I cannot see. Any ideas?

    canvas.addEventListener('mousedown', onMouseDown, false); 

    function onMouseDown(event){
        var mousePos = new Vector(event.clientX, event.clientY);
        mousedown = true;
    }

    canvas.addEventListener('mouseup', onMouseUp, false); 
    function onMouseUp(event){
        mousedown = false;
    }

    canvas.addEventListener('mousemove', onMouseMove, false); 
    function onMouseMove(event){
        mousePos = new Vector(event.clientX, event.clientY);
        if(onMouseDown){
            var difference = mousePos.subtract(previousMousePosition);
            pan.add(difference);
        }
        previousMousePosition = mousePos;
    }

    pan = new Vector(0, 0);

I am also getting an error saying that Vector is not defined and also one for mousePos.subtract. This is my vector.js:

var Vector = (function () {
function Vector(pX, pY) {
    this.setX(pX);
    this.setY(pY);

};
Vector.prototype.getX = function() {

    return this.mX;
};
Vector.prototype.setX = function (pX) {
    this.mX = pX;
};
Vector.prototype.getY = function() {

    return this.mY;
};
Vector.prototype.setY = function(pY) {
    this.mY = pY;
};

return Vector;
})();

Upvotes: 0

Views: 1006

Answers (1)

markE
markE

Reputation: 105015

You can pan the canvas content using the translate transformation.

context.translate(x,y) will move the canvas origin horizontally by x pixels and vertically by y pixels.

So to "pan right" by 5 pixels you can context.translate(-5,0).

The nice thing about using transformations is that you don't have to alter your existing drawing code -- translate will automatically "shift" all your drawings in the specified directions.

[ Addition: show how to get net-panning from users mouse drags ]

function log(){console.log.apply(console,arguments);}

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
function reOffset(){
  var BB=canvas.getBoundingClientRect();
  offsetX=BB.left;
  offsetY=BB.top;        
}
var offsetX,offsetY;
reOffset();
window.onscroll=function(e){ reOffset(); }
window.onresize=function(e){ reOffset(); }

var isDown=false;
var startX,startY;
var netPanningX=0;
var netPanningY=0;

var $results=$('#results');

for(var x=0;x<100;x++){ ctx.fillText(x,x*20+netPanningX,ch/2); }
for(var y=-50;y<50;y++){ ctx.fillText(y,cw/2,y*20+netPanningY); }

$("#canvas").mousedown(function(e){handleMouseDown(e);});
$("#canvas").mousemove(function(e){handleMouseMove(e);});
$("#canvas").mouseup(function(e){handleMouseUp(e);});
$("#canvas").mouseout(function(e){handleMouseOut(e);});

function handleMouseDown(e){
  // tell the browser we're handling this event
  e.preventDefault();
  e.stopPropagation();

  startX=parseInt(e.clientX-offsetX);
  startY=parseInt(e.clientY-offsetY);

  // Put your mousedown stuff here
  isDown=true;
}

function handleMouseUp(e){
  // tell the browser we're handling this event
  e.preventDefault();
  e.stopPropagation();

  mouseX=parseInt(e.clientX-offsetX);
  mouseY=parseInt(e.clientY-offsetY);

  // Put your mouseup stuff here
  isDown=false;
}

function handleMouseOut(e){
  // tell the browser we're handling this event
  e.preventDefault();
  e.stopPropagation();

  mouseX=parseInt(e.clientX-offsetX);
  mouseY=parseInt(e.clientY-offsetY);

  // Put your mouseOut stuff here
  isDown=false;
}

function handleMouseMove(e){
  if(!isDown){return;}
  // tell the browser we're handling this event
  e.preventDefault();
  e.stopPropagation();

  mouseX=parseInt(e.clientX-offsetX);
  mouseY=parseInt(e.clientY-offsetY);

  // dx & dy are the distance the mouse has moved since
  // the last mousemove event
  var dx=mouseX-startX;
  var dy=mouseY-startY;
  startX=mouseX;
  startY=mouseY;

  // accumulate the net panning done
  netPanningX+=dx;
  netPanningY+=dy;
  $results.text('Net change in panning: x:'+netPanningX+', y:'+netPanningY); 

  ctx.clearRect(0,0,cw,ch);
  for(var x=-50;x<50;x++){ ctx.fillText(x,x*20+netPanningX,ch/2); }
  for(var y=-50;y<50;y++){ ctx.fillText(y,cw/2,y*20+netPanningY); }

}
body{ background-color: ivory; }
#canvas{border:1px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h4 id=results>Drag the mouse to see net panning in x,y directions</h4>
<canvas id="canvas" width=300 height=300></canvas>

Upvotes: 1

Related Questions