|
| 1 | +import { gsap } from 'gsap'; |
| 2 | +import Physics2DPlugin from 'gsap/Physics2DPlugin'; |
| 3 | +import { Application, Assets, Graphics, Text } from 'pixi.js'; |
| 4 | +import { DropShadowFilter } from 'pixi-filters'; |
| 5 | + |
| 6 | +gsap.registerPlugin(Physics2DPlugin); |
| 7 | + |
| 8 | +(async () => { |
| 9 | + // Create a new application |
| 10 | + const app = new Application(); |
| 11 | + |
| 12 | + // Initialize the application |
| 13 | + await app.init({ background: '#1099bb', resizeTo: window, antialias: true }); |
| 14 | + await Assets.load( |
| 15 | + 'https://pixijs.com/assets/webfont-loader/Grandstander-ExtraBold.ttf', |
| 16 | + ); |
| 17 | + |
| 18 | + // Append the application canvas to the document body |
| 19 | + document.body.appendChild(app.canvas); |
| 20 | + |
| 21 | + app.stage.eventMode = 'static'; // Set the stage to static event mode |
| 22 | + app.stage.cursor = 'pointer'; // Change cursor to pointer on hover |
| 23 | + app.stage.hitArea = app.screen; // Set the hit area to the entire screen |
| 24 | + |
| 25 | + app.stage.addEventListener('click', (event) => { |
| 26 | + // Generate a random number of dots |
| 27 | + const dotCount = gsap.utils.random(15, 30, 1); |
| 28 | + const colors = ['white']; |
| 29 | + |
| 30 | + for (let i = 0; i < dotCount; i++) { |
| 31 | + // Create a dot element |
| 32 | + const dot = new Graphics() |
| 33 | + .circle(0, 0, gsap.utils.random(20, 40)) |
| 34 | + .fill('white') // Pick a random color |
| 35 | + .stroke({ color: 'white', width: 2 }); // Add a white stroke |
| 36 | + |
| 37 | + dot.filters = [ |
| 38 | + new DropShadowFilter({ |
| 39 | + color: 'black', |
| 40 | + alpha: 0.5, |
| 41 | + blur: 4, |
| 42 | + offset: { x: 0, y: 5 }, |
| 43 | + }), |
| 44 | + ]; |
| 45 | + |
| 46 | + app.stage.addChild(dot); |
| 47 | + |
| 48 | + // Set initial position and styles of the dot |
| 49 | + gsap.set(dot, { |
| 50 | + tint: gsap.utils.random(colors), // Pick a random color |
| 51 | + y: event.clientY, // position dot at coordinates of the click |
| 52 | + x: event.clientX, |
| 53 | + scale: 0, // Start at scale 0 |
| 54 | + }); |
| 55 | + |
| 56 | + // Animate the dot with physics2D |
| 57 | + gsap |
| 58 | + .timeline({ |
| 59 | + onComplete: () => dot.destroy(), // Clean up the dot after animation |
| 60 | + }) |
| 61 | + .to(dot, { |
| 62 | + scale: gsap.utils.random(0.3, 1), // Random scale for the pop-in effect |
| 63 | + duration: 0.02, // Quick pop-in effect |
| 64 | + ease: 'power3.out', |
| 65 | + }) |
| 66 | + .to(dot, { |
| 67 | + duration: 2, |
| 68 | + physics2D: { |
| 69 | + velocity: gsap.utils.random(500, 1000), // Random velocity |
| 70 | + angle: gsap.utils.random(0, 360), // Random direction |
| 71 | + gravity: 1500, // Gravity effect |
| 72 | + }, |
| 73 | + ease: 'none', |
| 74 | + }); // Start together with the previous tween |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + // add some text to the stage |
| 79 | + const text = new Text({ |
| 80 | + text: 'Click to create confetti', |
| 81 | + style: { |
| 82 | + fontFamily: 'Grandstander ExtraBold', |
| 83 | + fontSize: 50, |
| 84 | + fill: 'white', |
| 85 | + align: 'center', |
| 86 | + }, |
| 87 | + anchor: 0.5, |
| 88 | + x: app.screen.width / 2, |
| 89 | + y: app.screen.height / 2 - 50, |
| 90 | + }); |
| 91 | + |
| 92 | + text.filters = [ |
| 93 | + new DropShadowFilter({ |
| 94 | + color: 'black', |
| 95 | + alpha: 0.5, |
| 96 | + blur: 4, |
| 97 | + offset: { x: 0, y: 5 }, |
| 98 | + }), |
| 99 | + ]; |
| 100 | + |
| 101 | + app.stage.addChild(text); |
| 102 | +})(); |
0 commit comments