我正在尝试绘制一个“无限”图,这个想法是我将通过将画布向左移动一个像素来重新绘制画布,然后清除最右边的列,将一个像素绘制到一个已清除的列中。但是有些东西不起作用,我无法理解是什么。下面的示例代码:
var telemetryScreen, telemetryTimer;
function displayUI() {
if (!telemetryScreen) {
telemetryScreen = document.createElement("div");
telemetryScreen.className = "telemetry-screen";
document.body.appendChild(telemetryScreen);
}
return telemetryScreen;
}
var plotFps = function(canvas) {
canvas.className = "telemetry-fps";
canvas.width = 320;
canvas.height = 240;
canvas.getContext("2d").fillStyle = "red";
if (!telemetryScreen) displayUI().appendChild(canvas);
return function (delta) {
var context = canvas.getContext("2d");
context.drawImage(canvas, 1, 0, 319, 240, 0, 0, 319, 240);
context.clearRect(319, 0, 1, 240);
context.fillRect(319, 120 + delta, 1, 1);
};
}(document.createElement("canvas"));
function startSampling() {
var now, delta, last, start = new Date().getTime(),
seconsPerFrame = 16;
if (telemetryTimer) clearInterval(telemetryTimer);
telemetryTimer = setInterval(
function () {
last = now;
now = new Date().getTime();
delta = now - last - seconsPerFrame;
plotFps(delta);
}, seconsPerFrame);
// We don't anticipate framerates
// higher then 60 fps
}
startSampling();
我也尝试过translate()
,但是在画布滚动超过其宽度之一后它停止工作:/