1.0.11 • Published 3 years ago
color-thief-wasm-web v1.0.11
WebAssembly for Color Thief algorithm
Use WASM implement Color Thief algorithm for high performance
ref: color-thief
Install
You can install specify dependcines for different platform.
Node.js
$ npm i color-thief-wasmWeb
$ npm i color-thief-wasm-webBundler(Webpack)
$ npm i color-thief-wasm-bundlerminiprogram
$ npm i color-thief-wasm-miniprogramHow to use
In Node.js
const {
    get_color_thief
} = require('color-thief-wasm')
// export function get_color_thief(colors: Uint8Array, pixel_count: number, quality: number, colors_count: number): Array<any>;
const colors = get_color_thief(data, 64 * 64, 10, 5)In web
<script type="module">
    import init,{
        get_color_thief
    } from "./pkg-web/color_thief_wasm.js"
    init().then(() => {
      const colors = get_color_thief(data, 64 * 64, 10, 5)
      console.log(colors)
    })
</script>In miniprogram,we must modify wasm-pack build output content for compatable
import init, { get_color_thief } from 'color-thief-wasm-miniprogram/color_thief_wasm'
await init('/xxxpath/color_thief_wasm_bg.wasm') // there must provide absolute path in miniprogram
const colors = get_color_thief(data, 64 * 64, 10, 5)Or you can copy color-thief-wasm-miniprogram to your project for convenience that you can
$ tree ./ -I node_modules -L 2
./
├── miniprogram
│   ├── app.json
│   ├── app.less
│   ├── app.ts
│   ├── pages
│   ├── color-thief-wasm-miniprogram
│   └── utils
├── package-lock.json
├── package.json
├── project.config.json
├── tsconfig.json
├── typings
│   ├── index.d.ts
│   └── types// pages/index/index.js
import init, { get_color_thief } from '../../color-thief-wasm-miniprogram/color_thief_wasm'
await init('/color-thief-wasm-miniprogram/color_thief_wasm_bg.wasm') // there must provide absolute path
const colors = get_color_thief(data, 64 * 64, 10, 5)How to build
Before Build, must install rust wasm-pack and pnpm
$ npm run buildbenchmark
$ npm run bench
> color-thief-wasm@1.0.2 bench
> node bench.js
Running "GetColorThief" suite...
Progress: 100%
  Wasm GetColorThief:
    12 983 ops/s, ±3.27%   | fastest
  JavaScript GetColorThief:
    2 021 ops/s, ±6.99%    | slowest, 84.43% slower
Finished 2 cases!
  Fastest: Wasm GetColorThief
  Slowest: JavaScript GetColorThiefPublish
$ npm version patch && git push origin master