canvas-to-buffer
Advanced tools
Comparing version 1.1.0 to 1.1.1
34
index.js
const toBuffer = require('typedarray-to-buffer') | ||
const atob = require('atob') | ||
const isBrowser = typeof document !== 'undefined' && typeof document.createElement === 'function' | ||
const isBrowser = | ||
typeof document !== 'undefined' && typeof document.createElement === 'function' | ||
@@ -29,3 +30,3 @@ // cached, used only once for browser environments | ||
function composeImageType (index) { | ||
function composeImageType(index) { | ||
var imageType | ||
@@ -40,3 +41,3 @@ | ||
function isMatch (uri, imageType) { | ||
function isMatch(uri, imageType) { | ||
const match = uri && uri.match(imageType) | ||
@@ -50,3 +51,3 @@ | ||
// Performance tweak, we do not need a big canvas for finding out the supported image type | ||
function getTestCanvas () { | ||
function getTestCanvas() { | ||
var testCanvas | ||
@@ -64,3 +65,3 @@ | ||
function canvasSupportsImageTypeAsync (imageType, cb) { | ||
function canvasSupportsImageTypeAsync(imageType, cb) { | ||
try { | ||
@@ -79,3 +80,3 @@ getTestCanvas().toDataURL(imageType, function (err, uri) { | ||
function canvasSupportsImageTypeSync (imageType) { | ||
function canvasSupportsImageTypeSync(imageType) { | ||
var match | ||
@@ -91,3 +92,6 @@ | ||
options.debug && | ||
options.logger.debug('Failed to call toDataURL() on canvas for image type %s', imageType) | ||
options.logger.debug( | ||
'Failed to call toDataURL() on canvas for image type %s', | ||
imageType | ||
) | ||
} | ||
@@ -98,3 +102,3 @@ | ||
function verifyImageTypeAsync (imageType, cb) { | ||
function verifyImageTypeAsync(imageType, cb) { | ||
canvasSupportsImageTypeAsync(imageType, function (err, match) { | ||
@@ -121,3 +125,3 @@ if (err) { | ||
function verifyImageTypeSync (imageType) { | ||
function verifyImageTypeSync(imageType) { | ||
if (!canvasSupportsImageTypeSync(imageType)) { | ||
@@ -141,3 +145,3 @@ if (options.image.types[1]) { | ||
// callbacks are needed for server side tests | ||
function verifyImageType (cb) { | ||
function verifyImageType(cb) { | ||
const imageType = composeImageType(0) | ||
@@ -154,3 +158,3 @@ | ||
// http://jsperf.com/data-uri-to-buffer-performance/3 | ||
function uriToBuffer (uri) { | ||
function uriToBuffer(uri) { | ||
const uriSplitted = uri.split(',')[1] | ||
@@ -160,5 +164,5 @@ var bytes | ||
// Beware that the atob function might be a static one for server side tests | ||
if (typeof (atob) === 'function') { | ||
if (typeof atob === 'function') { | ||
bytes = atob(uriSplitted) | ||
} else if (typeof (self.constructor.atob) === 'function') { | ||
} else if (typeof self.constructor.atob === 'function') { | ||
bytes = self.constructor.atob(uriSplitted) | ||
@@ -179,3 +183,3 @@ } else { | ||
function toBufferSync () { | ||
function toBufferSync() { | ||
const imageType = self.getImageType() | ||
@@ -192,3 +196,3 @@ var buffer | ||
function toBufferAsync (cb) { | ||
function toBufferAsync(cb) { | ||
self.getImageType(function (err, imageType) { | ||
@@ -195,0 +199,0 @@ if (err) { |
{ | ||
"name": "canvas-to-buffer", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Converts a Canvas graphic into a Buffer, as fast as possible and without a copy.", | ||
@@ -32,2 +32,7 @@ "source": "index.js", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=8.10.0", | ||
"yarn": ">=1.3.0", | ||
"npm": ">=5.4.0" | ||
}, | ||
"dependencies": { | ||
@@ -34,0 +39,0 @@ "atob": "2.1.2", |
@@ -1,3 +0,2 @@ | ||
canvas-to-buffer | ||
================ | ||
# canvas-to-buffer | ||
@@ -8,11 +7,12 @@ [![Build Status](https://travis-ci.org/binarykitchen/canvas-to-buffer.svg?branch=master)](https://travis-ci.org/binarykitchen/canvas-to-buffer) | ||
Following performance tests prove that this module is using the fatest known method: | ||
Following performance tests prove that this module is using the fastest known method: | ||
[Data URI to Buffer performance tests](http://jsperf.com/data-uri-to-buffer-performance/3) | ||
Useful scenarios for this module: | ||
* Pipe each canvas from the browser to the server through web sockets at light speed | ||
* Compress a canvas into binary form for persistence | ||
* If the [Canvas package](https://www.npmjs.com/package/canvas) is too heavy for you and you only need the `toBuffer` part and/or you care about speed | ||
* Have one package that works on both sides, browser and server. In other words: you can browserify it. | ||
- Pipe each canvas from the browser to the server through web sockets at light speed | ||
- Compress a canvas into binary form for persistence | ||
- If the [Canvas package](https://www.npmjs.com/package/canvas) is too heavy for you and you only need the `toBuffer` part and/or you care about speed | ||
- Have one package that works on both sides, browser and server. In other words: you can browserify it. | ||
Furthermore this module is an important part of the [Videomail Client](https://github.com/binarykitchen/videomail-client) whose implementation can be seen on [Videomail](https://www.videomail.io) | ||
@@ -24,12 +24,12 @@ | ||
// I call it a Frame but you can go with i.E. CanvasConverter, whatever | ||
var Frame = require('canvas-to-buffer') | ||
var Frame = require("canvas-to-buffer"); | ||
// Drop in any canvas, i.E. from a webcam | ||
var frame = new Frame(canvas) | ||
var frame = new Frame(canvas); | ||
// Automatically detects image type and does the conversion | ||
var buffer = frame.toBuffer() | ||
var buffer = frame.toBuffer(); | ||
// Returns the chosen image type, could be `'image/png'` | ||
var imageType = frame.getImageType() | ||
var imageType = frame.getImageType(); | ||
``` | ||
@@ -40,9 +40,9 @@ | ||
```js | ||
var Frame = require('canvas-to-buffer') | ||
var frame = new Frame(canvas, { | ||
quality: 0.4, | ||
image: { | ||
types: ['webp', 'png'] | ||
} | ||
}) | ||
var Frame = require("canvas-to-buffer"); | ||
var frame = new Frame(canvas, { | ||
quality: 0.4, | ||
image: { | ||
types: ["webp", "png"], | ||
}, | ||
}); | ||
``` | ||
@@ -49,0 +49,0 @@ |
Sorry, the diff of this file is not supported yet
49470
14
384