Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.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
Can';不能在HTML画布内进行翻译_Html_Canvas_Html5 Canvas - Fatal编程技术网

Can';不能在HTML画布内进行翻译

Can';不能在HTML画布内进行翻译,html,canvas,html5-canvas,Html,Canvas,Html5 Canvas,我有一个HTML画布,每次在左侧添加一个新像素时,我想将当前内容向右滚动1个像素。基本上是创建一个绘图图表效果 function draw() { ctx.save(); y += Math.floor((Math.random() * 10) - 5); ctx.fillStyle = 'blue'; ctx.translate(1, 0); ctx.restore(); ctx.fillRect(0,y,3,3); window.re

我有一个HTML画布,每次在左侧添加一个新像素时,我想将当前内容向右滚动1个像素。基本上是创建一个绘图图表效果

function draw()
{
    ctx.save();
    y += Math.floor((Math.random() * 10) - 5);
    ctx.fillStyle = 'blue';
    ctx.translate(1, 0);
    ctx.restore();
    ctx.fillRect(0,y,3,3);

    window.requestAnimationFrame(draw);
}

// init canvas
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
ctx.fillRect(0,0,canvas.width,canvas.height);

var y = 0;
draw();

由于某些原因,我没有在画布上获得任何视觉输出

不清楚您试图对保存和恢复执行什么操作…
在下面的代码中,我删除了它们,并将代码简化为只打印一行

函数绘图(){
y+=Math.sin(y*23);
ctx.fillStyle='蓝色';
ctx.translate(1,0);
fillRect(0,y++,3,3);
window.requestAnimationFrame(绘制);
}
//初始化画布
var canvas=document.getElementById('canvas');
var ctx=canvas.getContext('2d');
var y=0;
draw()

不清楚您试图对保存和恢复做什么…
在下面的代码中,我删除了它们,并将代码简化为只打印一行

函数绘图(){
y+=Math.sin(y*23);
ctx.fillStyle='蓝色';
ctx.translate(1,0);
fillRect(0,y++,3,3);
window.requestAnimationFrame(绘制);
}
//初始化画布
var canvas=document.getElementById('canvas');
var ctx=canvas.getContext('2d');
var y=0;
draw()

您应该在绘制后恢复,否则您的翻译将无法进行effect@trker我试过了,但它没有滚动,请尝试。是的,它只画了一行,因为您只调用了draw()一次。是否尝试使用超时或循环调用draw()。@trker仔细查看函数draw,最后一行是:
window.requestAnimationFrame(draw)这是创建循环的回调,请在此处阅读更多相关内容:没错,我忘记了requestanimframe是如何工作的绘制后您应该恢复,否则您的翻译将无法完成effect@trker我试过了,但它没有滚动,请尝试。是的,它只画了一行,因为您只调用了draw()一次。是否尝试使用超时或循环调用draw()。@trker仔细查看函数draw,最后一行是:
window.requestAnimationFrame(draw)这是创建循环的回调,请在此处阅读更多相关内容:没错,我忘记了requestanimframe是如何工作的