Michal
Michal

Reputation: 193

globalAlpha on one image in canvas

I would like to do a little animation with html5-canvas. I have background, mug on background, coins in this mug which is transparently. I want fade effect, when mug is appearing. I have a problem, because when i give globalAlpha for mug, the rest got the same results. I want animation, where i could give fade effect for each element. Now i have this:

<body>
    <canvas id="myCanvas" width="1600" height="900"></canvas>
    <script>
        var canvas = document.getElementById('myCanvas');
        var context = canvas.getContext('2d');
        var background = new Image();
        var money1 = new Image();
        var money2 = new Image();
        var money3 = new Image();
        var money4 = new Image();
        var money5 = new Image()
        var mug = new Image();
        var move = 0.01;

        background.onload = function() {
            context.drawImage(background, 0, 0);
        }

        mug.onload = function(){

            context.globalAlpha = 0;
            var i = 0;

            var imgFadeInter = setInterval(function(){

                context.globalAlpha += 0.01;
                var a = context.drawImage(mug, 33, 212)
                console.log(a, 'text');
                i++;
                if(i == 10){ 
                    clearInterval(imgFadeInter)
                }

            }, 10);


        };
        money1.onload = function() {
            context.drawImage(money1, 155, 362);
        };

        money2.onload = function() {
            context.drawImage(money2, 158, 360);
        };

        money3.onload = function() {
            context.drawImage(money3, 151, 358);
        };
        money4.onload = function() {
            context.drawImage(money4, 148, 301);
        };

        money5.onload = function() {
            context.drawImage(money5, 136, 265);
        };
        background.src = 'background-mag.png';
        setTimeout(function(){mug.src = 'mug.png'}, 500);
        setTimeout(function(){money1.src = 'money1.png'}, 1000);
        setTimeout(function(){money2.src = 'money2.png'}, 2000);
        setTimeout(function(){money3.src = 'money3.png'}, 3000);
        setTimeout(function(){money4.src = 'money4.png'}, 4000);
        setTimeout(function(){money5.src = 'money5.png'}, 5000);
    </script>

Sorry for my english, I hope it what i wrote is understandable..

Upvotes: 0

Views: 757

Answers (1)

markE
markE

Reputation: 105015

You can add custom properties to your image objects, so add the alpha you desire for each image:

// start with money1's alpha at a low alpha so you can fade it in
// alpha will be divided by 100 later (using ints prevents rounding problems)
money1.alpha=20;

Then when you drawImage in your animation loop you can set individual globalAlpha based on the added property:

if(money1.alpha<=100){

    // draw money1 at its current alpha
    context.globalAlpha=money1.alpha/100;
    context.drawImage(money1, 155, 362);

    // be sure to tidy up...reset globalAlpha to its default of 1.00
    context.globalAlpha=1.00;

    // increase the alpha
    money1.alpha+=1;


};    

Good luck with your project!

Upvotes: 1

Related Questions