How to put a gif with Canvas

前端 未结 6 1349
孤街浪徒
孤街浪徒 2020-12-23 23:43

I\'m creating a game, and in my game, when the HERO stay near the MONSTER, a gif will be showed, to scare the player. But I have no idea how to do this. I tried to put PHP o

6条回答
  •  长情又很酷
    2020-12-23 23:53

    Here ya go: You will need to exctract each frames and make an array out of them split frames: http://gifgifs.com/split/

    easier if you have urls or path like http://lol.com/Img1.png ...... http://lol.com/Img27.png with which you can do a simple loop like this:

    var Img = [];
    for (var i = 0; i < 28; i++) {
      Img[i] = new Image();
      Img[i].src = "http://lol.com/Img"+i+".png";
    }
    

    function drawAnimatedImage(arr,x,y,angle,factor,changespeed) {
        if (!factor) {
            factor = 1;
        }
        if (!changespeed) {
            changespeed = 1;
        }
        ctx.save();
        ctx.translate(x, y);
        ctx.rotate(angle * Math.PI / 180);
        if (!!arr[Math.round(Date.now()/changespeed) % arr.length]) {
        ctx.drawImage(arr[Math.round(Date.now()/changespeed) % arr.length], -(arr[Math.round(Date.now()/changespeed) % arr.length].width * factor / 2), -(arr[Math.round(Date.now()/changespeed) % arr.length].height * factor / 2), arr[Math.round(Date.now()/changespeed) % arr.length].width * factor, arr[Math.round(Date.now()/changespeed) % arr.length].height * factor);
        }
        ctx.restore();
    }
    
        var canvas = document.getElementById("myCanvas");
        var ctx = canvas.getContext('2d');
        var waitingWolf = [];
        var url = ["https://i.imgur.com/k3T7psX.gif","https://i.imgur.com/CTSC8FC.gif","https://i.imgur.com/6NHLWKK.gif","https://i.imgur.com/U1u04sY.gif","https://i.imgur.com/4695vnQ.gif","https://i.imgur.com/oDO0YWT.gif","https://i.imgur.com/LqptRh1.gif","https://i.imgur.com/6gTxvul.gif","https://i.imgur.com/ULN5mqK.gif","https://i.imgur.com/RACB9WM.gif","https://i.imgur.com/4TZ6kNi.gif","https://i.imgur.com/9VvlzhK.gif","https://i.imgur.com/nGUnsfW.gif","https://i.imgur.com/2h8vLjK.gif","https://i.imgur.com/ZCdKkF1.gif","https://i.imgur.com/wZmWrYP.gif","https://i.imgur.com/4lhjVSz.gif","https://i.imgur.com/wVO0PbE.gif","https://i.imgur.com/cgGn5tV.gif","https://i.imgur.com/627gH5Y.gif","https://i.imgur.com/sLDSeS7.gif","https://i.imgur.com/1i1QNAs.gif","https://i.imgur.com/V3vDA1A.gif","https://i.imgur.com/Od2psNo.gif","https://i.imgur.com/WKDXFdh.gif","https://i.imgur.com/RlhIjaM.gif","https://i.imgur.com/293hMnm.gif","https://i.imgur.com/ITm0ukT.gif"]
        function setup () {
                    for (var i = 0; i < 28; i++) {
                        waitingWolf[i] = new Image();
                        waitingWolf[i].src = url[i];
                    }
        }
        setup();
    function yop() {
      ctx.clearRect(0,0,1000,1000)
                if (waitingWolf.length == 28) {
                    drawAnimatedImage(waitingWolf,300,100,0,1,60)
                }
    requestAnimationFrame(yop);
    }
    requestAnimationFrame(yop);
    
    

提交回复
热议问题