Last active
September 8, 2021 03:14
-
-
Save akinuri/069df4320fbfb05cb00bae0b588e4b7e to your computer and use it in GitHub Desktop.
"Fit All in Window" command in Illustrator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calcRect(rectArray) { | |
var rect = { | |
rect : rectArray.join(", "), | |
topLeft : { | |
x : rectArray[0], | |
y : rectArray[1], | |
}, | |
bottomRight : { | |
x : rectArray[2], | |
y : rectArray[3], | |
}, | |
}; | |
rect.width = Math.abs(rect.bottomRight.x - rect.topLeft.x); | |
rect.height = Math.abs(rect.bottomRight.y - rect.topLeft.y); | |
rect.center = [ | |
rect.topLeft.x + (rect.width / 2), | |
rect.topLeft.y - (rect.height / 2) | |
]; | |
rect.aspectRatio = rect.width / rect.height; | |
return rect; | |
} | |
function calcViewRect(rectArray, zoom) { | |
var rect = { | |
rect : rectArray.join(", "), | |
topLeft : { | |
x : rectArray[0], | |
y : rectArray[1], | |
}, | |
bottomRight : { | |
x : rectArray[2], | |
y : rectArray[3], | |
}, | |
}; | |
rect.width = Math.abs(rect.bottomRight.x - rect.topLeft.x); | |
rect.height = Math.abs(rect.bottomRight.y - rect.topLeft.y); | |
rect.zoom = zoom || 1; | |
rect.actualWidth = rect.width * zoom; | |
rect.actualHeight = rect.height * zoom; | |
rect.aspectRatio = rect.width / rect.height; | |
return rect; | |
} | |
function getDocumentBounds(artboards) { | |
var rect = { | |
topLeft : { | |
x : null, | |
y : null, | |
}, | |
bottomRight : { | |
x : null, | |
y : null, | |
}, | |
}; | |
for (var i = 0; i < artboards.length; i++) { | |
var artboardRect = calcRect(artboards[i].artboardRect); | |
if (rect.topLeft.x == null) { | |
rect.topLeft.x = artboardRect.topLeft.x; | |
} else { | |
if (artboardRect.topLeft.x < rect.topLeft.x) { | |
rect.topLeft.x = artboardRect.topLeft.x; | |
} | |
} | |
if (rect.topLeft.y == null) { | |
rect.topLeft.y = artboardRect.topLeft.y; | |
} else { | |
if (artboardRect.topLeft.y > rect.topLeft.y) { | |
rect.topLeft.y = artboardRect.topLeft.y; | |
} | |
} | |
if (rect.bottomRight.x == null) { | |
rect.bottomRight.x = artboardRect.bottomRight.x; | |
} else { | |
if (artboardRect.bottomRight.x > rect.bottomRight.x) { | |
rect.bottomRight.x = artboardRect.bottomRight.x; | |
} | |
} | |
if (rect.bottomRight.y == null) { | |
rect.bottomRight.y = artboardRect.bottomRight.y; | |
} else { | |
if (artboardRect.bottomRight.y < rect.bottomRight.y) { | |
rect.bottomRight.y = artboardRect.bottomRight.y; | |
} | |
} | |
} | |
rect.rect = [rect.topLeft.x, rect.topLeft.y, rect.bottomRight.x, rect.bottomRight.y]; | |
return rect; | |
} | |
function calcZoom(viewRect, documentRect, margin) { | |
if (documentRect.aspectRatio > viewRect.aspectRatio) { | |
return parseFloat(((viewRect.actualWidth - (2 * margin)) / documentRect.width).toFixed(2)); | |
} else { | |
return parseFloat(((viewRect.actualHeight - (2 * margin)) / documentRect.height).toFixed(2)); | |
} | |
} | |
function fitAll(document) { | |
var view = document.views[0]; | |
var viewRect = calcViewRect(view.bounds, view.zoom); | |
var documentRect = calcRect(getDocumentBounds(document.artboards).rect); | |
view.centerPoint = documentRect.center; | |
view.zoom = calcZoom(viewRect, documentRect, 20); | |
} | |
// fitAll(app.activeDocument); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment