image-exporter 1.0.0 → 1.0.1

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 CHANGED
@@ -1,4 +1,4 @@
1
- # image-exporter [v1.0.0]
1
+ # image-exporter [v1.0.1]
2
2
 
3
3
  image-exporter is a client-side javascript tool that downloads DOM elements as images. It can be imported using your favorite package manager or used directly the window.
4
4
 
@@ -814,6 +814,14 @@ async function captureElement(element, imageOptions, filenames) {
814
814
  // Ignores elements with data-ignore-capture attribute
815
815
  filter
816
816
  };
817
+ const styles = getComputedStyle(element);
818
+ const backgroundColor = styles.backgroundColor;
819
+ const backgroundImage = styles.backgroundImage;
820
+ let cleanUpBackground = false;
821
+ if (backgroundColor === "rgba(0, 0, 0, 0)" && backgroundImage === "none" && imageOptions.format === "jpg") {
822
+ element.style.backgroundColor = "#FFFFFF";
823
+ cleanUpBackground = true;
824
+ }
817
825
  switch (imageOptions.format) {
818
826
  case "jpg":
819
827
  dataURL = await toJpeg(element, htmlToImageOptions);
@@ -825,6 +833,10 @@ async function captureElement(element, imageOptions, filenames) {
825
833
  dataURL = await toSvg(element, htmlToImageOptions);
826
834
  break;
827
835
  }
836
+ if (cleanUpBackground) {
837
+ element.style.backgroundColor = "";
838
+ element.style.backgroundImage = "";
839
+ }
828
840
  return {
829
841
  dataURL,
830
842
  fileName: handleFileNames(imageOptions, filenames)
@@ -3632,7 +3644,7 @@ async function determineTotalElements(elements) {
3632
3644
  }
3633
3645
  let windowLogging = true;
3634
3646
  let loggingLevel = "none";
3635
- async function capture(elements, userConfig) {
3647
+ async function capture(elements, userConfig = defaultConfig) {
3636
3648
  try {
3637
3649
  const config = { ...defaultConfig, ...userConfig };
3638
3650
  windowLogging = config.enableWindowLogging;
@@ -819,6 +819,14 @@
819
819
  // Ignores elements with data-ignore-capture attribute
820
820
  filter
821
821
  };
822
+ const styles = getComputedStyle(element);
823
+ const backgroundColor = styles.backgroundColor;
824
+ const backgroundImage = styles.backgroundImage;
825
+ let cleanUpBackground = false;
826
+ if (backgroundColor === "rgba(0, 0, 0, 0)" && backgroundImage === "none" && imageOptions.format === "jpg") {
827
+ element.style.backgroundColor = "#FFFFFF";
828
+ cleanUpBackground = true;
829
+ }
822
830
  switch (imageOptions.format) {
823
831
  case "jpg":
824
832
  dataURL = await toJpeg(element, htmlToImageOptions);
@@ -830,6 +838,10 @@
830
838
  dataURL = await toSvg(element, htmlToImageOptions);
831
839
  break;
832
840
  }
841
+ if (cleanUpBackground) {
842
+ element.style.backgroundColor = "";
843
+ element.style.backgroundImage = "";
844
+ }
833
845
  return {
834
846
  dataURL,
835
847
  fileName: handleFileNames(imageOptions, filenames)
@@ -3637,7 +3649,7 @@
3637
3649
  }
3638
3650
  let windowLogging = true;
3639
3651
  let loggingLevel = "none";
3640
- async function capture(elements, userConfig) {
3652
+ async function capture(elements, userConfig = defaultConfig) {
3641
3653
  try {
3642
3654
  const config = { ...defaultConfig, ...userConfig };
3643
3655
  windowLogging = config.enableWindowLogging;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "image-exporter",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Easily download one or more DOM elements as images",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -2,6 +2,7 @@ import * as htmlToImage from "html-to-image";
2
2
  import { Image, ParsedImageOptions } from "../types";
3
3
  import { handleFileNames } from "./handle-filenames";
4
4
  import { Options } from "html-to-image/lib/types";
5
+ import { get } from "http";
5
6
 
6
7
  /**
7
8
  * captureElement
@@ -25,7 +26,21 @@ export async function captureElement(
25
26
  filter: filter,
26
27
  };
27
28
 
28
- // Captures image b ased on format
29
+ // If element has no background and is a JPG, default to white background
30
+ const styles = getComputedStyle(element);
31
+ const backgroundColor = styles.backgroundColor;
32
+ const backgroundImage = styles.backgroundImage;
33
+ let cleanUpBackground = false;
34
+ if (
35
+ backgroundColor === "rgba(0, 0, 0, 0)" &&
36
+ backgroundImage === "none" &&
37
+ imageOptions.format === "jpg"
38
+ ) {
39
+ element.style.backgroundColor = "#FFFFFF";
40
+ cleanUpBackground = true;
41
+ }
42
+
43
+ // Captures image based on format
29
44
  switch (imageOptions.format) {
30
45
  case "jpg":
31
46
  dataURL = await htmlToImage.toJpeg(element, htmlToImageOptions);
@@ -38,6 +53,11 @@ export async function captureElement(
38
53
  break;
39
54
  }
40
55
 
56
+ if (cleanUpBackground) {
57
+ element.style.backgroundColor = "";
58
+ element.style.backgroundImage = "";
59
+ }
60
+
41
61
  return {
42
62
  dataURL,
43
63
  fileName: handleFileNames(imageOptions, filenames),
@@ -18,7 +18,7 @@ export let loggingLevel = "none";
18
18
  */
19
19
  export async function capture(
20
20
  elements: HTMLElement[] | NodeListOf<HTMLElement> | HTMLElement,
21
- userConfig: Partial<Config>
21
+ userConfig: Partial<Config> = defaultConfig
22
22
  ): Promise<Image[] | null> {
23
23
  try {
24
24
  /* --------------------------------- Config --------------------------------- */