Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS循环使用Canvas绘制导致性能下降。 #676

Closed
neeboo opened this issue Sep 12, 2021 · 4 comments · Fixed by #679
Closed

JS循环使用Canvas绘制导致性能下降。 #676

neeboo opened this issue Sep 12, 2021 · 4 comments · Fixed by #679
Assignees
Labels
bug Something isn't working
Projects

Comments

@neeboo
Copy link

neeboo commented Sep 12, 2021

使用的 Kraken 版本 | What version of kraken are you using

master v0.8

重现步骤 | Steps To Reproduce

在Loop场景下(比如游戏?),JS使用Canvas绘制导致性能下降。

重现代码 | Code example:

const canvas = document.createElement('canvas');
canvas.width = screen.width;
canvas.height = screen.height;
const screenHeight = screen.height;
const screenWidth = screen.width;
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
const img = document.createElement('img');
// 资源图片,随便一张都可以
img.src = 'images/bg.png';

function loop(context, top) {
       // 清除上一帧
	context.clearRect(0, 0, screenWidth, screenHeight);

       //主图绘制
      context.drawImage(img, 0, -screenHeight + top, canvas.width, canvas.height);
      context.drawImage(img, 0, top, canvas.width, canvas.height);

       // 位移
	var nextTop = top + 2;
	if (nextTop >= screenHeight) {
		nextTop = 0;
	}
	
       // 帧重绘,用setTimeout也是一样
       requestAnimationFrame((some) => loop(context, nextTop));
}

// 开始循环
loop(ctx,0);

预期结果 | Expected results:

  1. 帧率稳定,不卡UI
  2. 不会崩溃

实际结果 | Actual results:

  1. 帧数迅速下降
  2. 一定时间后客户端崩溃
@neeboo neeboo added the bug Something isn't working label Sep 12, 2021
@andycall
Copy link
Member

@wssgcg1213 先看下原因是为啥

@wssgcg1213
Copy link
Member

Raster 的耗时看起来在叠加

image

@andycall
Copy link
Member

会不会是图片被重复绘制了

@wssgcg1213
Copy link
Member

wssgcg1213 commented Sep 13, 2021

感谢反馈, 问题在 canvas_painter.dart 里

Picture 是个指令收集器, 使用 picture 作为 snapshot 实质上只是让 UI 线程(dart vm)的指令扁平化了, 但是 c++ engine 侧的指令还在累计; 我这边把 Picture 转换为 Image 作为 snapshot 可以解决这个问题, 稍后提一个 PR

@andycall andycall added this to To Do in Performance via automation Sep 13, 2021
Performance automation moved this from To Do to Done Sep 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

Successfully merging a pull request may close this issue.

3 participants