11/**
22 * This is the frontend script loaded in the frontend if the action is used.
33 */
4+
5+ const LOCATION_MAP = {
6+ center : { x : 0.5 , y : 0.60 } ,
7+ top : { x : 0.5 , y : 0.10 } ,
8+ bottom : { x : 0.5 , y : 0.90 } ,
9+ left : { x : 0.10 , y : 0.5 } ,
10+ right : { x : 0.90 , y : 0.5 } ,
11+ 'top-left' : { x : 0.10 , y : 0.10 } ,
12+ 'top-right' : { x : 0.90 , y : 0.10 } ,
13+ 'bottom-left' : { x : 0.10 , y : 0.90 } ,
14+ 'bottom-right' : { x : 0.90 , y : 0.90 } ,
15+ }
16+
417InteractRunner . addActionConfig ( {
518 confetti : {
619 initAction : action => {
720 action . initActionFunction ( ( ) => {
8- const origin = { x : 0.5 , y : 0.65 }
21+ const location = action . getValue ( 'location' ) || 'center'
922
1023 const targets = action . getTargets ( )
1124 let targetEl = targets . length ? targets [ 0 ] : window
@@ -15,11 +28,16 @@ InteractRunner.addActionConfig( {
1528 targetEl = window
1629 }
1730
31+ let origin = { x : 0.5 , y : 0.60 }
32+ const pos = LOCATION_MAP [ location ] || LOCATION_MAP . center
33+
1834 if ( targetEl !== window && targetEl !== document ) {
1935 // Get the percentage of the center of the target element to the screen width
2036 const rect = targetEl . getBoundingClientRect ( )
21- origin . x = ( rect . left + ( rect . width / 2 ) ) / window . innerWidth
22- origin . y = ( rect . top + ( rect . height / 2 ) + 20 ) / window . innerHeight
37+ origin . x = ( rect . left + ( rect . width * pos . x ) ) / window . innerWidth
38+ origin . y = ( rect . top + ( rect . height * pos . y ) ) / window . innerHeight
39+ } else {
40+ origin = pos
2341 }
2442
2543 window . interactions . confetti ( origin )
0 commit comments