Skip to content

Instantly share code, notes, and snippets.

@blvz
Last active January 8, 2016 05:09
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 blvz/aa000d02ff2109ba4155 to your computer and use it in GitHub Desktop.
Save blvz/aa000d02ff2109ba4155 to your computer and use it in GitHub Desktop.
Creates a primitive rectangle for the current selection. Useful when adding background for text boxes.
// by @blvz
var padding = 10
, radius = 10;
(function(doc) {
var bounds, rect, newRect, fill, stroke
, tml = doc.getTimeline()
, sel = doc.selection.concat()
, rs = sel.filter(function(e) { return e.elementType == 'shape'
&& e.isRectangleObject })
sel = sel.filter(function(e) { return e !== rect })
if (!sel.length) return
rect = rs[0]
if (rect) rect.selected = false
bounds = doc.getSelectionRect()
bounds.left -= padding
bounds.top -= padding
bounds.right += padding
bounds.bottom += padding
doc.selectNone()
if (rect) rect.selected = true, fill = doc.getCustomFill(), stroke = doc.getCustomStroke()
doc.addNewPrimitiveRectangle(bounds, 0)
doc.selectNone()
newRect = tml.layers[tml.currentLayer].frames[tml.currentFrame].elements.concat().pop()
newRect.selected = true
doc.setRectangleObjectProperty('lockFlag', true)
doc.setRectangleObjectProperty('topLeftRadius', radius)
if (rect) doc.setCustomFill(fill), doc.setCustomStroke(stroke)
doc.selectNone()
if (rect) rect.selected = true, doc.deleteSelection()
sel.forEach(function(e) { e.selected = true })
doc.clipCut()
doc.clipPaste(true)
newRect.selected = true
})(fl.getDocumentDOM())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment