Skip to content

Commit

Permalink
Support node blob urls
Browse files Browse the repository at this point in the history
  • Loading branch information
raub committed Oct 17, 2023
1 parent e9e210f commit 6b7fd05
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Load images from:
* Local file.
* Data URI.
* Http(s) URL.
* Node.js Blob URL.
* Raw RGBA pixel data


Expand All @@ -40,7 +41,18 @@ Additional features:
const Image = require('image-raub');
```

See [TypeSctipt defenitions](/index.d.ts) for more details.
See [TypeScript defenitions](/index.d.ts) for more details.


### Set window icon

Compatible with [glfw-raub](https://github.com/node-3d/glfw-raub) `window.icon` property.

```js
const icon = new Image();
icon.src = __dirname + '/icons/logo.png';
icon.on('load', () => { window.icon = icon; });
```


### Load an OpenGL texture
Expand Down
17 changes: 14 additions & 3 deletions js/image.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const { inspect, inherits } = require('util');
const Emitter = require('events');

const { resolveObjectURL } = require('node:buffer');
const { inspect, inherits } = require('node:util');
const Emitter = require('node:events');
const { download } = require('addon-tools-raub');

const { Image } = require('../core');
Expand Down Expand Up @@ -99,6 +99,17 @@ class JsImage extends Image {
return;
}

// Object URL
if (/^blob:nodedata:/.test(this._src)) {
const blob = resolveObjectURL(this._src);
(async () => {
const arrayBuffer = await blob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
this._load(buffer);
})();
return;
}

// Data URI
if (/^data:/.test(this._src)) {
this._isDataUri = true;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Luis Blanco <luisblanco1337@gmail.com>",
"name": "image-raub",
"version": "4.1.3",
"version": "4.2.0",
"description": "Native Image loader for Node.js",
"license": "MIT",
"main": "index.js",
Expand Down

0 comments on commit 6b7fd05

Please sign in to comment.