Skip to content
This repository was archived by the owner on Jun 25, 2020. It is now read-only.

Commit 115bf91

Browse files
committed
Add 'modifyCss' option. Improve docs. Some cleanup.
1 parent 39a42a0 commit 115bf91

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,17 @@ If you want to use TypeScript, necessary [type definitions](https://github.com/m
4747
### Options
4848

4949
- `backgroundColor` — Creates a PNG with the given background color. Defaults to transparent.
50-
- `left` - Specify the viewbox's left position. Defaults to 0.
50+
- `canvg` - If canvg is passed in, it will be used to write svg to canvas. This will allow support for Internet Explorer
51+
- `encoderOptions` - A Number between 0 and 1 indicating image quality. The default is 0.8
52+
- `encoderType` - A DOMString indicating the image format. The default type is image/png.
5153
- `height` - Specify the image's height. Defaults to the viewbox's height if given, or the element's non-percentage height, or the element's bounding box's height, or the element's CSS height, or the computed style's height, or 0.
54+
- `left` - Specify the viewbox's left position. Defaults to 0.
55+
- `modifyCss` - A function that takes a CSS rule's selector and properties and returns a string of CSS. Supercedes `selectorRemap` and `modifyStyle`. Useful for modifying properties only for certain CSS selectors.
56+
- `modifyStyle` - A function that takes a CSS rule's properties and returns a string of CSS. Useful for modifying properties before they're inlined into the SVG.
5257
- `scale` — Changes the resolution of the output PNG. Defaults to `1`, the same dimensions as the source SVG.
5358
- `selectorRemap` — A function that takes a CSS selector and produces its replacement in the CSS that's inlined into the SVG. Useful if your SVG style selectors are scoped by ancestor elements in your HTML document.
54-
- `modifyStyle` - A function that takes CSS styles and returns replacement styles.
5559
- `top` - Specify the viewbox's top position. Defaults to 0.
5660
- `width` - Specify the image's width. Defaults to the viewbox's width if given, or the element's non-percentage width, or the element's bounding box's width, or the element's CSS width, or the computed style's width, or 0.
57-
- `encoderType` - A DOMString indicating the image format. The default type is image/png.
58-
- `encoderOptions` - A Number between 0 and 1 indicating image quality. The default is 0.8
59-
- `canvg` - If canvg is passed in, it will be used to write svg to canvas. This will allow support for Internet Explorer
6061

6162
### Testing
6263

index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
fill: green !important;
6464
}
6565

66+
#selectors-prefixed rect.css-styled {
67+
fill: green !important;
68+
}
69+
6670
/* Invalid selectors */
6771

6872
[ng\:cloak] {
@@ -192,6 +196,20 @@ <h3>Preview <button class="save btn">Save as PNG</button></h3>
192196
</svg>
193197
</li>
194198

199+
<li id=modified-style>
200+
<svg width=200 height=200>
201+
<rect x=0 y=50 width=100 height=100></rect>
202+
<rect class=css-styled x=100 y=50 width=100 height=100></rect>
203+
</svg>
204+
</li>
205+
206+
<li id=modified-css>
207+
<svg width=200 height=200>
208+
<rect x=0 y=50 width=100 height=100></rect>
209+
<rect class=css-styled x=100 y=50 width=100 height=100></rect>
210+
</svg>
211+
</li>
212+
195213
<li id=group>
196214
<svg width=200 height=200>
197215
<g id=sub-group transform="translate(40,40)">
@@ -347,6 +365,16 @@ <h3>Preview <button class="save btn">Save as PNG</button></h3>
347365
inlineTest('When CSS styling selectors are prefixed', $('#selectors-prefixed'), {
348366
selectorRemap: function(s) {return s.replace('#selectors-prefixed ', '')}
349367
});
368+
inlineTest('Modifying the style', $('#modified-style'), {
369+
modifyStyle: function(s) {return s.replace('green', 'red')}
370+
});
371+
inlineTest('Modifying the whole CSS rule', $('#modified-css'), {
372+
modifyCss: function(selector, properties) {
373+
selector = selector.replace('#selectors-prefixed ', '');
374+
properties = properties.replace('green', 'blue');
375+
return selector + '{' + properties + '}';
376+
}
377+
});
350378
inlineTest('Exporting a group within an SVG', $('#group'), null, {
351379
selector: '#sub-group'
352380
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "save-svg-as-png",
3-
"version": "1.2.0",
3+
"version": "1.3.1",
44
"description": "Convert a browser SVG to PNG or dataUri",
55
"main": "saveSvgAsPng.js",
66
"scripts": {

saveSvgAsPng.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@
6969
function styles(el, options, cssLoadedCallback) {
7070
var selectorRemap = options.selectorRemap;
7171
var modifyStyle = options.modifyStyle;
72+
var modifyCss = options.modifyCss || function(selector, properties) {
73+
var selector = selectorRemap ? selectorRemap(selector) : selector;
74+
var cssText = modifyStyle ? modifyStyle(properties) : properties;
75+
return selector + " { " + cssText + " }\n";
76+
};
7277
var css = "";
73-
// each font that has extranl link is saved into queue, and processed
74-
// asynchronously
78+
79+
// Each font that has an external link is saved into queue, and processed asynchronously.
7580
var fontsQueue = [];
7681
var sheets = document.styleSheets;
7782
for (var i = 0; i < sheets.length; i++) {
@@ -103,9 +108,7 @@
103108
}
104109

105110
if (match) {
106-
var selector = selectorRemap ? selectorRemap(rule.selectorText) : rule.selectorText;
107-
var cssText = modifyStyle ? modifyStyle(rule.style.cssText) : rule.style.cssText;
108-
css += selector + " { " + cssText + " }\n";
111+
css += modifyCss(rule.selectorText, rule.style.cssText);
109112
} else if(rule.cssText.match(/^@font-face/)) {
110113
// below we are trying to find matches to external link. E.g.
111114
// @font-face {
@@ -373,13 +376,13 @@
373376

374377
var pixelRatio = window.devicePixelRatio || 1;
375378

376-
canvas.style.width = canvas.width +'px';
377-
canvas.style.height = canvas.height +'px';
379+
canvas.style.width = canvas.width+'px';
380+
canvas.style.height = canvas.height+'px';
378381
canvas.width *= pixelRatio;
379382
canvas.height *= pixelRatio;
380383

381384
context.setTransform(pixelRatio,0,0,pixelRatio,0,0);
382-
385+
383386
if(options.canvg) {
384387
options.canvg(canvas, src);
385388
} else {

0 commit comments

Comments
 (0)