@3846masa/bmp 中文文档教程

发布于 3年前 浏览 28 项目主页 更新于 3年前

@3846masa/bmp

banner

npm-badgemit-license-badge

从 RGBA 原始字节(如 ImageData)创建 BMP(带 alpha 通道)二进制文件。

  • faster than other libraries (e.g. bmp-js)
  • tiny size (basic: ~ 500 bytes, webworker: ~ 700 bytes)
  • supports alpha channel

Table of Contents

Usage

CDN

Basic

基本 codesandbox-badge

请参阅 ./examples/basic

<script type="module">
  import { convert } from 'https://unpkg.com/@3846masa/bmp/lib/convert.mjs';

  const canvas = document.getElementById('canvas');
  const bmpImg = document.getElementById('bmp');

  function main() {
    const ctx = canvas.getContext('2d');

    const raw = ctx.getImageData(0, 0, canvas.width, canvas.height);
    const bmpBinary = convert(raw);
    const blob = new Blob([bmpBinary], { type: 'image/bmp' });

    bmpImg.src = URL.createObjectURL(blob);
  }

  main();
</script>

WebWorker

webworker codesandbox-badge

请参阅 ./examples/webworker

<script type="module">
  import { convert } from 'https://unpkg.com/@3846masa/bmp/lib/worker.mjs';

  const canvas = document.getElementById('canvas');
  const bmpImg = document.getElementById('bmp');

  async function main() {
    const ctx = canvas.getContext('2d');

    const raw = ctx.getImageData(0, 0, canvas.width, canvas.height);
    const bmpBinary = await convert(raw);
    const blob = new Blob([bmpBinary], { type: 'image/bmp' });

    bmpImg.src = URL.createObjectURL(blob);
  }

  main().catch(console.error);
</script>

No module

no-module codesandbox-badge

请参阅 ./examples/canvas-to-blob

<script src="https://unpkg.com/@3846masa/bmp/lib/polyfill.js"></script>
<script>
  const canvas = document.getElementById('canvas');
  const bmpImg = document.getElementById('bmp');

  function main() {
    canvas.toBlob(callback, 'image/bmp');

    function callback(blob) {
      bmpImg.src = URL.createObjectURL(blob);
    }
  }

  main();
</script>

Using via bundler

bundlephobia

npm i @3846masa/bmp
import { convert } from '@3846masa/bmp';

API

convert({ width, height, data }, options?)

将 RGBA 原始字节(如 ImageData)转换为 BMP 二进制文件。

worker.mjs 中,此函数返回 Promise。

widthnumber
heightnumber
dataUint8Array | Uint8ClampedArray
options.strictboolean
ReturnsUint8Array | Promise<Uint8Array>

HTMLCanvasElement.prototype.toBlob(callback, type)

callback(blob: Blob) => any
type'image/bmp'

Contributing

接受 PR。

License

麻省理工学院 (c) 3846masa

@3846masa/bmp

banner

npm-badgemit-license-badge

Create a BMP (w/ alpha channel) binary from RGBA raw bytes like ImageData.

  • faster than other libraries (e.g. bmp-js)
  • tiny size (basic: ~ 500 bytes, webworker: ~ 700 bytes)
  • supports alpha channel

Table of Contents

Usage

CDN

Basic

basic codesandbox-badge

See ./examples/basic.

<script type="module">
  import { convert } from 'https://unpkg.com/@3846masa/bmp/lib/convert.mjs';

  const canvas = document.getElementById('canvas');
  const bmpImg = document.getElementById('bmp');

  function main() {
    const ctx = canvas.getContext('2d');

    const raw = ctx.getImageData(0, 0, canvas.width, canvas.height);
    const bmpBinary = convert(raw);
    const blob = new Blob([bmpBinary], { type: 'image/bmp' });

    bmpImg.src = URL.createObjectURL(blob);
  }

  main();
</script>

WebWorker

webworker codesandbox-badge

See ./examples/webworker.

<script type="module">
  import { convert } from 'https://unpkg.com/@3846masa/bmp/lib/worker.mjs';

  const canvas = document.getElementById('canvas');
  const bmpImg = document.getElementById('bmp');

  async function main() {
    const ctx = canvas.getContext('2d');

    const raw = ctx.getImageData(0, 0, canvas.width, canvas.height);
    const bmpBinary = await convert(raw);
    const blob = new Blob([bmpBinary], { type: 'image/bmp' });

    bmpImg.src = URL.createObjectURL(blob);
  }

  main().catch(console.error);
</script>

No module

no-module codesandbox-badge

See ./examples/canvas-to-blob.

<script src="https://unpkg.com/@3846masa/bmp/lib/polyfill.js"></script>
<script>
  const canvas = document.getElementById('canvas');
  const bmpImg = document.getElementById('bmp');

  function main() {
    canvas.toBlob(callback, 'image/bmp');

    function callback(blob) {
      bmpImg.src = URL.createObjectURL(blob);
    }
  }

  main();
</script>

Using via bundler

bundlephobia

npm i @3846masa/bmp
import { convert } from '@3846masa/bmp';

API

convert({ width, height, data }, options?)

Convert RGBA raw bytes like ImageData to a BMP binary.

In worker.mjs, this function returns Promise.

widthnumber
heightnumber
dataUint8Array | Uint8ClampedArray
options.strictboolean
ReturnsUint8Array | Promise<Uint8Array>

HTMLCanvasElement.prototype.toBlob(callback, type)

callback(blob: Blob) => any
type'image/bmp'

Contributing

PRs accepted.

License

MIT (c) 3846masa

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文