Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

reactjs - React component export as PNG with a transparent background

I'm trying to use exportComponentAsPNG function from react-component-export-image lib: https://www.npmjs.com/package/react-component-export-image

exportComponentAsPNG(node, {fileName, html2CanvasOptions})

The function works great if I pass just a component. But I can't make it work with optional params and produce an image with transparent background (maybe I'm missing something about optional parameters conversion of JS in general). So I tried to pass a backgroundColor option, which, I think, should help me with getting a transparent background. What I've already tried:

  • exportComponentAsPNG(node, backgroundColor = null);
  • exportComponentAsPNG(node, backgroundColor: null);
  • exportComponentAsPNG(node, { backgroundColor: null });

I could specify backgroundColor with the previous version of react-component-export-image API, but now I'm getting lost with the new syntax.

question from:https://stackoverflow.com/questions/65918916/react-component-export-as-png-with-a-transparent-background

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As it is mentioned in the docs

How to Upgrade

The previous way of using an export looked like this:

exportComponentAsJPEG(node, fileName, type, backgroundColor, options)

  • The new way: pass node & an optional object with only the fields you need.
  • backgroundColor is no longer accepted in this main object, but is accepted in the "html2CanvasOptions" object, which is passed directly to html2canvas

if you say it've worked before with backgorundColor equals to null you should call it this way

exportComponentAsPNG(node, { html2CanvasOptions: {backgroundColor: null} })

you can also take look at its implementation src-code


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...