canvas向下移动图片卡顿
发布于 5 年前 作者 taomao 18925 次浏览 来自 问答

startTimer: function() {

var that = this;

var w2 = that.data.w2;

var h2 = that.data.h2;

var w1 = that.data.w1;

var h1 = that.data.h1;

var w3 = that.data.w3;

ctx.clearRect(0, 0, w2, h2); //清除画布内容

ctx.rect(0, 0, w2, h2)

var x = 0,

y = 0;

var maxheight = h2;

var listitem = {

x: 0,

y: 0,

state: 0

}

var miny = {

i: 10,

min: 0

};

for (var i = 0; i < bird.length; i++) {

var blockData = bird[i].block;

var tmp = array.sort(function(a, b) {

return Math.random() > .5 ? -1 : 1;

});

for (var j = 0; j < blockData.length; j++) {

if (blockData[j].state == 2) { //点错

ctx.drawImage("/images/apple/apple" + blockData[j].img + “.png”, blockData[j].x, bird[i].h1, w1, h1);

listitem.x = blockData[j].x;

listitem.y = bird[i].h1; // blockData[j].y;

listitem.state = 2;

} else if (blockData[j].state == 1) {

if (blockData[j].num < 20) {

ctx.drawImage("/images/apple/animation" + blockData[j].num + “.png”, blockData[j].x, bird[i].h1, w1, h1);

blockData[j].num += 1;

} else {

ctx.drawImage("/images/apple/animation19.png", blockData[j].x, bird[i].h1, w1, h1);

}

} else {

ctx.drawImage("/images/apple/apple" + blockData[j].img + “.png”, blockData[j].x, bird[i].h1, w1, h1);

}

blockData[j].y = blockData[j].y + speed;

if (blockData[j].y > maxheight) {

blockData[j].y = speed - h1;

blockData[j].img = tmp[j];

blockData[j].state = 0;

blockData[j].num = 0;

miny.i = i;

}

}

bird[i].h1 = bird[i].h1 + speed;

bird[i].h2 = bird[i].h2 + speed;

if (miny.min == 0) {

miny.min = bird[i].h1;

} else if (miny.min > bird[i].h1) {

miny.min = bird[i].h1;

}

bird[i].block = blockData;

}

if (miny.i < 10) {

var blacklist = bird[miny.i].block;

bird[miny.i].h1 = miny.min - h1;

bird[miny.i].h2 = miny.min;

for (var j = 0; j < blacklist.length; j++) {

blacklist[j].y = miny.min - h1;

}

bird[miny.i].block = blacklist;

}

if (listitem.state == 2) {

ctx.beginPath();

ctx.rect(listitem.x, listitem.y, w1, h1);

ctx.fillStyle = ‘rgba(255,0,0,0.5)’;

ctx.fill()

ctx.closePath()

}

ctx.draw();

switch (that.data.time) {

case 25:

speed = 6;

break;

case 20:

speed = 8;

break;

case 15:

speed = 10;

break;

case 10:

speed = 12;

break;

case 5:

speed = 14;

break;

}

if (that.data.startrun == 1) {

setTimeout(that.startTimer, 37)

// requestAnimationFrame(that.startTimer)

}

},

ios卡顿比较明显

代码片段   wechatide://minicode/8jPWtHmh7WjI

1 回复

官方大神给个建议

回到顶部