Bitbybit Docs
    Preparing search index...

    Class Asset

    Files in and out of a script: assets the running application stores under a name, files fetched from a URL, downloads, and conversions between File, Blob, ArrayBuffer and Uint8Array. The application supplies the lookups through assetManager, so what an asset name resolves to depends on where the script runs; fetching needs an endpoint that allows cross-origin requests.

    Index

    Constructors

    Properties

    assetManager: AssetManager

    convert

    • Reads all the bytes of a File or Blob into an ArrayBuffer, the form binary loaders and the STEP converters take.

      Parameters

      • inputs: FileDto

        The File or Blob to read

      Returns Promise<ArrayBuffer>

      The bytes as an ArrayBuffer

      const buffer = await bitbybit.asset.toArrayBuffer({ file });
      
    • Reads all the bytes of a File or Blob into a Uint8Array, a byte array that can be indexed and sliced.

      Parameters

      • inputs: FileDto

        The File or Blob to read

      Returns Promise<Uint8Array<ArrayBufferLike>>

      The bytes as a Uint8Array

      const bytes = await bitbybit.asset.toUint8Array({ file });
      
    • Wraps a Blob in a File with a name and a MIME type, which loaders that want a file name need; the Blob's own type is kept when mimeType is left out.

      Parameters

      • inputs: BlobToFileDto

        The Blob, the file name and the optional MIME type

      Returns File

      The File

      const file = bitbybit.asset.blobToFile({ blob, fileName: "part.step", mimeType: "application/step" });
      
    • Copies the bytes of a File into a plain Blob of the same type, dropping the name; a Blob given in comes back as a copy.

      Parameters

      • inputs: FileDto

        The File or Blob to copy

      Returns Blob

      The Blob

      const blob = bitbybit.asset.fileToBlob({ file });
      
    • Views the bytes of an ArrayBuffer as a Uint8Array; no bytes are copied, both share the same memory.

      Parameters

      Returns Uint8Array

      The Uint8Array over the same bytes

      const bytes = bitbybit.asset.arrayBufferToUint8Array({ arrayBuffer });
      
    • Copies exactly the bytes a Uint8Array covers into a new ArrayBuffer, so a view over part of a larger buffer gives only its own part.

      Parameters

      Returns ArrayBuffer

      The new ArrayBuffer

      const buffer = bitbybit.asset.uint8ArrayToArrayBuffer({ uint8Array: bytes });
      

    create

    • Makes a temporary URL for a File or Blob held in memory, so it can be handed to anything that loads from a URL, such as a texture or a model loader.

      The URL lives as long as the page does.

      Parameters

      Returns string

      The temporary URL

      const url = bitbybit.asset.createObjectURL({ file });
      
    • Makes a temporary URL for each File or Blob in a list, in the same order, as createObjectURL does for one.

      Parameters

      Returns string[]

      One temporary URL per file, in the same order

      const urls = bitbybit.asset.createObjectURLs({ files: [fileA, fileB] });
      

    download

    • Starts a browser download of the given content as a file named fileName plus the extension.

      Text content is wrapped in a Blob of the contentType; a Blob is downloaded as it is.

      Parameters

      • inputs: DownloadDto

        The file name, the content, the extension and the content type

      Returns void

      bitbybit.asset.download({ fileName: "points", content: "x,y,z\n1,2,3", extension: "csv", contentType: "text/csv" });
      

    fetch

    • Downloads the content at a URL as a Blob, raw bytes without a file name; the server must allow cross-origin requests.

      Parameters

      Returns Promise<Blob>

      The response body as a Blob

      const blob = await bitbybit.asset.fetchBlob({ url: "https://example.com/models/part.glb" });
      
    • Downloads the content at a URL as a File named after the last part of the URL, without its query string; the server must allow cross-origin requests.

      Parameters

      Returns Promise<File>

      The response body as a File

      const file = await bitbybit.asset.fetchFile({ url: "https://example.com/models/part.step" });
      const shape = await bitbybit.occt.io.loadSTEPorIGES({ assetFile: file, adjustZtoY: true });
    • Downloads the content at a URL and parses it as JSON; the server must allow cross-origin requests and the body must be valid JSON.

      Parameters

      Returns Promise<any>

      The parsed JSON value

      const settings = await bitbybit.asset.fetchJSON({ url: "https://example.com/data/settings.json" });
      
    • Downloads the content at a URL as plain text; the server must allow cross-origin requests.

      Parameters

      Returns Promise<string>

      The response body as text

      const csv = await bitbybit.asset.fetchText({ url: "https://example.com/data/points.csv" });
      

    get

    • Loads a named asset of the running application as a File, through the lookup the application supplies in assetManager.getAsset.

      Which store the name is looked up in depends on the application; a missing asset rejects the promise.

      Parameters

      Returns Promise<File>

      The asset as a File

      const file = await bitbybit.asset.getFile({ fileName: "part.step" });
      const shape = await bitbybit.occt.io.loadSTEPorIGES({ assetFile: file, adjustZtoY: true });
    • Loads a named asset of the running application and reads it as text, for JSON, CSV or other text files stored as assets.

      Parameters

      Returns Promise<string>

      The asset's content as text

      const csv = await bitbybit.asset.getTextFile({ fileName: "points.csv" });
      const rows = bitbybit.csv.parseToArray({ csv, rowSeparator: "\n", columnSeparator: "," });
    • Loads a named local asset, one kept in the browser rather than on a server, through the lookup the application supplies in assetManager.getLocalAsset.

      A name that resolves to several files gives a list.

      Parameters

      Returns Promise<File | File[]>

      The asset as a File, or a list of Files when the name holds several

      const file = await bitbybit.asset.getLocalFile({ fileName: "part.step" });
      
    • Loads a named local asset, one kept in the browser rather than on a server, and reads it as text; a name that resolves to several files gives a list of texts.

      Parameters

      Returns Promise<string | string[]>

      The content as text, or a list of texts when the name holds several files

      const text = await bitbybit.asset.getLocalTextFile({ fileName: "settings.json" });