Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

phaser create animation from canvas texture

 preload ()
    {
        const framesPerRow = 8;
        const frameTotal = 32;

        //  Create a CanvasTexture that is 256 x 128 in size.
        //  The frames will be 32 x 32, which means we'll fit in 8 x 4 of them to our texture size, for a total of 32 frames.
        const canvasFrame = this.textures.createCanvas('dynamicFrames', 256, 128);

        let radius = 0;
        const radiusInc = 16 / frameTotal;

        let x = 0;
        let y = 0;
        let ctx = canvasFrame.context;

        ctx.fillStyle = '#ffff00';

        for (let i = 1; i <= frameTotal; i++)
        {
            //  Draw an arc to the CanvasTexture
            ctx.beginPath();
            ctx.arc(x + 16, y + 16, Math.max(1, radius), 0, Math.PI * 2, false);
            ctx.closePath();
            ctx.fill();

            //  Now we add a frame to the CanvasTexture.
            //  See the docs for the arguments.
            canvasFrame.add(i, 0, x, y, 32, 32);

            x += 32;
            radius += radiusInc;

            //  Hit the end of the row? Wrap it around.
            if (i % framesPerRow === 0)
            {
                x = 0;
                y += 32;
            }
        }

        //  Call this if running under WebGL, or you'll see nothing change
        canvasFrame.refresh();

        //  Display the whole of our freshly baked sprite sheet
        this.add.image(0, 0, 'dynamicFrames', '__BASE').setOrigin(0);

        //  Let's create an animation from the new frames
        this.anims.create({
            key: 'pulse',
            frames: this.anims.generateFrameNumbers('dynamicFrames', { start: 1, end: frameTotal }),
            frameRate: 28,
            repeat: -1,
            yoyo: true
        });

        //  Add a bunch of Sprites that all use the same base texture and animation
        for (let i = 0; i < 50; i++)
        {
            const ball = this.add.sprite(8 + i * 16, 164, 'dynamicFrames').play('pulse');

            this.tweens.add({
                targets: ball,
                y: 584,
                duration: 2000,
                ease: 'Quad.easeInOut',
                delay: i * 30,
                yoyo: true,
                repeat: -1
            });
        }
    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: phaser create animation from sprite config.js 
Javascript :: phaser hide animation on complete 
Javascript :: phaser animation on stop event 
Javascript :: phaser reverse animation 
Javascript :: accessing-nested-javascript-objects-and-arrays-by-string-path 
Javascript :: complex expression in programming 
Javascript :: using cron with bull node js 
Javascript :: remove text in div JQuery zqqqqqqqqqqqqqqqzzzzzzzzzzzzzzzzzzzzzzzzzzzzzqqqqqqqqqqqqqqqqqq 
Javascript :: after end time run function 
Javascript :: polygon intersection js 
Javascript :: Call this API in order to fetch the user data. API: https://jsonplaceholder.typicode.com/users. 
Javascript :: rxact 6 number long in yup validation 
Javascript :: the document has mutated since the result was returned 
Javascript :: check if value is a string javascript 
Javascript :: format numbers js 
Javascript :: js promise example 
Javascript :: javascript add to a dictionary 
Javascript :: localstorage in next js 
Javascript :: find an object in an array by one of its properties 
Javascript :: clear cache javascript 
Javascript :: javascript self executing function 
Javascript :: getDownload url in firebase 
Javascript :: .remove javascript 
Javascript :: create a reactjs app with backend and docker 
Javascript :: how to use break in javascript 
Javascript :: javascript after 
Javascript :: object properties 
Javascript :: twitter javascript api 
Javascript :: javascript remove last word from string 
Javascript :: react custom hooks 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =