javascript - 使用过渡更改 Canvas 上的图像

标签 javascript image canvas transition

下面的代码让不同的图片出现在我的 Canvas 上。 我如何通过淡入淡出和滑动等漂亮的过渡来改变它们?

setInterval(function () {
    if (i >= carInfo.length) {
        i = 0;
    }
    imageObj.onload = function () {
        context.clearRect(0, 0, canvas.width, canvas.height);
        context.fillText(textVar, 10, 10);
        context.drawImage(imageObj, 69, 50);
    };
    imageObj.src = '' + carInfo[i].Picture + '';
    var textVar = "" + carInfo[i].carType + ": " + carInfo[i].Description;
   // alert(textVar);
    i++
},2000);

最佳答案

淡入/淡出

可以通过使用 context.setGlobalAlpha 逐渐改变图像的不透明度来完成

幻灯片

可以通过逐渐改变 context.drawimage(imageObj,x,y) 的 x,y 坐标来完成

这是代码和 fiddle :http://jsfiddle.net/m1erickson/5sAS9/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" />
<script src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>

<script>
    $(function(){

        var canvas=document.getElementById("canvas");
        var ctx=canvas.getContext("2d");

        window.requestAnimFrame = (function(callback) {
          return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
          function(callback) {
            window.setTimeout(callback, 1000 / 60);
          };
        })();


        var imageIndex=0;
        var animPctComplete=0;
        var fps = 60;

        // image loader

        var imageURLs=[];
        var imagesOK=0;
        var imgs=[];
        imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-1.jpg");
        imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-2.jpg");
        imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house204-3.jpg");
        loadAllImages();

        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 ) {
                        animate();
                    }
                }; 
                img.src = imageURLs[i];
            }      
        }

        function animate() {
            setTimeout(function() {
                requestAnimFrame(animate);

                // get the current image
                // get the xy where the image will be drawn
                var img=imgs[imageIndex];
                var imgX=(canvas.width/2-img.width/2)*animPctComplete;
                var imgY=(canvas.height/2)-img.height/2;

                // set the current opacity
                ctx.globalAlpha=animPctComplete;

                // draw the image
                ctx.clearRect(0,0,canvas.width,canvas.height);
                ctx.drawImage(img,imgX,imgY);

                // increment the animationPctComplete for next frame
                animPctComplete+=.01;  //100/fps;

                // if the current animation is complete
                // reset the animation with the next image
                if(animPctComplete>=1.00){
                    animPctComplete=0.00;
                    imageIndex++;
                    if(imageIndex>=imgs.length){imageIndex=0;}
                }

            }, 1000 / fps);
        }


    }); // end $(function(){});
</script>

</head>

<body>
    <canvas id="canvas" width=600 height=400></canvas>
</body>
</html>

关于javascript - 使用过渡更改 Canvas 上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18717457/

相关文章:

javascript - 使用 javascript instant.js 库处理图像不适用于 ajax

node.js - 错误 : Module version mismatch. 预期为 11,实际为 1

javascript - 将带有图像标签的 Svg 转换为 PNG

javascript - 来自其他文件的函数未定义

php - 获取图像尺寸

Canvas 的java增量时间问题

javascript - HTML5 Canvas 显示图像

javascript - 使用jquery从表中获取订单总值(value)

javascript - 只为 iOS 设备加载 JS 脚本?

python - int16、uint8 等图像背后的逻辑