Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript HTML5画布照片幻灯片放映问题_Javascript_Html_Canvas - Fatal编程技术网

Javascript HTML5画布照片幻灯片放映问题

Javascript HTML5画布照片幻灯片放映问题,javascript,html,canvas,Javascript,Html,Canvas,您好,我找到了一个帆布照片幻灯片,但我只是想知道如何使图像之间的转换变慢 var loaded = 0, numOfImages = 4; //first part of chain, invoke async load var image0 = document.createElement('img'); //this will work in new Chrome var image1 = document.createElement('img'); //instead of new Im

您好,我找到了一个帆布照片幻灯片,但我只是想知道如何使图像之间的转换变慢

var loaded = 0, numOfImages = 4;

//first part of chain, invoke async load
var image0 = document.createElement('img'); //this will work in new Chrome
var image1 = document.createElement('img'); //instead of new Image
var image2 = document.createElement('img');
var image3 = document.createElement('img');

//common event handler when images has loaded with counter
//to know that all images has loaded
image0.onload = image1.onload = 
image2.onload = image3.onload = function(e) {
    loaded+;
    if (loaded === numOfImages)

        draw();   // <-- second part of chain, invoke loop
}

//show if any error occurs
image0.onerror = image1.onerror = 
image2.onerror = image3.onerror = function(e) {
    console.log(e);
}

//invoke async loading... you can put these four into your
//window.onload if you want to
image0.src = "img/pic1.jpg";
image1.src = "img/pic2.jpg";
image2.src = "img/pic3.jpg";
image3.src = "img/pic4.jpg";

// this is the main function
function draw() {

    var images = new Array(image0, image1, image2, image3),
        counter = 0,
        maxNum = images.length - 1,

        myCanvas = document.getElementById('myCanvas'),
        ctx = myCanvas.getContext('2d'),

        me = this; //this we need for setTimeout()

    //third part of chain, have a function to invoke by setTimeout
    this._draw = function() {

        //if the next image will cover the canvas
        //there is no real need to clear the canvas first.
        //I'll leave it here as you ask for this specifically
        ctx.clearRect(0, 0, myCanvas.width, myCanvas.height)
        ctx.drawImage(images[counter++], 0, 0);
        if (counter > maxNum) counter = 0;

        setTimeout(me._draw, 1000); //here we use me instead of this
    }
    this._draw(); //START the loop
}
var-load=0,numOfImages=4;
//链的第一部分,调用异步加载
var image0=document.createElement('img')//这将在新的Chrome中工作
var image1=document.createElement('img')//而不是新的形象
var image2=document.createElement('img');
var image3=document.createElement('img');
//使用计数器加载图像时的公共事件处理程序
//要知道所有图像都已加载
image0.onload=image1.onload=
image2.onload=image3.onload=function(e){
加载+;
如果(已加载===numOfImages)

draw();//您的onload方法中有一个输入错误:

loaded++;  // instead of loaded+;
setTimeout控制延迟,直到再次调用me.\u draw

例如,3秒的延迟为3000毫秒,如下所示:

setTimeout(me._draw, 3000); 
如果您想要实际的转换,您可以使用如下内容:

setTimeout(me._draw, 3000); 

下面是代码和小提琴:


正文{背景色:象牙;}
画布{边框:1px纯红;}
$(函数(){
加载的var=0,numOfImages=4;
//链的第一部分,调用异步加载
var image0=document.createElement('img');//这将在新的Chrome中工作
var image1=document.createElement('img');//代替新图像
var image2=document.createElement('img');
var image3=document.createElement('img');
//使用计数器加载图像时的公共事件处理程序
//要知道所有图像都已加载
image0.onload=image1.onload=
image2.onload=image3.onload=function(e){
加载++;
如果(已加载===numOfImages)

draw();//我确实更改了setTimeout,但出于某种原因,它并没有产生任何影响?有什么想法吗?您的代码中有一个错误:loaded+应该是loaded++。无论如何,下面是一个使用3000ms延迟而不是1000ms延迟的运行示例。干杯!