Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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