画布图像动画(Crossfade)

时间:2015-11-06 02:15:33

标签: javascript canvas tweenmax

使用HTML5画布我试图加载图片并创建一个交叉淡入淡出'当第一张图像淡入视图然后,在短暂的延迟后,第二张图像在第一张图像的顶部淡入等等。

在这个论坛的类似问题的答案的帮助下,我有2个单独的代码工作...一个加载一系列图像,另一个激活一个'淡入&#39 ;影响。我的问题是我不知道如何将这两个脚本组合起来加载图像数组,并且每个图像在加载时都会逐渐淡入。

以下是我工作过的2个单独的脚本:

将图像阵列加载到CANVAS中:

HTML

<canvas id="canvas" width=600 height=350></canvas>

JS

window.onload = function() {

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;

var delay=2000;
var nextTime=0;
var nextImage=0;

var imageURLs=[];  
imageURLs.push("img/sunflower0.jpg");
imageURLs.push("img/sunflower1.jpg");
imageURLs.push("img/sunflower2.jpg");

var imgs=[];
var imagesOK=0;
loadAllImages(start);

function loadAllImages(callback){
    for (var i=0; i<imageURLs.length; i++) {
        var img = new Image();
        imgs.push(img);
        img.onload = function(){ 
            imagesOK++; 
            if (imagesOK >= imageURLs.length ) {
                callback();
            }
        };
        img.src = imageURLs[i];
   }      
}

function start(){
    requestAnimationFrame(animate);
}

function animate(currentTime){
    requestAnimationFrame(animate);

    if(currentTime<nextTime){return;}
        ctx.clearRect(0,0,cw,ch);
        ctx.drawImage(imgs[nextImage],0,0);
        nextTime=currentTime+delay;
        nextImage++;
       if(nextImage>imgs.length-1){nextImage=0;}

}
} // close window.onload

以他们加载到CANVAS中的形象出现:

我设法使用Canvas和Greensock TweenMax获得单独的代码工作:

<script     src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.0/TweenMax.min.js">     </script>

<script>

var ctx, img;

function init() {
    ctx = document.getElementById("canvas").getContext("2d");

    img = new Image();
    img.src = "img/sunflower0.jpg";
    img.xpos = 0;
    img.ypos = 0;
    img.globalAlpha = 0;

    img.onload = function() {
        TweenMax.ticker.addEventListener("tick",loop);
    }

    TweenMax.to(img, 5 ,{globalAlpha:1});
}

function loop(){
   ctx.clearRect(0,0,500,336);
   ctx.globalAlpha = img.globalAlpha;
   ctx.drawImage(img, img.xpos, img.ypos);
}

init(); 

有人能告诉我如何组合这两个脚本以获得交叉渐变效果吗?

非常感谢!

1 个答案:

答案 0 :(得分:4)

我会使用三个画布:

var imgs = [];

var rand = Math.random;
var common = "http://lorempixel.com/500/300?";
var imageURLs = [common + rand(), common + rand(), common + rand(), common + rand(), common + rand()];

var imagesOK = 0;
function loadAllImages(callback) {
  for (var i = 0; i < imageURLs.length; i++) {
    var img = new Image();
    imgs.push(img);
    img.onload = function() {
      imagesOK++;
      if (imagesOK >= imageURLs.length) {
        callback();
      }
    };
    img.src = imageURLs[i];
  }
}
var ctx = main.getContext('2d');
var last = main.cloneNode(true).getContext('2d');
var next = main.cloneNode(true).getContext('2d');

var current = 0;
var op = 1;

function nextImage() {
  if (current++ >= imgs.length - 1) current = 0;
  op = 1;
  fade();
}

function fade() {
  op -= .01;
  
  last.clearRect(0, 0, main.width, main.height);
  last.globalAlpha = op;
  last.drawImage(imgs[current], 0, 0);

  next.clearRect(0, 0, main.width, main.height);
  next.globalAlpha = 1 - op;
  next.drawImage(imgs[(current + 1) % (imgs.length)], 0, 0);

  ctx.clearRect(0, 0, main.width, main.height);
  ctx.drawImage(last.canvas, 0, 0);
  ctx.drawImage(next.canvas, 0, 0);

  if (op <= 0) setTimeout(nextImage, 1500);
  else requestAnimationFrame(fade);
}
loadAllImages(fade);
<canvas id="main" width="500" height="300"></canvas>