d3-svg-to-png
Advanced tools
Comparing version 0.3.0 to 0.3.1
26
index.js
@@ -1,6 +0,6 @@ | ||
function inlineStyles (source, target) { | ||
function inlineStyles(source, target) { | ||
// inline style from source element to the target (detached) one | ||
const computed = window.getComputedStyle(source); | ||
for (const styleKey of computed) { | ||
target.style[styleKey] = computed[styleKey] | ||
target.style[styleKey] = computed[styleKey]; | ||
} | ||
@@ -10,7 +10,7 @@ | ||
for (let i = 0; i < source.children.length; i++) { | ||
inlineStyles(source.children[i], target.children[i]) | ||
inlineStyles(source.children[i], target.children[i]); | ||
} | ||
} | ||
function copyToCanvas ({ source, target, scale, format, quality }) { | ||
function copyToCanvas({ source, target, scale, format, quality }) { | ||
let svgData = new XMLSerializer().serializeToString(target); | ||
@@ -38,3 +38,5 @@ let canvas = document.createElement('canvas'); | ||
ctxt.drawImage(img, 0, 0); | ||
resolve(canvas.toDataURL(`image/${format === 'jpg' ? 'jpeg' : format}`, quality)); | ||
resolve( | ||
canvas.toDataURL(`image/${format === 'jpg' ? 'jpeg' : format}`, quality) | ||
); | ||
}; | ||
@@ -44,3 +46,3 @@ }); | ||
function downloadImage ({ file, name, format }) { | ||
function downloadImage({ file, name, format }) { | ||
let a = document.createElement('a'); | ||
@@ -68,7 +70,7 @@ a.download = `${name}.${format}`; | ||
// Accept a selector or directly a DOM Element | ||
source = (source instanceof Element) ? source : document.querySelector(source); | ||
source = source instanceof Element ? source : document.querySelector(source); | ||
// Create a new SVG element similar to the source one to avoid modifying the | ||
// source element. | ||
const target = document.createElementNS('http://www.w3.org/2000/svg', 'svg') | ||
const target = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); | ||
target.innerHTML = source.innerHTML; | ||
@@ -86,3 +88,3 @@ for (const attr of source.attributes) { | ||
if (background) { | ||
target.style.background = background | ||
target.style.background = background; | ||
} | ||
@@ -92,4 +94,4 @@ | ||
if (ignore != null) { | ||
const elt = target.querySelector(ignore); | ||
elt.parentNode.removeChild(elt); | ||
const elts = target.querySelectorAll(ignore); | ||
[].forEach.call(elts, elt => elt.parentNode.removeChild(elt)); | ||
} | ||
@@ -110,2 +112,2 @@ | ||
return file; | ||
} | ||
}; |
{ | ||
"name": "d3-svg-to-png", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Converts SVG elements to PNG and other image formats while keeping CSS styles", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,0 +0,0 @@ # d3-svg-to-png |
@@ -0,0 +0,0 @@ declare module 'd3-svg-to-png' { |
Sorry, the diff of this file is not supported yet
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
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
105
7456