I am trying to create zoom plugin on canvas image[I do understand there are pre-defined plugins available, thought of doing it on my own]. On mouse over of the canvas image I am copying the particular portion of original image to another canvas placed below the original image. I tried ctxZoom.scale(2, 2);
but the copied image is not getting zoomed. On moving the mouse over the real image, zoomed image should be displayed below the original image which is not happening now. Any idea where I went wrong?
HTML
<div id="imageContainer">
<div class="active">
<canvas id="canvas"></canvas>
<canvas id="canvasZoom"></canvas>
</div>
</div>
JS
$(document).ready(function() {
var canvasZoom = document.getElementById('canvasZoom');
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var img = new Image();
loadImage = function(src) {
img.crossOrigin = '';
img.src = src;
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
ctx.restore();
ctx.drawImage(img, 0, 0, img.width, img.height);
}
}
loadImage("http://www.foreignstudents.com/sites/default/files/webfm/car_0.jpg");
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
canvas.addEventListener('mousemove', function(e) {
$("#canvasZoom").show();
$('#canvasZoomm').css({
left: e.pageX - 150,
top: e.pageY
});
var mousePos = getMousePos(canvas, e);
console.log('Mouse position: ' + mousePos.x + ',' + mousePos.y)
//grab the context from your destination canvas
var ctxZoom = canvasZoom.getContext('2d');
ctxZoom.scale(2, 2);//Not Working
ctxZoom.putImageData(ctx.getImageData(mousePos.x - 150, mousePos.y - 60, 500, 500), 0, 0);
ctxZoom.scale(2, 2);//Not Working
ctx.restore();
});
canvas.addEventListener('mouseout', function(e) {
//$("#canvasZoom").hide('slow');
});
});