I try to get zoom effect instead of scaling just with div movement's (left, top) and increase canvas size:
i bind mouse click for this functionality:
first click works perfect (where i click there zoom in)
second click doesn't work (where i click not zoom in this position)
i need get zoom like this: http://phrogz.net/tmp/canvas_zoom_to_cursor.html but not use scale
and translate
i just need use $(div).css('left', 'xPX') and $(div).css('top', 'xPX');
/* DOCUMENTATION:
first click = 1.4 scale
second click = 1.8 scale
i try to get zoom effect instead of scaling just with div movement's (left, top):
first click works perfect (where i click there zoom in)
second click doesn't work (where i click not zoom in this position)
*/
var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');
$('#p').css('left', '100px');
$('#p').css('top', '100px');
var click = 0;
draw100Scale();
$(document).click(function(e) {
var pos = getMousePos(e);
if(click == 0) {
/* FIRST CLICK == FIRST ZOOM */
canvas.width = 700; //500 + 40% = 700
canvas.height = 700; //500 + 40% = 700
$('#p').css('left', $('#p').offset().left + $('#p').offset().left * 40 / 100 - pos.x * 40 / 100);
$('#p').css('top',
$('#p').offset().top + $('#p').offset().top * 40 / 100 - pos.y * 40 / 100);
draw140Scale();
click++;
} else {
/* SECOND CLICK = SECOND ZOOM */
canvas.width = 900; //500 + 80% = 900
canvas.height = 900; //500 + 80% = 900
$('#p').css('left', $('#p').offset().left + $('#p').offset().left * 80 / 100 - pos.x * 80 / 100);
$('#p').css('top',
$('#p').offset().top + $('#p').offset().top * 80 / 100 - pos.y * 80 / 100);
draw180Scale();
click++;
}
});
/* FUNCTIONS */
function getMousePos(evt) {
evt = evt.originalEvent || window.event || evt;
if (evt.clientX !== undefined && evt.clientY !== undefined) {
return {
x: evt.clientX,
y: evt.clientY
};
}
}
function draw100Scale() {
ctx.fillStyle = '#CECECE';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'red';
ctx.fillRect(100, 100, 100, 100);
ctx.fillStyle = 'green';
ctx.fillRect(100, 200, 100, 100);
ctx.fillStyle = 'blue';
ctx.fillRect(100, 300, 100, 100);
ctx.fillStyle = 'yellow';
ctx.fillRect(200, 100, 100, 100);
ctx.fillStyle = 'pink';
ctx.fillRect(200, 200, 100, 100);
ctx.fillStyle = 'orange';
ctx.fillRect(200, 300, 100, 100);
ctx.fillStyle = 'violet';
ctx.fillRect(300, 100, 100, 100);
ctx.fillStyle = 'grey';
ctx.fillRect(300, 200, 100, 100);
ctx.fillStyle = 'white';
ctx.fillRect(300, 300, 100, 100);
}
function draw140Scale() {
ctx.fillStyle = '#CECECE';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'red';
ctx.fillRect(140, 140, 140, 140);
ctx.fillStyle = 'green';
ctx.fillRect(140, 280, 140, 140);
ctx.fillStyle = 'blue';
ctx.fillRect(140, 420, 140, 140);
ctx.fillStyle = 'yellow';
ctx.fillRect(280, 140, 140, 140);
ctx.fillStyle = 'pink';
ctx.fillRect(280, 280, 140, 140);
ctx.fillStyle = 'orange';
ctx.fillRect(280, 420, 140, 140);
ctx.fillStyle = 'violet';
ctx.fillRect(420, 140, 140, 140);
ctx.fillStyle = 'grey';
ctx.fillRect(420, 280, 140, 140);
ctx.fillStyle = 'white';
ctx.fillRect(420, 420, 140, 140);
}
function draw180Scale() {
ctx.fillStyle = '#CECECE';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'red';
ctx.fillRect(180, 180, 180, 180);
ctx.fillStyle = 'green';
ctx.fillRect(180, 360, 180, 180);
ctx.fillStyle = 'blue';
ctx.fillRect(180, 540, 180, 180);
ctx.fillStyle = 'yellow';
ctx.fillRect(360, 180, 180, 180);
ctx.fillStyle = 'pink';
ctx.fillRect(360, 360, 180, 180);
ctx.fillStyle = 'orange';
ctx.fillRect(360, 540, 180, 180);
ctx.fillStyle = 'violet';
ctx.fillRect(540, 180, 180, 180);
ctx.fillStyle = 'grey';
ctx.fillRect(540, 360, 180, 180);
ctx.fillStyle = 'white';
ctx.fillRect(540, 540, 180, 180);
}
* {
margin: 0px;
padding: 0px;
}
#c {
position: absolute;
}
#p {
position: absolute;
left: 0px;
top: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id='p'>
<canvas id='c' width='500' height='500'></canvas>
</div>