Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/tldraw/tldraw/llms.txt

Use this file to discover all available pages before exploring further.

Asset utilities provide functions for manipulating images, sizing boxes, and loading fonts in tldraw applications.

containBoxSize

Contains a size within a given box size while maintaining aspect ratio.
function containBoxSize(
  originalSize: BoxWidthHeight,
  containBoxSize: BoxWidthHeight
): BoxWidthHeight
originalSize
BoxWidthHeight
required
The original size of the asset with w and h properties.
containBoxSize
BoxWidthHeight
required
The container size to fit within.
Returns: Adjusted size that fits within the container while maintaining aspect ratio.

Example

const originalSize = { w: 1920, h: 1080 }
const containerSize = { w: 800, h: 600 }
const fitted = containBoxSize(originalSize, containerSize)
// Result: { w: 800, h: 450 }

downsizeImage

Resize an image Blob to be smaller than its current size with high quality.
async function downsizeImage(
  blob: Blob,
  width: number,
  height: number,
  opts?: { type?: string; quality?: number }
): Promise<Blob>
blob
Blob
required
The image Blob to resize.
width
number
required
The desired width in pixels.
height
number
required
The desired height in pixels.
opts.type
string
The MIME type for the output image. Defaults to the input blob’s type.
opts.quality
number
The image quality from 0 to 1. Defaults to 0.85.
Returns: A promise that resolves to the resized image Blob.

Example

const image = await (await fetch('/image.jpg')).blob()
const size = await getImageSize(image)
const resizedImage = await downsizeImage(
  image,
  size.w / 2,
  size.h / 2,
  { type: 'image/jpeg', quality: 0.85 }
)

preloadFont

Preload a font face and add it to the document fonts.
async function preloadFont(
  id: string,
  font: TLTypeFace
): Promise<FontFace>
id
string
required
The font family identifier.
font
TLTypeFace
required
The font configuration object.
Returns: A promise that resolves to the loaded FontFace instance.

TLTypeFace interface

interface TLTypeFace {
  url: string
  display?: FontDisplay
  featureSettings?: string
  stretch?: string
  style?: string
  unicodeRange?: string
  variant?: string
  weight?: string
  format?: string
}

Example

const fontFace = await preloadFont('MyFont', {
  url: 'https://example.com/fonts/myfont.woff2',
  weight: '400',
  style: 'normal',
  format: 'woff2'
})
// Font is now available for use

Build docs developers (and LLMs) love