Comparing version 1.3.0 to 2.0.0
{ | ||
"name": "blob-util", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "Utilities for working with Blob objects in the browser", | ||
"main": "lib/index.js", | ||
"main": "dist/blob-util.cjs.js", | ||
"module": "dist/blob-util.es.js", | ||
"types": "dist/blob-util.d.ts", | ||
"repository": { | ||
@@ -23,29 +25,38 @@ "type": "git", | ||
"scripts": { | ||
"jshint": "jshint -c .jshintrc lib/*.js test/test.js", | ||
"test": "npm run jshint && zuul ./test/test.js", | ||
"test-local": "zuul ./test/test.js --local 9000", | ||
"build": "mkdirp dist && npm run browserify && npm run min", | ||
"browserify": "browserify . -p bundle-collapser/plugin -s blobUtil | ./bin/es3ify.js | derequire > dist/blob-util.js", | ||
"lint": "standard test/*js bin/*js && tslint src/*ts", | ||
"test": "npm run build && npm run lint && zuul --no-coverage ./test/test.js", | ||
"test-local": "npm run build && zuul ./test/test.js --local 9000 --no-coverage", | ||
"clean": "rimraf dist lib && mkdirp dist lib", | ||
"build": "run-s clean build-ts build-js min", | ||
"build-ts": "tsc src/blob-util.ts --target ES6 --module es2015 --outDir lib -d && cpy lib/blob-util.d.ts dist/", | ||
"build-js": "run-p build-es build-cjs build-umd && rimraf lib", | ||
"build-es": "rollup -i lib/blob-util.js -f es -o dist/blob-util.es.js", | ||
"build-cjs": "rollup -i lib/blob-util.js -f cjs -o dist/blob-util.cjs.js", | ||
"build-umd": "rollup -i lib/blob-util.js -f umd -n blobUtil -o dist/blob-util.js", | ||
"min": "uglifyjs dist/blob-util.js -mc > dist/blob-util.min.js", | ||
"jsdoc2md": "jsdoc2md --heading-depth 3 ./lib/index.js > README.md", | ||
"jsdoc": "jsdoc -d docs ./lib/index.js && mv -f docs/global.html docs/index.html" | ||
"doc": "run-s cleanup-doc gen-doc gen-readme cleanup-doc", | ||
"gen-doc": "typedoc --out docs-tmp --target ES6 --theme markdown --mdHideSources --excludePrivate --exclude private.ts src", | ||
"gen-readme": "node bin/write-docs-to-readme.js", | ||
"cleanup-doc": "rimraf docs-tmp" | ||
}, | ||
"dependencies": { | ||
"blob": "0.0.4", | ||
"native-or-lie": "1.0.2" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^9.0.3", | ||
"bundle-collapser": "^1.1.4", | ||
"chai": "~1.8.1", | ||
"chai-as-promised": "~4.1.0", | ||
"derequire": "^2.0.0", | ||
"es3ify": "^0.1.3", | ||
"cpy-cli": "^1.0.1", | ||
"istanbul": "^0.2.7", | ||
"jsdoc": "^3.3.0-alpha10", | ||
"jsdoc-to-markdown": "^0.5.9", | ||
"jshint": "~2.3.0", | ||
"mkdirp": "^0.5.0", | ||
"mocha": "~1.18", | ||
"native-or-lie": "1.0.2", | ||
"npm-run-all": "^4.1.3", | ||
"pify": "^3.0.0", | ||
"request": "^2.36.0", | ||
"rimraf": "^2.6.2", | ||
"rollup": "^0.59.1", | ||
"standard": "^11.0.1", | ||
"tslint": "^5.10.0", | ||
"tslint-config-standard": "^7.0.0", | ||
"typedoc": "^0.11.1", | ||
"typedoc-plugin-markdown": "^1.1.11", | ||
"typescript": "^2.8.3", | ||
"uglify-js": "^2.4.13", | ||
@@ -56,5 +67,4 @@ "zuul": "^3.10.1", | ||
"files": [ | ||
"lib", | ||
"dist" | ||
] | ||
} |
561
README.md
@@ -1,2 +0,2 @@ | ||
blob-util [![Build Status](https://travis-ci.org/nolanlawson/blob-util.svg)](https://travis-ci.org/nolanlawson/blob-util) | ||
blob-util [![Build Status](https://travis-ci.org/nolanlawson/blob-util.svg)](https://travis-ci.org/nolanlawson/blob-util) [![TypeScript](https://img.shields.io/badge/%3C%2F%3E-typescript-blue.svg)](http://www.typescriptlang.org/) | ||
===== | ||
@@ -30,27 +30,36 @@ | ||
Download it from the `dist/` folder above, or use NPM: | ||
Via npm: | ||
```bash | ||
$ npm install blob-util | ||
npm install blob-util | ||
``` | ||
or Bower: | ||
ES modules are supported: | ||
```bash | ||
$ bower install blob-util | ||
```js | ||
import { canvasToBlob } from 'blob-util' | ||
canvasToBlob(canvas, 'image/png').then(/* ... */) | ||
``` | ||
Then stick it in your HTML: | ||
Or as a script tag: | ||
```html | ||
<script src="blob-util.js"></script> | ||
<script src="https://unpkg.com/blob-util/dist/blob-util.min.js"></script> | ||
``` | ||
Now you have a `window.blobUtil` object. Or if you don't like globals, you can use Browserify. | ||
Then it's available as a global `blobUtil` object: | ||
```js | ||
blobUtil.canvasToBlob(canvas, 'image/png').then(/* ... */) | ||
``` | ||
Browser support | ||
----- | ||
As of v2.0.0, a built-in `Promise` polyfill is no longer provided. Assuming you provide a `Promise` | ||
polyfill, the supported browsers are: | ||
* Firefox | ||
* Chrome | ||
* Edge | ||
* IE 10+ | ||
@@ -110,133 +119,162 @@ * Safari 6+ | ||
Warning: this API uses [Promises](https://promisesaplus.com/), because it's not 2009 anymore. | ||
<!-- begin insert API --> | ||
### Overview | ||
## Index | ||
* [createBlob(parts, options)](#createBlob) | ||
* [createObjectURL(blob)](#createObjectURL) | ||
* [revokeObjectURL(url)](#revokeObjectURL) | ||
* [blobToBinaryString(blob)](#blobToBinaryString) | ||
* [base64StringToBlob(base64, type)](#base64StringToBlob) | ||
* [binaryStringToBlob(binary, type)](#binaryStringToBlob) | ||
* [blobToBase64String(blob)](#blobToBase64String) | ||
* [dataURLToBlob(dataURL)](#dataURLToBlob) | ||
* [blobToDataURL(blob)](#blobToDataURL) | ||
* [imgSrcToDataURL(src, type, crossOrigin, quality)](#imgSrcToDataURL) | ||
* [canvasToBlob(canvas, type, quality)](#canvasToBlob) | ||
* [imgSrcToBlob(src, type, crossOrigin, quality)](#imgSrcToBlob) | ||
* [arrayBufferToBlob(buffer, type)](#arrayBufferToBlob) | ||
* [blobToArrayBuffer(blob)](#blobToArrayBuffer) | ||
<a name="createBlob"></a> | ||
### createBlob(parts, options) | ||
Shim for | ||
[new Blob()](https://developer.mozilla.org/en-US/docs/Web/API/Blob.Blob) | ||
to support | ||
[older browsers that use the deprecated <code>BlobBuilder</code> API](http://caniuse.com/blob). | ||
### Functions | ||
**Params** | ||
* [arrayBufferToBinaryString](#arraybuffertobinarystring) | ||
* [arrayBufferToBlob](#arraybuffertoblob) | ||
* [base64StringToBlob](#base64stringtoblob) | ||
* [binaryStringToArrayBuffer](#binarystringtoarraybuffer) | ||
* [binaryStringToBlob](#binarystringtoblob) | ||
* [blobToArrayBuffer](#blobtoarraybuffer) | ||
* [blobToBase64String](#blobtobase64string) | ||
* [blobToBinaryString](#blobtobinarystring) | ||
* [blobToDataURL](#blobtodataurl) | ||
* [canvasToBlob](#canvastoblob) | ||
* [createBlob](#createblob) | ||
* [createObjectURL](#createobjecturl) | ||
* [dataURLToBlob](#dataurltoblob) | ||
* [imgSrcToBlob](#imgsrctoblob) | ||
* [imgSrcToDataURL](#imgsrctodataurl) | ||
* [revokeObjectURL](#revokeobjecturl) | ||
- parts `Array` - content of the <code>Blob</code> | ||
- options `Object` - usually just <code>{type: myContentType}</code> | ||
--- | ||
**Returns**: `Blob` | ||
## Functions | ||
**Example**: | ||
<a id="arraybuffertobinarystring"></a> | ||
### arrayBufferToBinaryString | ||
▸ **arrayBufferToBinaryString**(buffer: *`ArrayBuffer`*): `string` | ||
Convert an `ArrayBuffer` to a binary string. | ||
Example: | ||
```js | ||
var myBlob = blobUtil.createBlob(['hello world'], {type: 'text/plain'}); | ||
var myString = blobUtil.arrayBufferToBinaryString(arrayBuff) | ||
``` | ||
<a name="createObjectURL"></a> | ||
### createObjectURL(blob) | ||
Shim for | ||
[URL.createObjectURL()](https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL) | ||
to support browsers that only have the prefixed | ||
<code>webkitURL</code> (e.g. Android <4.4). | ||
**Parameters:** | ||
**Params** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| buffer | `ArrayBuffer` | array buffer | | ||
- blob `Blob` | ||
**Returns:** `string` | ||
binary string | ||
**Returns**: `string` - url | ||
___ | ||
<a id="arraybuffertoblob"></a> | ||
**Example**: | ||
### arrayBufferToBlob | ||
▸ **arrayBufferToBlob**(buffer: *`ArrayBuffer`*, type?: *`string`*): `Blob` | ||
Convert an `ArrayBuffer` to a `Blob`. | ||
Example: | ||
```js | ||
var myUrl = blobUtil.createObjectURL(blob); | ||
var blob = blobUtil.arrayBufferToBlob(arrayBuff, 'audio/mpeg'); | ||
``` | ||
<a name="revokeObjectURL"></a> | ||
### revokeObjectURL(url) | ||
Shim for | ||
[URL.revokeObjectURL()](https://developer.mozilla.org/en-US/docs/Web/API/URL.revokeObjectURL) | ||
to support browsers that only have the prefixed | ||
<code>webkitURL</code> (e.g. Android <4.4). | ||
**Parameters:** | ||
**Params** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| buffer | `ArrayBuffer` | - | | ||
| `Optional` type | `string` | the content type (optional) | | ||
- url `string` | ||
**Returns:** `Blob` | ||
Blob | ||
**Example**: | ||
___ | ||
<a id="base64stringtoblob"></a> | ||
### base64StringToBlob | ||
▸ **base64StringToBlob**(base64: *`string`*, type?: *`string`*): `Blob` | ||
Convert a base64-encoded string to a `Blob`. | ||
Example: | ||
```js | ||
blobUtil.revokeObjectURL(myUrl); | ||
var blob = blobUtil.base64StringToBlob(base64String); | ||
``` | ||
<a name="blobToBinaryString"></a> | ||
### blobToBinaryString(blob) | ||
Convert a <code>Blob</code> to a binary string. Returns a Promise. | ||
**Parameters:** | ||
**Params** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| base64 | `string` | base64-encoded string | | ||
| `Optional` type | `string` | the content type (optional) | | ||
- blob `Blob` | ||
**Returns:** `Blob` | ||
Blob | ||
**Returns**: `Promise` - Promise that resolves with the binary string | ||
___ | ||
<a id="binarystringtoarraybuffer"></a> | ||
**Example**: | ||
### binaryStringToArrayBuffer | ||
▸ **binaryStringToArrayBuffer**(binary: *`string`*): `ArrayBuffer` | ||
Convert a binary string to an `ArrayBuffer`. | ||
```js | ||
blobUtil.blobToBinaryString(blob).then(function (binaryString) { | ||
// success | ||
}).catch(function (err) { | ||
// error | ||
}); | ||
var myBuffer = blobUtil.binaryStringToArrayBuffer(binaryString) | ||
``` | ||
<a name="base64StringToBlob"></a> | ||
### base64StringToBlob(base64, type) | ||
Convert a base64-encoded string to a <code>Blob</code>. Returns a Promise. | ||
**Params** | ||
**Parameters:** | ||
- base64 `string` | ||
- type `string` | `undefined` - the content type (optional) | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| binary | `string` | binary string | | ||
**Returns**: `Promise` - Promise that resolves with the <code>Blob</code> | ||
**Returns:** `ArrayBuffer` | ||
array buffer | ||
**Example**: | ||
___ | ||
<a id="binarystringtoblob"></a> | ||
### binaryStringToBlob | ||
▸ **binaryStringToBlob**(binary: *`string`*, type?: *`string`*): `Blob` | ||
Convert a binary string to a `Blob`. | ||
Example: | ||
```js | ||
blobUtil.base64StringToBlob(base64String).then(function (blob) { | ||
// success | ||
}).catch(function (err) { | ||
// error | ||
}); | ||
var blob = blobUtil.binaryStringToBlob(binaryString); | ||
``` | ||
<a name="binaryStringToBlob"></a> | ||
### binaryStringToBlob(binary, type) | ||
Convert a binary string to a <code>Blob</code>. Returns a Promise. | ||
**Parameters:** | ||
**Params** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| binary | `string` | binary string | | ||
| `Optional` type | `string` | the content type (optional) | | ||
- binary `string` | ||
- type `string` | `undefined` - the content type (optional) | ||
**Returns:** `Blob` | ||
Blob | ||
**Returns**: `Promise` - Promise that resolves with the <code>Blob</code> | ||
___ | ||
<a id="blobtoarraybuffer"></a> | ||
**Example**: | ||
### blobToArrayBuffer | ||
▸ **blobToArrayBuffer**(blob: *`Blob`*): `Promise`<`ArrayBuffer`> | ||
Convert a `Blob` to an `ArrayBuffer`. | ||
Example: | ||
```js | ||
blobUtil.binaryStringToBlob(binaryString).then(function (blob) { | ||
blobUtil.blobToArrayBuffer(blob).then(function (arrayBuff) { | ||
// success | ||
@@ -248,15 +286,22 @@ }).catch(function (err) { | ||
<a name="blobToBase64String"></a> | ||
### blobToBase64String(blob) | ||
Convert a <code>Blob</code> to a binary string. Returns a Promise. | ||
**Parameters:** | ||
**Params** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| blob | `Blob` | - | | ||
- blob `Blob` | ||
**Returns:** `Promise`<`ArrayBuffer`> | ||
Promise that resolves with the <code>ArrayBuffer</code> | ||
**Returns**: `Promise` - Promise that resolves with the binary string | ||
___ | ||
<a id="blobtobase64string"></a> | ||
### blobToBase64String | ||
**Example**: | ||
▸ **blobToBase64String**(blob: *`Blob`*): `Promise`<`string`> | ||
Convert a `Blob` to a binary string. | ||
Example: | ||
```js | ||
@@ -270,18 +315,24 @@ blobUtil.blobToBase64String(blob).then(function (base64String) { | ||
<a name="dataURLToBlob"></a> | ||
### dataURLToBlob(dataURL) | ||
Convert a data URL string | ||
(e.g. <code>'data:image/png;base64,iVBORw0KG...'</code>) | ||
to a <code>Blob</code>. Returns a Promise. | ||
**Parameters:** | ||
**Params** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| blob | `Blob` | - | | ||
- dataURL `string` | ||
**Returns:** `Promise`<`string`> | ||
Promise that resolves with the binary string | ||
**Returns**: `Promise` - Promise that resolves with the <code>Blob</code> | ||
___ | ||
<a id="blobtobinarystring"></a> | ||
**Example**: | ||
### blobToBinaryString | ||
▸ **blobToBinaryString**(blob: *`Blob`*): `Promise`<`string`> | ||
Convert a `Blob` to a binary string. | ||
Example: | ||
```js | ||
blobUtil.dataURLToBlob(dataURL).then(function (blob) { | ||
blobUtil.blobToBinaryString(blob).then(function (binaryString) { | ||
// success | ||
@@ -292,46 +343,49 @@ }).catch(function (err) { | ||
``` | ||
<a name="blobToDataURL"></a> | ||
###blobToDataURL(blob) | ||
Convert a <code>Blob</code> to a data URL string | ||
(e.g. <code>'data:image/png;base64,iVBORw0KG...'</code>). | ||
Returns a Promise. | ||
**Params** | ||
**Parameters:** | ||
- blob `Blob` | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| blob | `Blob` | - | | ||
**Returns**: `Promise` - Promise that resolves with the data URL string | ||
**Returns:** `Promise`<`string`> | ||
Promise that resolves with the binary string | ||
**Example**: | ||
___ | ||
<a id="blobtodataurl"></a> | ||
### blobToDataURL | ||
▸ **blobToDataURL**(blob: *`Blob`*): `Promise`<`string`> | ||
Convert a `Blob` to a data URL string (e.g. `'data:image/png;base64,iVBORw0KG...'`). | ||
Example: | ||
```js | ||
blobUtil.blobToDataURL(blob).then(function (dataURL) { | ||
// success | ||
}).catch(function (err) { | ||
// error | ||
}); | ||
var dataURL = blobUtil.blobToDataURL(blob); | ||
``` | ||
<a name="imgSrcToDataURL"></a> | ||
### imgSrcToDataURL(src, type, crossOrigin, quality) | ||
Convert an image's <code>src</code> URL to a data URL by loading the image and painting | ||
it to a <code>canvas</code>. Returns a Promise. | ||
<p/>Note: this will coerce the image to the desired content type, and it | ||
will only paint the first frame of an animated GIF. | ||
**Parameters:** | ||
**Params** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| blob | `Blob` | - | | ||
- src `string` | ||
- type `string` | `undefined` - the content type (optional, defaults to 'image/png') | ||
- crossOrigin `string` | `undefined` - for CORS-enabled images, set this to | ||
'Anonymous' to avoid "tainted canvas" errors | ||
- quality `number` | `undefined` - a number between 0 and 1 indicating image quality | ||
if the requested type is 'image/jpeg' or 'image/webp' | ||
**Returns:** `Promise`<`string`> | ||
Promise that resolves with the data URL string | ||
**Returns**: `Promise` - Promise that resolves with the data URL string | ||
___ | ||
<a id="canvastoblob"></a> | ||
**Examples**: | ||
### canvasToBlob | ||
▸ **canvasToBlob**(canvas: *`HTMLCanvasElement`*, type?: *`string`*, quality?: *`number`*): `Promise`<`Blob`> | ||
Convert a `canvas` to a `Blob`. | ||
Examples: | ||
```js | ||
blobUtil.imgSrcToDataURL('http://mysite.com/img.png').then(function (dataURL) { | ||
blobUtil.canvasToBlob(canvas).then(function (blob) { | ||
// success | ||
@@ -343,5 +397,6 @@ }).catch(function (err) { | ||
Most browsers support converting a canvas to both `'image/png'` and `'image/jpeg'`. You may also want to try `'image/webp'`, which will work in some browsers like Chrome (and in other browsers, will just fall back to `'image/png'`): | ||
```js | ||
blobUtil.imgSrcToDataURL('http://some-other-site.com/img.jpg', 'image/jpeg', | ||
'Anonymous', 1.0).then(function (dataURL) { | ||
blobUtil.canvasToBlob(canvas, 'image/webp').then(function (blob) { | ||
// success | ||
@@ -353,59 +408,100 @@ }).catch(function (err) { | ||
<a name="canvasToBlob"></a> | ||
### canvasToBlob(canvas, type, quality) | ||
Convert a <code>canvas</code> to a <code>Blob</code>. Returns a Promise. | ||
**Parameters:** | ||
**Params** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| canvas | `HTMLCanvasElement` | HTMLCanvasElement | | ||
| `Optional` type | `string` | the content type (optional, defaults to 'image/png') | | ||
| `Optional` quality | `number` | a number between 0 and 1 indicating image quality if the requested type is 'image/jpeg' or 'image/webp' | | ||
- canvas `string` | ||
- type `string` | `undefined` - the content type (optional, defaults to 'image/png') | ||
- quality `number` | `undefined` - a number between 0 and 1 indicating image quality | ||
if the requested type is 'image/jpeg' or 'image/webp' | ||
**Returns:** `Promise`<`Blob`> | ||
Promise that resolves with the <code>Blob</code> | ||
**Returns**: `Promise` - Promise that resolves with the <code>Blob</code> | ||
___ | ||
<a id="createblob"></a> | ||
### createBlob | ||
**Examples**: | ||
▸ **createBlob**(parts: *`Array`<`any`>*, properties?: * `BlobPropertyBag` | `string`*): `Blob` | ||
Shim for [`new Blob()`](https://developer.mozilla.org/en-US/docs/Web/API/Blob.Blob ) to support [older browsers that use the deprecated `BlobBuilder` API](http://caniuse.com/blob ). | ||
Example: | ||
```js | ||
blobUtil.canvasToBlob(canvas).then(function (blob) { | ||
// success | ||
}).catch(function (err) { | ||
// error | ||
}); | ||
var myBlob = blobUtil.createBlob(['hello world'], {type: 'text/plain'}); | ||
``` | ||
Most browsers support converting a canvas to both `'image/png'` and `'image/jpeg'`. You may | ||
also want to try `'image/webp'`, which will work in some browsers like Chrome (and in other browsers, will just fall back to `'image/png'`): | ||
**Parameters:** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| parts | `Array`<`any`> | content of the <code>Blob</code> | | ||
| `Optional` properties | `BlobPropertyBag` | `string`| usually <code>{type: myContentType}</code>, you can also pass a string for the content type | | ||
**Returns:** `Blob` | ||
Blob | ||
___ | ||
<a id="createobjecturl"></a> | ||
### createObjectURL | ||
▸ **createObjectURL**(blob: *`Blob`*): `string` | ||
Shim for [`URL.createObjectURL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL ) to support browsers that only have the prefixed `webkitURL` (e.g. Android <4.4). | ||
Example: | ||
```js | ||
blobUtil.canvasToBlob(canvas, 'image/webp').then(function (blob) { | ||
// success | ||
}).catch(function (err) { | ||
// error | ||
}); | ||
var myUrl = blobUtil.createObjectURL(blob); | ||
``` | ||
<a name="imgSrcToBlob"></a> | ||
### imgSrcToBlob(src, type, crossOrigin, quality) | ||
Convert an image's <code>src</code> URL to a <code>Blob</code> by loading the image and painting | ||
it to a <code>canvas</code>. Returns a Promise. | ||
**Parameters:** | ||
<p/>Note: this will coerce the image to the desired content type, and it | ||
will only paint the first frame of an animated GIF. | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| blob | `Blob` | - | | ||
**Params** | ||
**Returns:** `string` | ||
url | ||
- src `string` | ||
- type `string` | `undefined` - the content type (optional, defaults to 'image/png') | ||
- crossOrigin `string` | `undefined` - for CORS-enabled images, set this to | ||
'Anonymous' to avoid "tainted canvas" errors | ||
- quality `number` | `undefined` - a number between 0 and 1 indicating image quality | ||
if the requested type is 'image/jpeg' or 'image/webp' | ||
___ | ||
<a id="dataurltoblob"></a> | ||
**Returns**: `Promise` - Promise that resolves with the <code>Blob</code> | ||
### dataURLToBlob | ||
**Examples**: | ||
▸ **dataURLToBlob**(dataURL: *`string`*): `Blob` | ||
Convert a data URL string (e.g. `'data:image/png;base64,iVBORw0KG...'`) to a `Blob`. | ||
Example: | ||
```js | ||
var blob = blobUtil.dataURLToBlob(dataURL); | ||
``` | ||
**Parameters:** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| dataURL | `string` | dataURL-encoded string | | ||
**Returns:** `Blob` | ||
Blob | ||
___ | ||
<a id="imgsrctoblob"></a> | ||
### imgSrcToBlob | ||
▸ **imgSrcToBlob**(src: *`string`*, type?: *`string`*, crossOrigin?: *`string`*, quality?: *`number`*): `Promise`<`Blob`> | ||
Convert an image's `src` URL to a `Blob` by loading the image and painting it to a `canvas`. | ||
Note: this will coerce the image to the desired content type, and it will only paint the first frame of an animated GIF. | ||
Examples: | ||
```js | ||
blobUtil.imgSrcToBlob('http://mysite.com/img.png').then(function (blob) { | ||
@@ -417,5 +513,4 @@ // success | ||
``` | ||
```js | ||
blobUtil.imgSrcToBlob('http://some-other-site.com/img.jpg', 'image/jpeg', | ||
blobUtil.imgSrcToBlob('http://some-other-site.com/img.jpg', 'image/jpeg', | ||
'Anonymous', 1.0).then(function (blob) { | ||
@@ -428,17 +523,29 @@ // success | ||
<a name="arrayBufferToBlob"></a> | ||
### arrayBufferToBlob(buffer, type) | ||
Convert an <code>ArrayBuffer</code> to a <code>Blob</code>. Returns a Promise. | ||
**Parameters:** | ||
**Params** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| src | `string` | image src | | ||
| `Optional` type | `string` | the content type (optional, defaults to 'image/png') | | ||
| `Optional` crossOrigin | `string` | for CORS-enabled images, set this to 'Anonymous' to avoid "tainted canvas" errors | | ||
| `Optional` quality | `number` | a number between 0 and 1 indicating image quality if the requested type is 'image/jpeg' or 'image/webp' | | ||
- buffer `ArrayBuffer` | ||
- type `string` | `undefined` - the content type (optional) | ||
**Returns:** `Promise`<`Blob`> | ||
Promise that resolves with the <code>Blob</code> | ||
**Returns**: `Promise` - Promise that resolves with the <code>Blob</code> | ||
___ | ||
<a id="imgsrctodataurl"></a> | ||
**Example**: | ||
### imgSrcToDataURL | ||
▸ **imgSrcToDataURL**(src: *`string`*, type?: *`string`*, crossOrigin?: *`string`*, quality?: *`number`*): `Promise`<`string`> | ||
Convert an image's `src` URL to a data URL by loading the image and painting it to a `canvas`. | ||
Note: this will coerce the image to the desired content type, and it will only paint the first frame of an animated GIF. | ||
Examples: | ||
```js | ||
blobUtil.arrayBufferToBlob(arrayBuff, 'audio/mpeg').then(function (blob) { | ||
blobUtil.imgSrcToDataURL('http://mysite.com/img.png').then(function (dataURL) { | ||
// success | ||
@@ -449,48 +556,62 @@ }).catch(function (err) { | ||
``` | ||
```js | ||
blobUtil.imgSrcToDataURL('http://some-other-site.com/img.jpg', 'image/jpeg', | ||
'Anonymous', 1.0).then(function (dataURL) { | ||
// success | ||
}).catch(function (err) { | ||
// error | ||
}); | ||
``` | ||
<a name="blobToArrayBuffer"></a> | ||
### blobToArrayBuffer(blob) | ||
Convert a <code>Blob</code> to an <code>ArrayBuffer</code>. Returns a Promise. | ||
**Parameters:** | ||
**Params** | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| src | `string` | image src | | ||
| `Optional` type | `string` | the content type (optional, defaults to 'image/png') | | ||
| `Optional` crossOrigin | `string` | for CORS-enabled images, set this to 'Anonymous' to avoid "tainted canvas" errors | | ||
| `Optional` quality | `number` | a number between 0 and 1 indicating image quality if the requested type is 'image/jpeg' or 'image/webp' | | ||
- blob `Blob` | ||
**Returns:** `Promise`<`string`> | ||
Promise that resolves with the data URL string | ||
**Returns**: `Promise` - Promise that resolves with the <code>ArrayBuffer</code> | ||
___ | ||
<a id="revokeobjecturl"></a> | ||
**Example**: | ||
### revokeObjectURL | ||
▸ **revokeObjectURL**(url: *`string`*): `void` | ||
Shim for [`URL.revokeObjectURL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL.revokeObjectURL ) to support browsers that only have the prefixed `webkitURL` (e.g. Android <4.4). | ||
Example: | ||
```js | ||
blobUtil.blobToArrayBuffer(blob).then(function (arrayBuff) { | ||
// success | ||
}).catch(function (err) { | ||
// error | ||
}); | ||
blobUtil.revokeObjectURL(myUrl); | ||
``` | ||
Credits | ||
---- | ||
**Parameters:** | ||
Thanks to [webmodules/blob](https://github.com/webmodules/blob) for the Blob constructor shim, and to the rest of [the PouchDB team](https://github.com/pouchdb/pouchdb/graphs/contributors) for figuring most of this crazy stuff out. | ||
| Param | Type | Description | | ||
| ------ | ------ | ------ | | ||
| url | `string` | | | ||
Building the library | ||
---- | ||
**Returns:** `void` | ||
npm install | ||
npm run build | ||
___ | ||
To generate documentation in `doc/`: | ||
npm run jsdoc | ||
or in markdown format as `api.md` | ||
<!-- end insert API --> | ||
npm run jsdoc2md | ||
Credits | ||
---- | ||
The playground is just `jsdoc` with some extra text containing Kirby and the code samples and such. | ||
Thanks to the rest of [the PouchDB team](https://github.com/pouchdb/pouchdb/graphs/contributors) for figuring most of this crazy stuff out. | ||
So unfortunately you will need to do a manual diff to get the docs up to date. You'll need to diff: | ||
Building the library | ||
---- | ||
* `README.md` to its previous version (make sure to keep the code samples, which were manually added) | ||
* `docs` to its previous version | ||
npm install | ||
npm run build | ||
@@ -509,1 +630,5 @@ Testing the library | ||
npm run test-local | ||
To build the API docs and insert them in the README: | ||
npm run doc |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
0
7
1179
1
623
0
62699
22
1
- Removedblob@0.0.4
- Removednative-or-lie@1.0.2
- Removedblob@0.0.4(transitive)
- Removedimmediate@3.0.6(transitive)
- Removedlie@3.3.0(transitive)
- Removednative-or-lie@1.0.2(transitive)