1
- module . exports = ( page , { glbPath, outputPath, format, quality, timeout} ) => {
2
- return page . evaluate ( ( browser_glbPath , browser_outputPath , browser_format , browser_quality , timeout ) => {
1
+ const colors = require ( './colors' )
2
+ module . exports = ( page , { glbPath, outputPath, backgroundColor, format, quality, timeout} ) => {
3
+ return page . evaluate ( ( browser_glbPath , browser_outputPath , backgroundColor , image_format , browser_quality , timeout , colors ) => {
3
4
return new Promise ( ( resolve , reject ) => {
4
5
var startTime = Number ( new Date ( ) ) ;
5
6
var endTime = startTime + timeout ;
@@ -13,16 +14,48 @@ module.exports = (page, {glbPath, outputPath, format, quality, timeout}) => {
13
14
}
14
15
}
15
16
16
- modelViewer = document . getElementById ( 'snapshot-viewer' ) ;
17
- timeoutSet = setInterval ( isTimedOut , 1000 ) ;
17
+ const rafAsync = function ( ) {
18
+ return new Promise ( resolve => {
19
+ requestAnimationFrame ( resolve ) ;
20
+ } ) ;
21
+ }
22
+
23
+ const checkElementExists = async function ( element ) {
24
+ while ( element === null ) {
25
+ await rafAsync ( )
26
+ }
27
+ return element ;
28
+ }
29
+
30
+ const modelViewerCanvas = async function ( resolveCanvas ) {
31
+ checkElementExists ( modelViewer . shadowRoot )
32
+ const srcCanvas = modelViewer . shadowRoot . getElementById ( 'webgl-canvas' ) ;
33
+ checkElementExists ( srcCanvas )
34
+ if ( backgroundColor === colors . transparent ) {
35
+ resolveCanvas ( srcCanvas ) ;
36
+ }
37
+ const destinationCanvas = document . createElement ( "canvas" ) ;
38
+ destinationCanvas . width = srcCanvas . width ;
39
+ destinationCanvas . height = srcCanvas . height ;
40
+
41
+ const destCtx = destinationCanvas . getContext ( '2d' ) ;
42
+ destCtx . fillStyle = backgroundColor ;
43
+ destCtx . fillRect ( 0 , 0 , srcCanvas . width , srcCanvas . height ) ;
44
+
45
+ destCtx . drawImage ( srcCanvas , 0 , 0 ) ;
18
46
19
- modelViewer . addEventListener ( 'model-visibility' , function ( ) {
47
+
48
+ resolveCanvas ( destinationCanvas ) ;
49
+ }
50
+
51
+ const modelViewerVisibleCallback = async function ( event ) {
20
52
try {
21
53
const visible = event . detail . visible ;
22
54
if ( visible ) {
23
55
let t0 = Number ( new Date ( ) ) ;
56
+ const canvas = await new Promise ( modelViewerCanvas )
24
57
window . saveDataUrl (
25
- modelViewer . toDataURL ( browser_format , browser_quality ) ,
58
+ canvas . toDataURL ( image_format , browser_quality ) ,
26
59
browser_outputPath ,
27
60
) ;
28
61
@@ -37,8 +70,13 @@ module.exports = (page, {glbPath, outputPath, format, quality, timeout}) => {
37
70
} catch ( err ) {
38
71
reject ( err ) ;
39
72
}
40
- } ) ;
73
+ }
74
+
75
+ modelViewer = document . getElementById ( 'snapshot-viewer' ) ;
76
+ timeoutSet = setInterval ( isTimedOut , 1000 ) ;
77
+
78
+ modelViewer . addEventListener ( 'model-visibility' , modelViewerVisibleCallback )
41
79
modelViewer . src = browser_glbPath ;
42
80
} ) ;
43
- } , glbPath , outputPath , format , quality , timeout ) ;
81
+ } , glbPath , outputPath , backgroundColor , format , quality , timeout , colors ) ;
44
82
}
0 commit comments