如何删除和递归图像?

时间:2019-05-29 15:12:52

标签: javascript canvas

我使图像从右移到左。 但我想加上功能,当图像达到x:50时,此图像被删除 然后向左画。

我尝试使用“ if”之类的控制语句,但是它不起作用

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

var img = new Image();
img.src = "img1.png";
var speed = 0;
var xpos = 800;
var CXpos = 0;  //changeing xpos
var result = Math.floor(Math.random() * 500) + 1;

function icon(){
    ctx.beginPath();
    speed -= 1;
    CXpos = xpos + speed;
    ctx.drawImage(img, CXpos, result, 60, 60);
    if(Cxpos == 50){
        canvas.width = canvas.width;    //I don't know it's correct code?
        icon();
    }
    ctx.closePath();
}

function iconypos(){
    var num = Math.floor(Math.random*100)+1
    return num;
}

function draw(){
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    icon();
}

setInterval(draw, 10);

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的意思,这就是您要寻找的。只需将<ul class="tags"> <li class="tag"><a rel="tag" class="term" href="/site1">Flight</a></li> <li class="tag"><a rel="tag" class="term" href="/site2">Action</a></li> <li class="tag"><a rel="tag" class="term" href="/site3">Gravity</a></li> </ul> 变量“重置”到起始点(即speed)。

无需再次调用0,因为间隔中的下一个滴答声仍会调用此

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

var img = new Image();
img.src = "https://avatars2.githubusercontent.com/u/15933454?s=60&v=4";
var speed = 0;
var xpos = 800;
var CXpos = 0;  //changeing xpos
var result = Math.floor(Math.random() * 500) + 1;

function icon(){
    ctx.beginPath();
    speed -= 1;
    CXpos = xpos + speed;
    ctx.drawImage(img, CXpos, result, 60, 60);
    if(CXpos == 50){
        speed = 0;    //I don't know it's correct code?
        //icon();
    }
    ctx.closePath();
}

function iconypos(){
    var num = Math.floor(Math.random*100)+1
    return num;
}

function draw(){
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    icon();
}

setInterval(draw, 10);