Skip to content

Utility Functions

Asset Studio exports a set of utility functions for working with geometry and SVG assets.

Import

ts
import { getRotatedBounds, parseSvg, downloadURI, downloadBlob } from '@asset-studio/core'

getRotatedBounds

Calculates the axis-aligned bounding box of a rectangle after rotation. Useful for determining how much space a rotated item occupies on the canvas.

Signature

ts
function getRotatedBounds(
  width: number,
  height: number,
  rotation: number,
): {
  minX: number
  maxX: number
  minY: number
  maxY: number
}

Parameters

ParameterTypeDescription
widthnumberWidth of the rectangle in pixels.
heightnumberHeight of the rectangle in pixels.
rotationnumberRotation angle in degrees.

Return Value

PropertyTypeDescription
minXnumberLeftmost X coordinate of the bounding box.
maxXnumberRightmost X coordinate of the bounding box.
minYnumberTopmost Y coordinate of the bounding box.
maxYnumberBottommost Y coordinate of the bounding box.

Example

ts
const bounds = getRotatedBounds(200, 100, 45)
const boundingWidth = bounds.maxX - bounds.minX
const boundingHeight = bounds.maxY - bounds.minY

parseSvg

Fetches an SVG file from a URL, extracts the first <path> element's d attribute, and computes its bounding box as a viewBox. The returned values can be passed directly to a Shape item.

Signature

ts
async function parseSvg(url: string): Promise<{
  svgPath: string
  viewBox: { x: number; y: number; w: number; h: number }
} | null>

Parameters

ParameterTypeDescription
urlstringURL of the SVG file to parse.

Return Value

Returns an object with the extracted path data, or null if no <path> element is found.

PropertyTypeDescription
svgPathstringThe d attribute of the first <path> in the SVG.
viewBox{ x: number; y: number; w: number; h: number }Bounding box of the path, used as viewBox on a Shape item.

Example

ts
const result = await parseSvg('https://example.com/icon.svg')

if (result) {
  addItem('Shape', {
    x: 100,
    y: 100,
    width: 200,
    height: 200,
    itemProps: {
      svgPath: result.svgPath,
      viewBox: result.viewBox,
      fill: '#7c3aed',
    },
  })
}

downloadURI

Triggers a browser download from a data URL or object URL.

Signature

ts
function downloadURI(uri: string, name: string): void

Parameters

ParameterTypeDescription
uristringThe data URL or object URL to download.
namestringThe file name for the download.

Example

ts
const uri = exportImage(2)
downloadURI(uri, 'design.png')

downloadBlob

Triggers a browser download from a Blob object.

Signature

ts
function downloadBlob(blob: Blob, name: string): void

Parameters

ParameterTypeDescription
blobBlobThe blob to download.
namestringThe file name for the download.

Example

ts
const blob = await exportVideo(30)
if (blob) downloadBlob(blob, 'export.mp4')

Released under a proprietary license.