Tutorial: Get shapes and sizes

Get shapes and sizes

The following examples, shows how to get shapes and their sizing using plugins. Most of the examples are designed to be copied (just click on the code) and pasted into your own plugin. To understand how to use plugins, see Using plugins.

Get all the shapes, and resize the page to fit with a border

In this example, we shall open a new drawing and draw some shapes. Then we shall run our plugin, which will get the bounding box of all shapes, and reposition the shapes with a 10 pixel border and resize the page to fit all shapes.

var shapes = Vecta.activePage.shapes(), //get all shapes on the active page
    select = new Vecta.Selection(shapes), //create a selection object and add in all shapes
    box = select.box(), //get the total bounding box of all shapes
    border = 10; //10 pixel border

//Calculate the offset to move shapes with a 10 pixel border
box.offset_x = border - box.x;
box.offset_y = border - box.y;

//Iterate through each shape and offset them
shapes.forEach(function (shape) {

    //Read the current position, calling moveU without parameters will default to using pixels
    var move = shape.moveU();
    
    //Offset the shape
    shape.move(move.x + box.offset_x, move.y + box.offset_y);
});

//Once we moved all shapes, we set the page size to the size of our merged bounding boxes
//plus 2 times the border, because we have left and right plus top and bottom borders
Vecta.activePage.width(box.width + border * 2) //set width 
    .height(box.height + border * 2); //set height
See also:

Get the currently selected shapes and resize

In this example, we wanted the user to select some shapes, and then run the plugin. The plugin will then resize all shapes to double their size, including stroke widths and font sizes.

var shapes = Vecta.selection.shapes(), //get the current selection object shapes
    ratio = 2; //resize by a ratio of 2

shapes.forEach(function (shape) {
    var size = shape.sizeU(), //Get the shape size in pixels
        //Because stroke width may contain units, so we create a Vecta.Length object and convert to pixels
        stroke_width = new Vecta.Length(shape.strokeWidth()).px(),
        //Font size may return units, so we convert to pixels        
        font_size = new Vecta.Length(shape.fontSize()).px();
    
    //Set the shape size to 2 times original
    shape.size(size.width * ratio, size.height * ratio)
        .strokeWidth(stroke_width * ratio)
        .fontSize(font_size * ratio);
});

//Upon completion, we need to refresh the selection handles
Vecta.selection.refresh();
See also:
vecta.io Early Access