@@ -5,59 +5,87 @@ import { capture } from '../core/capture';
55 * and provides additional export formats.
66 *
77 * @param {Element } el - DOM element to capture
8- * @param {number } [scale=1] - Scale factor for the output image
8+ * @param {Object } [options={}] - Capture options
9+ * @param {number } [options.scale=1] - Scale factor for the output image
910 * @returns {Promise<string> } Promise that resolves to SVG data URL
1011 */
11- async function snapdom ( el , scale = 1 ) {
12- return await capture ( el , scale ) ;
12+
13+ async function snapdom ( element , options = { } ) {
14+ return await capture ( element , options ) ;
1315}
1416
15- snapdom . toImg = async function ( el , scale = 1 ) {
16- const dataURL = await snapdom ( el , scale ) ;
17+
18+ /*
19+ snapdom.toSvg = async (el, opt = {}) => {
20+ const dataUrl = await capture(el, opt);
21+ const svgEncoded = dataUrl.split(',')[1];
22+ return decodeURIComponent(svgEncoded);
23+ };
24+ */
25+
26+ snapdom . toImg = async ( el , opt = { } ) => {
27+ const url = await snapdom ( el , opt ) ;
1728 const img = new Image ( ) ;
18- img . src = dataURL ;
29+ img . src = url ;
1930 await img . decode ( ) ;
2031 return img ;
2132} ;
2233
23- snapdom . toCanvas = async function ( el , scale = 1 ) {
24- const img = await snapdom . toImg ( el , scale ) ;
25- const canvas = document . createElement ( ' canvas' ) ;
34+ snapdom . toCanvas = async ( el , opt = { } ) => {
35+ const img = await snapdom . toImg ( el , opt ) ;
36+ const canvas = document . createElement ( " canvas" ) ;
2637 canvas . width = img . width ;
2738 canvas . height = img . height ;
28- const ctx = canvas . getContext ( '2d' ) ;
29- ctx . drawImage ( img , 0 , 0 ) ;
39+ canvas . getContext ( "2d" ) . drawImage ( img , 0 , 0 ) ;
3040 return canvas ;
3141} ;
3242
33- snapdom . toPng = async function ( el , scale = 1 ) {
34- const canvas = await snapdom . toCanvas ( el , scale ) ;
43+ snapdom . toPng = async ( el , opt = { } ) => {
44+ const canvas = await snapdom . toCanvas ( el , opt ) ;
3545 const img = new Image ( ) ;
36- img . src = canvas . toDataURL ( ' image/png' ) ;
46+ img . src = canvas . toDataURL ( " image/png" ) ;
3747 await img . decode ( ) ;
3848 return img ;
3949} ;
4050
41- snapdom . toJpg = async function ( el , scale = 1 , quality = 0.92 ) {
42- const canvas = await snapdom . toCanvas ( el , scale ) ;
51+ snapdom . toJpg = async ( el , opt = { } ) => {
52+ const { backgroundColor = "#fff" , quality = 0.92 } = opt ;
53+ const canvas = await snapdom . toCanvas ( el , opt ) ;
54+ const temp = document . createElement ( "canvas" ) ;
55+ temp . width = canvas . width ;
56+ temp . height = canvas . height ;
57+ const ctx = temp . getContext ( "2d" ) ;
58+ ctx . fillStyle = backgroundColor ;
59+ ctx . fillRect ( 0 , 0 , temp . width , temp . height ) ;
60+ ctx . drawImage ( canvas , 0 , 0 ) ;
4361 const img = new Image ( ) ;
44- img . src = canvas . toDataURL ( ' image/jpeg' , quality ) ;
62+ img . src = temp . toDataURL ( " image/jpeg" , quality ) ;
4563 await img . decode ( ) ;
4664 return img ;
4765} ;
4866
49- snapdom . toWebp = async function ( el , scale = 1 , quality = 0.92 ) {
50- const canvas = await snapdom . toCanvas ( el , scale ) ;
67+ snapdom . toWebp = async ( el , opt = { } ) => {
68+ const { backgroundColor = "#fff" , quality = 0.92 } = opt ;
69+ const canvas = await snapdom . toCanvas ( el , opt ) ;
70+ const temp = document . createElement ( "canvas" ) ;
71+ temp . width = canvas . width ;
72+ temp . height = canvas . height ;
73+ const ctx = temp . getContext ( "2d" ) ;
74+ ctx . fillStyle = backgroundColor ;
75+ ctx . fillRect ( 0 , 0 , temp . width , temp . height ) ;
76+ ctx . drawImage ( canvas , 0 , 0 ) ;
5177 const img = new Image ( ) ;
52- img . src = canvas . toDataURL ( ' image/webp' , quality ) ;
78+ img . src = temp . toDataURL ( " image/webp" , quality ) ;
5379 await img . decode ( ) ;
5480 return img ;
5581} ;
5682
57- snapdom . toBlob = async function ( el , scale = 1 ) {
58- const dataURL = await snapdom ( el , scale ) ;
59- const svgXml = decodeURIComponent ( dataURL . split ( "," ) [ 1 ] ) ;
60- return new Blob ( [ svgXml ] , { type : "image/svg+xml" } ) ;
83+ snapdom . toBlob = async ( el , opt = { } ) => {
84+ const dataUrl = await snapdom ( el , opt ) ;
85+ const svgText = decodeURIComponent ( dataUrl . split ( ',' ) [ 1 ] ) ;
86+ return new Blob ( [ svgText ] , { type : "image/svg+xml" } ) ;
6187} ;
6288
63- export { snapdom } ;
89+ export {
90+ snapdom
91+ } ;
0 commit comments