縮放變換scale(sx,sy)傳入兩個(gè)參數(shù),分別是水平方向和垂直方向上對(duì)象的縮放倍數(shù)。例如context.scale(2,2)就是對(duì)圖像放大兩倍。其實(shí),看上去簡單,實(shí)際用起來還是有一些問題的。我們來看一段代碼:
JavaScript Code復(fù)制內(nèi)容到剪貼板
- <!DOCTYPE html>
- <html lang="zh">
- <head>
- <meta charset="UTF-8">
- <title>縮放變換</title>
- <style>
- body { background: url("./images/bg3.jpg") repeat; }
- #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; }
- </style>
- </head>
- <body>
- <div id="canvas-warp">
- <canvas id="canvas">
- 你的瀏覽器居然不支持Canvas?!趕快換一個(gè)吧??!
- </canvas>
- </div>
-
- <script>
- window.onload = function(){
- var canvas = document.getElementById("canvas");
- canvas.width = 800;
- canvas.height = 600;
- var context = canvas.getContext("2d");
- context.fillStyle = "#FFF";
- context.fillRect(0,0,800,600);
-
- context.strokeStyle = "red";
- context.lineWidth = 5;
- for(var i = 1; i < 4; i++){
- context.save();
- context.scale(i,i);
- context.strokeRect(50,50,150,100);
- context.restore();
- }
- };
- </script>
- </body>
- </html>
其實(shí)縮放很簡單,稍微復(fù)雜的是,如何讓鼠標(biāo)成為放大或者縮小的中心。如果數(shù)學(xué)幾何不好,計(jì)算公式就可能看不明白了。
JavaScript Code復(fù)制內(nèi)容到剪貼板
- 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();
- }
這個(gè)時(shí)候,基本功能就實(shí)現(xiàn)了,加載一張圖片和加載多張圖片都差不多,維護(hù)每一張圖片的位置和大小,下面來整理一下代碼吧。
JavaScript Code復(fù)制內(nèi)容到剪貼板
- 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
- };
- }
童鞋們一定在期待著我說解決副作用的途徑。很遺憾,沒有什么好的方法去解決這些副作用。如果想固定左上角坐標(biāo)縮放,可以把左上角坐標(biāo)變成(0,0),這樣的話無論是什么倍數(shù),0乘上它還是0,所以不變。如果不想讓線條粗細(xì)變化,那就別使用線條?;蛘咦约悍庋b一個(gè)函數(shù),不要使用scale()。
究其根本,之前我們說過平移變換、旋轉(zhuǎn)變換、縮放變換都屬于坐標(biāo)變換,或者說是畫布變換。因此,縮放并非縮放的是圖像,而是整個(gè)坐標(biāo)系、整個(gè)畫布!就像是對(duì)坐標(biāo)系的單位距離縮放了一樣,所以坐標(biāo)和線條都會(huì)進(jìn)行縮放。仔細(xì)想想,這一切貌似挺神奇的。