//REF: http://stackoverflow.com/questions/17426080/html5-canvas-rotate-zoom-image
var canvas = document.getElementById('canvas');
var image = document.getElementById('image');
var element = canvas.getContext("2d");
//set delta for zoom and keep track of current zoom
var zoomDelta = 0.1;
var currentScale = 1;
//set delta for rotation and keep of current rotation
var currentAngle = 0;
var startX, startY, isDown = false;
jQuery('#carregar').click(function () {
element.translate(canvas.width / 2, canvas.height / 2);
//the new refactored function common to all steps
drawImage();
jQuery('#canvas').attr('data-girar', 0);
this.disabled = true;
});
jQuery('#giraresq').click(function () {
angleInDegrees = -90;
currentAngle += angleInDegrees;
drawImage();
});
jQuery('#girardir').click(function () {
angleInDegrees = 90;
currentAngle += angleInDegrees;
drawImage();
});
jQuery('#zoomIn').click(function () {
currentScale += zoomDelta;
drawImage();
});
jQuery('#zoomOut').click(function () {
currentScale -= zoomDelta;
drawImage();
});
canvas.onmousedown = function (e) {
var pos = getMousePos(canvas, e);
startX = pos.x; //store current position
startY = pos.y;
isDown = true; //mark that we are in move operation
}
canvas.onmousemove = function (e) {
if (isDown === true) {
var pos = getMousePos(canvas, e);
var x = pos.x;
var y = pos.y;
//translate difference from now and start
element.translate(x - startX, y - startY);
drawImage();
//update start positions for next loop
startX = x;
startY = y;
}
}
//reset move operation status
canvas.onmouseup = function (e) {
isDown = false;
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
// Function to clear canvas
function clear()...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.