@d3plus/export 3.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/es/index.js +1 -0
- package/es/src/saveElement.js +41 -0
- package/package.json +40 -0
- package/umd/d3plus-export.full.js +1145 -0
- package/umd/d3plus-export.full.js.map +1 -0
- package/umd/d3plus-export.full.min.js +90 -0
- package/umd/d3plus-export.js +157 -0
- package/umd/d3plus-export.js.map +1 -0
- package/umd/d3plus-export.min.js +20 -0
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @d3plus/export
|
|
2
|
+
|
|
3
|
+
Export methods for transforming and downloading SVG.
|
|
4
|
+
|
|
5
|
+
## Installing
|
|
6
|
+
|
|
7
|
+
If using npm, `npm install @d3plus/export`. Otherwise, you can download the [latest release from GitHub](https://github.com/d3plus/d3plus/releases/latest) or load from a [CDN](https://cdn.jsdelivr.net/npm/@d3plus/export@3.0.0-alpha.0/+esm).
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import modules from "@d3plus/export";
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
In vanilla JavaScript, a `d3plus` global is exported from the pre-bundled version:
|
|
14
|
+
|
|
15
|
+
```html
|
|
16
|
+
<script src="https://cdn.jsdelivr.net/npm/@d3plus/export@3.0.0-alpha.0"></script>
|
|
17
|
+
<script>
|
|
18
|
+
console.log(d3plus);
|
|
19
|
+
</script>
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Examples
|
|
23
|
+
|
|
24
|
+
Live examples can be found on [d3plus.org](https://d3plus.org/), which includes a collection of example visualizations using @d3plus/react.
|
|
25
|
+
|
|
26
|
+
## API Reference
|
|
27
|
+
|
|
28
|
+
#####
|
|
29
|
+
* [saveElement](#saveElement) - Downloads an HTML Element as a bitmap PNG image.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
<a name="saveElement"></a>
|
|
34
|
+
#### d3plus.**saveElement**(elem, [options], [renderOptions]) [<>](https://github.com/d3plus/d3plus/blob/main/packages/export/src/saveElement.js#L9)
|
|
35
|
+
|
|
36
|
+
Downloads an HTML Element as a bitmap PNG image.
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
This is a global function
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
###### <sub>Documentation generated on Thu, 13 Mar 2025 19:58:29 GMT</sub>
|
package/es/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as saveElement } from "./src/saveElement.js";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { toBlob, toSvg } from "html-to-image";
|
|
2
|
+
import { saveAs } from "file-saver";
|
|
3
|
+
var defaultOptions = {
|
|
4
|
+
filename: "download",
|
|
5
|
+
type: "png"
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
@function saveElement
|
|
9
|
+
@desc Downloads an HTML Element as a bitmap PNG image.
|
|
10
|
+
@param {HTMLElement} elem A single element to be saved to one file.
|
|
11
|
+
@param {Object} [options] Additional options to specify.
|
|
12
|
+
@param {String} [options.filename = "download"] Filename for the downloaded file, without the extension.
|
|
13
|
+
@param {String} [options.type = "png"] File type of the saved document. Accepted values are `"png"` and `"jpg"`.
|
|
14
|
+
@param {Function} [options.callback] Function to be invoked after saving is complete.
|
|
15
|
+
@param {Object} [renderOptions] Custom options to be passed to the html-to-image function.
|
|
16
|
+
*/ export default function(elem) {
|
|
17
|
+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, renderOptions = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
18
|
+
if (!elem) return;
|
|
19
|
+
options = Object.assign({}, defaultOptions, options);
|
|
20
|
+
// rename renderOptions.background to backgroundColor for backwards compatibility
|
|
21
|
+
renderOptions = Object.assign({
|
|
22
|
+
backgroundColor: renderOptions.background
|
|
23
|
+
}, renderOptions);
|
|
24
|
+
function finish(blob) {
|
|
25
|
+
saveAs(blob, "".concat(options.filename, ".").concat(options.type));
|
|
26
|
+
if (options.callback) options.callback();
|
|
27
|
+
}
|
|
28
|
+
if (options.type === "svg") {
|
|
29
|
+
toSvg(elem, renderOptions).then(function(dataUrl) {
|
|
30
|
+
var xhr = new XMLHttpRequest();
|
|
31
|
+
xhr.open("GET", dataUrl);
|
|
32
|
+
xhr.responseType = "blob";
|
|
33
|
+
xhr.onload = function() {
|
|
34
|
+
return finish(xhr.response);
|
|
35
|
+
};
|
|
36
|
+
xhr.send();
|
|
37
|
+
});
|
|
38
|
+
} else {
|
|
39
|
+
toBlob(elem, renderOptions).then(finish);
|
|
40
|
+
}
|
|
41
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@d3plus/export",
|
|
3
|
+
"version": "3.0.0-alpha.0",
|
|
4
|
+
"description": "Export methods for transforming and downloading SVG.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": "./es/index.js",
|
|
8
|
+
"browser": "./umd/d3plus-export.full.js",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=18"
|
|
11
|
+
},
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"files": [
|
|
14
|
+
"umd",
|
|
15
|
+
"es"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://d3plus.org",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/d3plus/d3plus.git",
|
|
21
|
+
"directory": "packages/export"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"export",
|
|
25
|
+
"d3",
|
|
26
|
+
"d3plus",
|
|
27
|
+
"data",
|
|
28
|
+
"visualization"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build:esm": "node ../../scripts/build-esm.js",
|
|
32
|
+
"build:umd": "node ../../scripts/build-umd.js",
|
|
33
|
+
"dev": "node ../../scripts/dev.js",
|
|
34
|
+
"test": "eslint index.js src/**/*.js && eslint --global=it test && mocha 'test/**/*-test.js'"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"file-saver": "1.3.3",
|
|
38
|
+
"html-to-image": "^1.11.11"
|
|
39
|
+
}
|
|
40
|
+
}
|