Class: Util

Util

Utility class providing various helper functions for OpenLIME. Includes methods for SVG manipulation, file loading, image processing, and string handling.


new Util()

Source:

Methods


<async, static> appendImg(container, url [, imgClass])

Appends loaded image to container

Parameters:
Name Type Argument Default Description
container HTMLElement

Target container

url string

Image URL

imgClass string <optional>
null

Optional CSS class

Source:
Returns:
Type
Promise.<void>

<async, static> appendImgs(container, urls [, imgClass])

Appends multiple images to container

Parameters:
Name Type Argument Default Description
container HTMLElement

Target container

urls Array.<string>

Array of image URLs

imgClass string <optional>
null

Optional CSS class

Source:
Returns:
Type
Promise.<void>

<static> computeSDF(buffer, w, h [, cutoff] [, radius])

Computes Signed Distance Field from image data Implementation based on Felzenszwalb & Huttenlocher algorithm

Parameters:
Name Type Argument Default Description
buffer Uint8Array

Input image data

w number

Image width

h number

Image height

cutoff number <optional>
0.25

Distance field cutoff

radius number <optional>
8

Maximum distance to compute

Source:
Returns:

Computed distance field

Technical Details:

  • Uses 2D Euclidean distance transform
  • Separate inner/outer distance fields
  • Optimized grid computation
  • Sub-pixel accuracy
Type
Float32Array | Array

<static> createSVGElement(tag [, attributes])

Creates an SVG element with optional attributes

Parameters:
Name Type Argument Description
tag string

SVG element tag name

attributes Object <optional>

Key-value pairs of attributes

Source:
Returns:

Created SVG element

Type
SVGElement
Example
```javascript
const circle = Util.createSVGElement('circle', {
    cx: '50',
    cy: '50',
    r: '40'
});
```

<static> isSVGString(input)

Tests if string is valid SVG content

Parameters:
Name Type Description
input string

String to test

Source:
Returns:

True if string is valid SVG

Type
boolean

<async, static> loadHTML(url)

Loads HTML content from URL

Parameters:
Name Type Description
url string

URL to HTML file

Source:
Throws:

If fetch fails

Type
Error
Returns:

HTML content

Type
Promise.<string>

<async, static> loadImage(url)

Loads image from URL

Parameters:
Name Type Description
url string

Image URL

Source:
Throws:

If image loading fails

Type
Error
Returns:

Loaded image

Type
Promise.<HTMLImageElement>

<async, static> loadJSON(url)

Loads and parses JSON from URL

Parameters:
Name Type Description
url string

URL to JSON file

Source:
Throws:

If fetch or parsing fails

Type
Error
Returns:

Parsed JSON data

Type
Promise.<Object>

<async, static> loadSVG(url)

Loads SVG file from URL

Parameters:
Name Type Description
url string

URL to SVG file

Source:
Throws:

If fetch fails or content isn't SVG

Type
Error
Returns:

Loaded and parsed SVG

Type
Promise.<SVGElement>
Example
```javascript
const svg = await Util.loadSVG('icons/icon.svg');
document.body.appendChild(svg);
```

<static> padZeros(num, size)

Pads a number with leading zeros

Parameters:
Name Type Description
num number

Number to pad

size number

Desired string length

Source:
Returns:

Zero-padded number string

Type
string
Example
```javascript
Util.padZeros(42, 5); // Returns "00042"
```

<async, static> rasterizeSVG(url [, size])

Rasterizes SVG to ImageData

Parameters:
Name Type Argument Default Description
url string

SVG URL

size Array.<number> <optional>
[64,64]

Output dimensions [width, height]

Source:
Returns:

Rasterized image data

Processing steps:

  1. Loads SVG file
  2. Sets up canvas context
  3. Handles aspect ratio
  4. Centers image
  5. Renders to ImageData
Type
Promise.<ImageData>
Example
```javascript
const imageData = await Util.rasterizeSVG('icon.svg', [128, 128]);
context.putImageData(imageData, 0, 0);
```

<static> SVGFromString(text)

Parses SVG string into DOM element

Parameters:
Name Type Description
text string

SVG content string

Source:
Throws:

If parsing fails

Type
Error
Returns:

Parsed SVG element

Type
SVGElement