Skip to content

Instantly share code, notes, and snippets.

@trischbeck
Last active September 17, 2021 10:24
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 trischbeck/ae82886485f41b015bd1e8a08c91a9ba to your computer and use it in GitHub Desktop.
Save trischbeck/ae82886485f41b015bd1e8a08c91a9ba to your computer and use it in GitHub Desktop.
Sorts selected visual elements within a view, based on https://gist.github.com/robkamp/fac2b2d80ee44e7085c70738de544341 #archi
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Script Name: alignVertically
Purpose: Sorts selected visual elements within a view by ID (alphabetically) and stacks them in a vertical layout
Builds on the AlignSpreadHorizontally script by Rob Kamp (https://gist.github.com/robkamp/fac2b2d80ee44e7085c70738de544341)
Thomas Rischbeck, rischbeck@itmc.ch, (C) ITMC 2021
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */// Author: Rob Kamp
console.log("Start: Spread and Align vertically");
minY = null;
minX = null;
maxWidth = 0;
maxHeight = 0;
distance = 5;
// Determine the width, height, and
$(selection).each(function(element) {
minY = Math.min(element.bounds.y,minY==null?element.bounds.y:minY);
minX = Math.min(element.bounds.x,minX==null?element.bounds.x:minX);
maxWidth = Math.max(element.bounds.width,maxWidth);
maxHeight = Math.max(element.bounds.height,maxHeight);
});
// Sort the selection so the order stays the same
$(selection).sort(function (left,right) {
return left.bounds.x-right.bounds.x;
});
// Walk throught the sorted selection
$(selection).each(function (element) {
// Set the new bounds
element.bounds = {x: minX, y: minY, width: maxWidth, height: maxHeight};
// Set the spacing for the next element
//minX = minX + maxWidth + 20;
minY = minY + maxHeight + distance;
});
console.log("End: Spread and Align vertically");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment