{"_id":"dom-to-image-improved","_rev":"1-f4d3069a9f521ebdd6c351f25cefad35","name":"dom-to-image-improved","dist-tags":{"latest":"2.8.0"},"versions":{"2.8.0":{"name":"dom-to-image-improved","version":"2.8.0","description":"Generates an image from a DOM node using HTML5 canvas and SVG","main":"src/dom-to-image-improved.js","dependencies":{},"devDependencies":{"chai":"^4.2.0","grunt":"^1.0.4","grunt-cli":"^1.3.2","grunt-contrib-jshint":"^2.1.0","grunt-contrib-uglify":"^4.0.1","grunt-contrib-watch":"^1.1.0","grunt-karma":"^3.0.2","js-yaml":"^3.13.1","karma":"^4.1.0","karma-chai":"^0.1.0","karma-chrome-launcher":"^2.2.0","karma-firefox-launcher":"^1.1.0","karma-mocha":"^1.3.0","karma-mocha-reporter":"^2.2.5","mocha":"^6.1.4"},"scripts":{"test":"grunt test"},"repository":{"type":"git","url":"git+https://github.com/mcklayin/dom-to-image-improved.git"},"keywords":["dom","image","raster","render","html","canvas","svg"],"author":{"name":"Dmytro Pidhaiko","email":"mcklayin@gmail.com"},"contributors":[{"name":"Anatolii Saienko","email":"anatoly.sayenko@gmail.com"},{"name":"Marc Brooks","email":"idisposable@gmail.com"},{"name":"Daniel Fischer","email":"daniel.fischer@iqdoq.de"},{"name":"Aidas Klimas"},{"name":"Edgardo Di Gesto"},{"name":"樊冬 Fan Dong","email":"CG-man@outlook.com"}],"license":"MIT","bugs":{"url":"https://github.com/mcklayin/dom-to-image-improved/issues"},"homepage":"https://github.com/mcklayin/dom-to-image-improved#readme","gitHead":"3685138281dc2151ac1ef8dcc68a28940b2c2b0a","_id":"dom-to-image-improved@2.8.0","_npmVersion":"6.4.1","_nodeVersion":"10.15.3","_npmUser":{"name":"mcklayin","email":"mcklayin@gmail.com"},"dist":{"integrity":"sha512-O0Fdg4cwqebRqezTAXrQbESqn7hPvKhfF2SFM49cXjYsneOyu3qHgn1LF66W71m8qIUElCpe5JGhS/CQAjgAgg==","shasum":"cbe3981b96d495a0d9a929d366a50d1f3a7ddd24","tarball":"https://registry.npmjs.org/dom-to-image-improved/-/dom-to-image-improved-2.8.0.tgz","fileCount":324,"unpackedSize":4104988,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdptfvCRA9TVsSAnZWagAAlaoP/j04G2RtRJxzcsZ0oQ5J\nZeObCcZMCLlyMMJCgRAeoTbeyTzVrjebA75UESsGW4eVlH+B6nAqz0WmFc/E\nT7lx66T/QXjwE8O8WtawUxW1HU0eE2pjx0WOaY6Zbjcy/998Lb9pxGwJG7+F\nfA+wKkUEKGWCpYvJCexrhOVMolOdxcXsN6HoRfRsxU9mIt5hM+lZ336cXGSg\nLmoRqOIfX8FJqyHCCSp3ZoFzZCB1dcrc1u2v3e5FECOtzbhmvioZnV2NCaLy\n2XPeQMZZ3coDDYsSppnyD1Rua9ZT5x+JLT/ZEd2txZdjLJLpj5+xQYvIR3/T\nYUWo03MTeyM2DnjyPSWXjvqRhRTP6SyN78NVyslaS4saEnJAsJnSZTf2PtNh\nVkBkKWs3PyVwtOF48sVC2iP41giWARKSUw1krBBzvYeXzk94qEucqPMqeQwq\nJhO1ijXa+AfLpFC23Lb3os3Ov/8ndHQ1n8JXMg8/n5NljrHvUkaUO+5w0x26\nNi7E33SPmREKoMYfq0nOfIhaY/ya9YV50808XrIw66iAOFpdPfB2Zz2BOu0O\nmTmmRmuD2ZmzRYr/S3yN9FmJy9IgL1Xof2860JMUSJoMuv3MKQexqpkhwSX6\na8FRqSplWEHChQg4pG86qhuf32qlr4wTDN7Odheq9lliPEM6XTubwuidVqyG\n7MFi\r\n=PW/E\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCQhD2Svk3rNI5aOUWJsl1J8f/A8S9h4g6YOZqP94XvHQIgN+hK7W1QtU1Hx5QAvgy2/3X9eHizLb790lIHniZzaZ8="}]},"maintainers":[{"name":"mcklayin","email":"mcklayin@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/dom-to-image-improved_2.8.0_1571215342431_0.1554129088481777"},"_hasShrinkwrap":false}},"time":{"created":"2019-10-16T08:42:22.429Z","2.8.0":"2019-10-16T08:42:22.655Z","modified":"2022-04-29T02:27:37.496Z"},"maintainers":[{"name":"mcklayin","email":"mcklayin@gmail.com"}],"description":"Generates an image from a DOM node using HTML5 canvas and SVG","homepage":"https://github.com/mcklayin/dom-to-image-improved#readme","keywords":["dom","image","raster","render","html","canvas","svg"],"repository":{"type":"git","url":"git+https://github.com/mcklayin/dom-to-image-improved.git"},"contributors":[{"name":"Anatolii Saienko","email":"anatoly.sayenko@gmail.com"},{"name":"Marc Brooks","email":"idisposable@gmail.com"},{"name":"Daniel Fischer","email":"daniel.fischer@iqdoq.de"},{"name":"Aidas Klimas"},{"name":"Edgardo Di Gesto"},{"name":"樊冬 Fan Dong","email":"CG-man@outlook.com"}],"author":{"name":"Dmytro Pidhaiko","email":"mcklayin@gmail.com"},"bugs":{"url":"https://github.com/mcklayin/dom-to-image-improved/issues"},"license":"MIT","readme":"# DOM to Image\n\n[![Build Status](https://travis-ci.org/1904labs/dom-to-image-improved.svg?branch=master)](https://travis-ci.org/1904labs/dom-to-image-improved)\n\n## What is it\n\n**dom-to-image-improved** is a library which can turn arbitrary DOM node into\na vector (SVG) or raster (PNG or JPEG) image, written in JavaScript.\n\nThis fork of [dom-to-image-improved by Marc Brooks (1904labs)](https://github.com/1904labs/dom-to-image-improved)\nwith some important fixes merged. We are eternally grateful for his starting point.\n\nBased on [dom-to-image by Anatolii Saienko (tsayen)](https://github.com/tsayen/dom-to-image)\nwith some important fixes merged. We are eternally grateful for his starting point.\n\nAnatolii's version was based on [domvas by Paul Bakaus](https://github.com/pbakaus/domvas)\nand has been completely rewritten, with some bugs fixed and some new\nfeatures (like web font and image support) added.\n\n## Installation\n\n### NPM\n\n`npm install dom-to-image-improved`\n\nThen load\n\n```javascript\n/* in ES 6 */\nimport domtoimage from 'dom-to-image-improved';\n/* in ES 5 */\nvar domtoimage = require('dom-to-image-improved');\n```\n\n### Bower\n\n`bower install dom-to-image-improved`\n\nInclude either `src/dom-to-image-improved.js` or `dist/dom-to-image-improved.min.js` in your page\nand it will make the `domtoimage` variable available in the global scope.\n\n```html\n<script src=\"path/to/dom-to-image-improved.min.js\" />\n<script>\n  domtoimage.toPng(node)\n  //...\n</script>\n```\n\n## Usage\n\nAll the top level functions accept DOM node and rendering options,\nand return promises, which are fulfilled with corresponding data URLs.  \nGet a PNG image base64-encoded data URL and display right away:\n\n```javascript\nvar node = document.getElementById('my-node');\n\ndomtoimage.toPng(node)\n    .then(function (dataUrl) {\n        var img = new Image();\n        img.src = dataUrl;\n        document.body.appendChild(img);\n    })\n    .catch(function (error) {\n        console.error('oops, something went wrong!', error);\n    });\n```\n\nGet a PNG image blob and download it (using [FileSaver](https://github.com/eligrey/FileSaver.js/),\nfor example):\n\n```javascript\ndomtoimage.toBlob(document.getElementById('my-node'))\n    .then(function (blob) {\n        window.saveAs(blob, 'my-node.png');\n    });\n```\n\nSave and download a compressed JPEG image:\n\n```javascript\ndomtoimage.toJpeg(document.getElementById('my-node'), { quality: 0.95 })\n    .then(function (dataUrl) {\n        var link = document.createElement('a');\n        link.download = 'my-image-name.jpeg';\n        link.href = dataUrl;\n        link.click();\n    });\n```\n\nGet an SVG data URL, but filter out all the `<i>` elements:\n\n```javascript\nfunction filter (node) {\n    return (node.tagName !== 'i');\n}\n\ndomtoimage.toSvg(document.getElementById('my-node'), {filter: filter})\n    .then(function (dataUrl) {\n        /* do something */\n    });\n```\n\nGet the raw pixel data as a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)\nwith every 4 array elements representing the RGBA data of a pixel:\n\n```javascript\nvar node = document.getElementById('my-node');\n\ndomtoimage.toPixelData(node)\n    .then(function (pixels) {\n        for (var y = 0; y < node.scrollHeight; ++y) {\n          for (var x = 0; x < node.scrollWidth; ++x) {\n            pixelAtXYOffset = (4 * y * node.scrollHeight) + (4 * x);\n            /* pixelAtXY is a Uint8Array[4] containing RGBA values of the pixel at (x, y) in the range 0..255 */\n            pixelAtXY = pixels.slice(pixelAtXYOffset, pixelAtXYOffset + 4);\n          }\n        }\n    });\n```\n\nGet a canvas object:\n\n```javascript\ndomtoimage.toCanvas(document.getElementById('my-node'))\n    .then(function (canvas) {\n        console.log('canvas', canvas.width, canvas.height);\n    });\n```\n\n* * *\n\n_All the functions under `impl` are not public API and are exposed only\nfor unit testing._\n\n* * *\n\n### Rendering options\n\n#### filter\n\nA function taking DOM node as argument. Should return true if passed node\nshould be included in the output (excluding node means excluding it's\nchildren as well). Not called on the root node.\n\n#### bgcolor\n\nA string value for the background color, any valid CSS color value.\n\n#### scale\n\nA number for image scaling\n\n#### canvas\n\nCanvas drawImage options: width, height, sx, sy, sw, sh, dx, dy, dw, dh.\nWidth & height influence on image result.\nCan be used to change image size or crop.\n\n#### height, width\n\nHeight and width in pixels to be applied to node before rendering.\n\n#### style\n\nAn object whose properties to be copied to node's style before rendering.\nYou might want to check [this reference](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Properties_Reference)\nfor JavaScript names of CSS properties.\n\n#### quality\n\nA number between 0 and 1 indicating image quality (e.g. 0.92 => 92%) of the\nJPEG image. Defaults to 1.0 (100%)\n\n#### cacheBust\n\nSet to true to append the current time as a query string to URL requests to enable cache busting. Defaults to false\n\n#### imagePlaceholder\n\nA data URL for a placeholder image that will be used when fetching an image fails. Defaults to undefined and will throw an error on failed images\n\n## Browsers\n\nIt's tested on latest Chrome and Firefox (49 and 45 respectively at the time\nof writing), with Chrome performing  significantly better on big DOM trees,\npossibly due to it's more performant SVG support, and the fact that it supports\n `CSSStyleDeclaration.cssText` property.  \n\n_Internet Explorer is not (and will not be) supported, as it does not support\nSVG `<foreignObject>` tag_  \n\n_Safari [is not supported](https://github.com/tsayen/dom-to-image/issues/27), as it uses a stricter security model on `<foreignObject`> tag. Suggested workaround is to use `toSvg` and render on the server._`\n\n## Dependencies\n\n### Source\n\nOnly standard lib is currently used, but make sure your browser supports:  \n\n- [Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)  \n- SVG `<foreignObject>` tag\n\n### Tests\n\nMost importantly, tests depend on:\n\n- [js-imagediff](https://github.com/HumbleSoftware/js-imagediff),\n    to compare rendered and control images\n\n- [ocrad.js](https://github.com/antimatter15/ocrad.js), for the\n    parts when you can't compare images (due to the browser\n    rendering differences) and just have to test whether the text is rendered\n\n## How it works\n\nThere might some day exist (or maybe already exists?) a simple and standard\nway of exporting parts of the HTML to image (and then this script can only\nserve as an evidence of all the hoops I had to jump through in order to get\nsuch obvious thing done) but I haven't found one so far.  \n\nThis library uses a feature of SVG that allows having arbitrary HTML content\ninside of the `<foreignObject>` tag. So, in order to render that DOM node\nfor you, following steps are taken:  \n\n1. Clone the original DOM node recursively\n\n1. Compute the style for the node and each sub-node and copy it to\n   corresponding clone\n\n    - and don't forget to recreate pseudo-elements, as they are not\n      cloned in any way, of course\n\n1. Embed web fonts\n\n    - find all the `@font-face` declarations that might represent web fonts\n\n    - parse file URLs, download corresponding files\n\n    - base64-encode and inline content as `data:` URLs\n\n    - concatenate all the processed CSS rules and put them into one `<style>`\n        element, then attach it to the clone\n\n1. Embed images\n\n    - embed image URLs in `<img>` elements\n\n    - inline images used in `background` CSS property, in a fashion similar to\n      fonts\n\n1. Serialize the cloned node to XML\n\n1. Wrap XML into the `<foreignObject>` tag, then into the SVG, then make it a\n   data URL\n\n1. Optionally, to get PNG content or raw pixel data as a Uint8Array, create an\n   Image element with the SVG as a source, and render it on an off-screen\n   canvas, that you have also created, then read the content from the canvas\n\n1. Done!  \n\n## Things to watch out for\n\n- if the DOM node you want to render includes a `<canvas>` element with\n    something drawn on it, it should be handled fine, unless the canvas is\n    [tainted](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image) -\n    in this case rendering will rather not succeed.  \n\n- at the time of writing, Firefox has a problem with some external stylesheets\n    (see issue #13). In such case, the error will be caught and logged.  \n\n## Authors\n\nDmytro Pidhaiko, Marc Brooks(dom-to-image-more), Anatolii Saienko (original dom-to-image), Paul Bakaus (original idea),\nAidas Klimas (fixes), Edgardo Di Gesto (fixes), 樊冬 Fan Dong (fixes)\n\n## License\n\nMIT\n","readmeFilename":"README.md"}