var canvas=document.getElementById("myCanvas");
var context=canvas.getContext("2d");
var cw,ch,iw,ih;
var currentScale=1.00;
var img=new Image();
img.onload=start;
img.src="https://dl.dropboxusercontent.com/u/139992952/multple/flower.png";
function start(){
// cache the image size and set the canvas size to image size
cw=iw=canvas.width=img.width;
ch=ih=canvas.height=img.height;
// activate the zoom buttons
document.getElementById('zoomOut').onclick=function(){
currentScale/=1.1;
zoom(currentScale);
};
document.getElementById('zoomIn').onclick=function(){
currentScale*=1.1;
zoom(currentScale);
};
zoom(currentScale);
}
function zoom(scale) {
// clear the canvas
context.clearRect(0,0,cw,ch);
// reset the [0,0] origin to the center
// of the canvas using context.translate
context.translate(cw/2,ch/2);
// redraw the image using the scaling version of drawImage
var newWidth=iw*scale;
var newHeight=ih*scale;
context.drawImage(
// our drawing source is the img
img,
// start with the entire img
0,0,img.width,img.height,
// draw the img at its scaled size
// since [0,0] is now at center canvas, reset the x,y
// as half the scaled image size
-newWidth/2,-newHeight/2,newWidth,newHeight
);
// clean up, reset the canvas origin by undoing the last translate()
context.translate(-cw/2,-ch/2);
// do your stuff!
// cleanTextOnCanvas();
}
body{ background-color: ivory; }
canvas{border:1px solid red;}
<button id='zoomOut'>Zoom Out</button>
<button id='zoomIn'>Zoom In</button>
<br>
<canvas id="myCanvas" width=300 height=300></canvas>