@thi.ng/dl-asset

@thi.ng/dl-asset

npm versionnpm downloads Mastodon Follow

[!NOTE] This is one of 190 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.

🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️

About

Canvas, video recording & file asset download helpers for web apps.

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

  • @thi.ng/mime - 650+ file extension to MIME type mappings, based on mime-db

Installation

yarn add @thi.ng/dl-asset

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/dl-asset"></script>

Skypack documentation

For Node.js REPL:

const dlAsset = await import("@thi.ng/dl-asset");

Package sizes (brotli'd, pre-treeshake): ESM: 576 bytes

Dependencies

Usage examples

Several projects in this repo's /examples directory are using this package:

ScreenshotDescriptionLive demoSource
Self-modifying, animated typographic grid with emergent complex patternsDemoSource
Probabilistic color theme generatorDemoSource
Color palette generation via dominant color extraction from uploaded imagesDemoSource
Fiber-based cooperative multitasking basicsDemoSource
2.5D hidden line visualization of digital elevation files (DEM)DemoSource
Various hdom-canvas shape drawing examples & SVG conversion / exportDemoSource
Barnsley fern IFS fractal rendererDemoSource
Worker based, interactive Mandelbrot visualizationDemoSource
Parser grammar livecoding editor/playground & codegenDemoSource
Randomized 4-point 2D color gradient image generatorDemoSource
Live coding playground for 2D geometry generation using @thi.ng/pointfree-langDemoSource
Generative audio synth offline renderer and WAV file exportDemoSource
Animated Voronoi diagram, cubic splines & SVG downloadDemoSource
Interactive grid generator, SVG generation & export, undo/redo supportDemoSource
Multi-layer vectorization & dithering of bitmap imagesDemoSource
rdom & WebGL-based image channel editorDemoSource
1D Wolfram automata with OBJ point cloud exportDemoSource
Zig-based DOM creation & canvas drawing appDemoSource

API

Generated API docs

Basic usage

import { download } from "@thi.ng/dl-asset";

const src = "Hellö wörld!";

// mime type derived from file extension (.txt)
download("hello.txt", src, {
utf8: true,
expire: 1000
});

// with explicit MIME type string
download("hello.txt", src, {
mime: "text/plain",
utf8: true,
expire: 1000
});

...or using MIME type preset from @thi.ng/mime.

import { preferredType } from "@thi.ng/mime";

downloadWithMime("hello.txt", src, {
// here mandatory to provide MIME type
mime: preferredType("txt"), // text/plain
utf8: true,
expire: 1000
});

Simplified canvas downloads

Since v2.1.0, HTML canvas downloads can be simplified using downloadCanvas():

import { downloadCanvas } from "@thi.ng/dl-asset";

// download as PNG (default)
downloadCanvas(canvas, "file-name-without-ext");

// download as JPG or WebP with given quality
downloadCanvas(canvas, "file-name-without-ext", "jpeg", 0.9);

downloadCanvas(canvas, "file-name-without-ext", "webp", 0.9);

Authors

If this project contributes to an academic publication, please cite it as:

@misc{thing-dl-asset,
title = "@thi.ng/dl-asset",
author = "Karsten Schmidt",
note = "https://thi.ng/dl-asset",
year = 2020
}

License

© 2020 - 2024 Karsten Schmidt // Apache License 2.0

Generated using TypeDoc