Skip to content

Instantly share code, notes, and snippets.

@davidtorroija
Created September 23, 2016 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidtorroija/b014817868b739068014788b43477d55 to your computer and use it in GitHub Desktop.
Save davidtorroija/b014817868b739068014788b43477d55 to your computer and use it in GitHub Desktop.
Reveal.prototype.resize = function(canvas) {
var _this, challengeToolsDiv, container, footerHeight, headerHeight, height, init_height, init_ratio, init_width, objects, ratio, ref, resize_height, resize_width, scaleX, scaleY, toolsDiv, toolsHeight, width;
_this = this;
init_width = this.init_width;
init_height = this.init_height;
init_ratio = init_width / init_height;
container = $('#widget-container');
resize_width = false;
resize_height = false;
width = container.find('.main')[0].clientWidth;
height = container[0].clientHeight;
if ((ref = this.cornerPosition) === 'top-left' || ref === 'top-right') {
height = height - 20;
}
if (!this.editor) {
headerHeight = $('.widget-metadata-header').height();
footerHeight = $('.widget-metadata-footer').height();
toolsDiv = $('.tools').height();
challengeToolsDiv = $('.challenge_tools').height();
toolsHeight = challengeToolsDiv + toolsDiv;
height = height - footerHeight - headerHeight - 20;
if (footerHeight) {
toolsHeight = 50;
}
$('.main').height(height + toolsHeight);
}
ratio = width / height;
console.log('init_height', init_height, 'init_width', init_width, init_ratio, 'height/width', height, width, ratio);
if (ratio < init_ratio) {
resize_width = true;
} else {
resize_height = true;
}
objects = canvas.getObjects();
scaleX = scaleY = height / init_height;
$.each(objects, (function(_this) {
return function(index, object) {
var fabricObject, model, newScaleX, newScaleY, new_left, new_top, restX, restY;
if (object.cid) {
model = _this.collection.get(object.cid);
fabricObject = model.get('fabricObject');
} else if (object.isPoint) {
fabricObject = object.toJSON();
}
if (fabricObject) {
console.log('resize', resize_width, resize_height);
if (resize_width) {
scaleX = width / init_width;
scaleY = width / init_width;
restX = ((1 - fabricObject.scaleX) * width) / init_width;
restY = ((1 - fabricObject.scaleY) * width) / init_width;
newScaleY = scaleY - restY;
newScaleX = scaleX - restX;
if ((object.scaleY !== newScaleY) && (newScaleX !== object.scaleX)) {
object.scaleX = newScaleX;
object.scaleY = newScaleY;
new_top = (object.top * width) / init_width;
new_left = (object.left * width) / init_width;
object.set('top', new_top);
object.set('left', new_left);
}
}
if (resize_height) {
restX = 0;
restY = 0;
restX = ((1 - fabricObject.scaleX) * height) / init_height;
restY = ((1 - fabricObject.scaleY) * height) / init_height;
newScaleY = scaleY - restY;
newScaleX = scaleX - restX;
if ((object.scaleY !== newScaleY) && (newScaleX !== object.scaleX)) {
object.scaleX = newScaleX;
object.scaleY = newScaleY;
new_top = (object.top * height) / init_height;
new_left = (object.left * height) / init_height;
object.set('top', new_top);
object.set('left', new_left);
}
}
return object.setCoords();
}
};
})(this));
canvas.setWidth(width);
canvas.setHeight(height);
canvas.renderAll();
if (this.isInChallenge && this.wasResized && this.reportingDisabled) {
this.showAnswers();
this.hideAssessmentTools();
this.wasResized = false;
console.log('rreeee todo');
}
return console.log('resize');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment