使用HTML5 canvas做地圖(3)圖片載入平移放大縮小

HPhone發表於2013-12-06

    終於開始可以寫程式碼了,手都開始癢了。這裡的程式碼僅僅是在chrome檢測過,我可以肯定的是IE10以下瀏覽器是行不通,我一直在考慮,是不是使用IE禁止看我的篇部落格,就是這群使用IE的人,給我加了很多工作量。

    一個地圖的基本動作,無非就是載入資料,平移,放大與縮小。這篇部落格主要是通過一張圖片的方式來模擬一下。

    我這裡認為大家都稍微瞭解甚至熟悉canvas的一些API,就不具體說,每一個引數代表什麼意思了。

<!DOCTYPE html>
<html>
	<head>
		<meta charset='utf-8'>
		<title>圖片載入平移放大縮小示例</title>
		<style>
			html,body{
				margin:0px;
				padding:0px;
			}
			canvas{
				border: 1px solid #000;
			}
		</style>
	</head>
	<body>
		<canvas id="canvas" width="800" height="800"></canvas>
		<script type="text/javascript" src="main.js"></script>
	</body>
</html>
var canvas,context;
function int(){
    canvas=document.getElementById('canvas');
    context=canvas.getContext('2d');
} 

圖片載入

 建立一個圖片物件之後,圖片不能馬上繪製到canvas上面,因為圖片還沒有載入完成。所以我們需要監聽圖片物件載入完事件,然後再去繪製。

var img,//圖片物件
    imgIsLoaded//圖片是否載入完成;
function loadImg(){
    img=new Image();
    img.onload=function(){
        imgIsLoaded=true;
        //draw image
    }
    img.src="map.jpg";
}

圖片繪製

    繪製影象一個函式就可以搞定,但是需要記錄這個影象的左上角座標以及縮放比例。

var imgX,imgY,imgScale;
function drawImage(){
    context.clearRect(0,0,canvas.width,canvas.height);
    context.drawImage(img,0,0,img.width,img.height,imgX,imgY,img.width*imgScale,img.height*imgScale);
}

圖片平移

 html5事件最小細度在DOM上,所以我們無法對canvas上的影象做監聽,只能對canvas監聽。

  1. 首先監聽滑鼠mousedown事件,等事件發生之後,再監聽滑鼠mousemove事件和mouseup事件
  2. mousemove事件發生之後,獲得滑鼠移動的位移,相應的圖片的位置改變多少
  3. mouseup事件發生之後,取消對mousemove以及mouseup事件監聽
canvas.onmousedown=function(event){
    var pos=windowToCanvas(canvas,event.clientX,event.clientY);
    canvas.onmousemove=function(event){
        canvas.style.cursor="move";
        var pos1=windowToCanvas(canvas,event.clientX,event.clientY);
        var x=pos1.x-pos.x;
        var y=pos1.y-pos.y;
        pos=pos1;
        imgX+=x;
        imgY+=y;
        drawImage();
    }
    canvas.onmouseup=function(){
        canvas.onmousemove=null;
        canvas.onmouseup=null;
        canvas.style.cursor="default";
    }
}

function windowToCanvas(canvas,x,y){
    var bbox = canvas.getBoundingClientRect();
    return {
        x:x - bbox.left - (bbox.width - canvas.width) / 2,
        y:y - bbox.top - (bbox.height - canvas.height) / 2
    };
}

圖片縮放

    其實縮放很簡單,稍微複雜的是,如何讓滑鼠成為放大或者縮小的中心。如果數學幾何不好,計算公式就可能看不明白了。

canvas.onmousewheel=canvas.onwheel=function(event){//chrome firefox瀏覽器相容
    var pos=windowToCanvas(canvas,event.clientX,event.clientY);
    event.wheelDelta=event.wheelDelta?event.wheelDelta:(event.deltaY*(-40));
    if(event.wheelDelta>0){
        imgScale*=2;
        imgX=imgX*2-pos.x;
        imgY=imgY*2-pos.y;
    }else{
        imgScale/=2;
        imgX=imgX*0.5+pos.x*0.5;
        imgY=imgY*0.5+pos.y*0.5;
    }
    drawImage();
}

  這個時候,基本功能就實現了,載入一張圖片和載入多張圖片都差不多,維護每一張圖片的位置和大小,下面來整理一下程式碼吧。

var canvas,context;
var img,//圖片物件
    imgIsLoaded,//圖片是否載入完成;
    imgX=0,
    imgY=0,
    imgScale=1;

(function int(){
    canvas=document.getElementById('canvas');
    context=canvas.getContext('2d');
    loadImg();
})();

function loadImg(){
    img=new Image();
    img.onload=function(){
        imgIsLoaded=true;
        drawImage();
    }
    img.src="map.jpg";
}

function drawImage(){
    context.clearRect(0,0,canvas.width,canvas.height);
    context.drawImage(img,0,0,img.width,img.height,imgX,imgY,img.width*imgScale,img.height*imgScale);
}

canvas.onmousedown=function(event){
    var pos=windowToCanvas(canvas,event.clientX,event.clientY);
    canvas.onmousemove=function(event){
        canvas.style.cursor="move";
        var pos1=windowToCanvas(canvas,event.clientX,event.clientY);
        var x=pos1.x-pos.x;
        var y=pos1.y-pos.y;
        pos=pos1;
        imgX+=x;
        imgY+=y;
        drawImage();
    }
    canvas.onmouseup=function(){
        canvas.onmousemove=null;
        canvas.onmouseup=null;
        canvas.style.cursor="default";
    }
}
canvas.onmousewheel=canvas.onwheel=function(event){
    var pos=windowToCanvas(canvas,event.clientX,event.clientY);
    event.wheelDelta=event.wheelDelta?event.wheelDelta:(event.deltaY*(-40));
    if(event.wheelDelta>0){
        imgScale*=2;
        imgX=imgX*2-pos.x;
        imgY=imgY*2-pos.y;
    }else{
        imgScale/=2;
        imgX=imgX*0.5+pos.x*0.5;
        imgY=imgY*0.5+pos.y*0.5;
    }
    drawImage();
}

function windowToCanvas(canvas,x,y){
    var bbox = canvas.getBoundingClientRect();
    return {
        x:x - bbox.left - (bbox.width - canvas.width) / 2,
        y:y - bbox.top - (bbox.height - canvas.height) / 2
    };
}

  如果不想複製這些程式碼的話,可以溫柔的點選一下這裡下載 

相關文章