Skip to content

Instantly share code, notes, and snippets.

@nulayuhz
Forked from clouddueling/fabricMOdule.js
Last active June 1, 2016 12:44
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nulayuhz/6796712604e6678c442e to your computer and use it in GitHub Desktop.
// Reset Zoom
self.resetZoom = function() {
var objects = canvas.getObjects();
for (var i in objects) {
var scaleX = objects[i].scaleX;
var scaleY = objects[i].scaleY;
var left = objects[i].left;
var top = objects[i].top;
var tempScaleX = scaleX * (1 / canvasScale);
var tempScaleY = scaleY * (1 / canvasScale);
var tempLeft = left * (1 / canvasScale);
var tempTop = top * (1 / canvasScale);
objects[i].scaleX = tempScaleX;
objects[i].scaleY = tempScaleY;
objects[i].left = tempLeft;
objects[i].top = tempTop;
objects[i].setCoords();
}
var width = canvas.getWidth();
var height = canvas.getHeight();
var tempWidth = width * (1 / canvasScale);
var tempHeight = height * (1 / canvasScale);
canvas.setWidth(tempWidth);
canvas.setHeight(tempHeight);
canvas.renderAll();
canvasScale = 1;
};
// Zoom In
self.zoomIn = function() {
// TODO limit the max canvas zoom in
canvasScale = canvasScale * SCALE_FACTOR;
var objects = canvas.getObjects();
for (var i in objects) {
var scaleX = objects[i].scaleX;
var scaleY = objects[i].scaleY;
var left = objects[i].left;
var top = objects[i].top;
var tempScaleX = scaleX * SCALE_FACTOR;
var tempScaleY = scaleY * SCALE_FACTOR;
var tempLeft = left * SCALE_FACTOR;
var tempTop = top * SCALE_FACTOR;
objects[i].scaleX = tempScaleX;
objects[i].scaleY = tempScaleY;
objects[i].left = tempLeft;
objects[i].top = tempTop;
objects[i].setCoords();
}
var width = canvas.getWidth();
var height = canvas.getHeight();
var tempWidth = width * SCALE_FACTOR;
var tempHeight = height * SCALE_FACTOR;
canvas.setWidth(tempWidth);
canvas.setHeight(tempHeight);
canvas.renderAll();
};
// Zoom Out
self.zoomOut = function() {
// TODO limit max cavas zoom out
canvasScale = canvasScale / SCALE_FACTOR;
var objects = canvas.getObjects();
for (var i in objects) {
var scaleX = objects[i].scaleX;
var scaleY = objects[i].scaleY;
var left = objects[i].left;
var top = objects[i].top;
var tempScaleX = scaleX * (1 / SCALE_FACTOR);
var tempScaleY = scaleY * (1 / SCALE_FACTOR);
var tempLeft = left * (1 / SCALE_FACTOR);
var tempTop = top * (1 / SCALE_FACTOR);
objects[i].scaleX = tempScaleX;
objects[i].scaleY = tempScaleY;
objects[i].left = tempLeft;
objects[i].top = tempTop;
objects[i].setCoords();
}
var width = canvas.getWidth();
var height = canvas.getHeight();
var tempWidth = width * (1 / SCALE_FACTOR);
var tempHeight = height * (1 / SCALE_FACTOR);
canvas.setWidth(tempWidth);
canvas.setHeight(tempHeight);
canvas.renderAll();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment