繁体   English   中英

HTML5 canvas javascript问题延迟

[英]HTML5 canvas javascript problems in delay

我试图在绘制图形时进行延迟,但是我认为setTimeout遇到了麻烦。 任何帮助或建议,将不胜感激。

<canvas id="myCanvas"></canvas>
<script type="text/javascript">
function vertix(y,ctx){

  ctx.moveTo(0, y);
  ctx.lineTo(500, y);
}
function horizons(x,ctx){
    ctx.moveTo(x, 0);
    ctx.lineTo(x, 375);
}
var canvas=document.getElementById('myCanvas');
var ctx=canvas.getContext('2d');
var context=ctx;
var i;
ctx.fillStyle='#FF0000';
canvas.setAttribute('align', 'center');
canvas.setAttribute('width', '800px');
canvas.setAttribute('height', '800px'); // clears the canvas

for (var x = 0.5; x < 500; x += 10) {
    setTimeout("horizons(x,ctx)",1000,'JavaScript');
}
for (var y = 0.5; y < 375; y += 10) {
    setTimeout("vertix(y,ctx)",1000,'JavaScript');
}
ctx.strokeStyle = "#eee";
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, 40);
ctx.lineTo(240, 40);
ctx.moveTo(260, 40);
ctx.lineTo(500, 40);
ctx.moveTo(495, 35);
ctx.lineTo(500, 40);
ctx.lineTo(495, 45);
ctx.moveTo(60, 0);
ctx.lineTo(60, 153);
ctx.moveTo(60, 173);
ctx.lineTo(60, 375);
ctx.moveTo(65, 370);
ctx.lineTo(60, 375);
ctx.lineTo(55, 370);
ctx.strokeStyle = "#000";
ctx.stroke();
context.font = "bold 12px sans-serif";
context.fillText("x", 248, 43);
context.fillText("y", 58, 165);
context.font = "bold 12px sans-serif";
context.fillText("x", 248, 43);
context.fillText("y", 58, 165);
context.textBaseline = "top";
context.fillText("( 0 , 0 )", 8, 5);
context.textAlign = "right";
context.textBaseline = "bottom";
context.fillText("( 500 , 375 )", 492, 370);
context.fillRect(0, 0, 3, 3);
context.fillRect(497, 372, 3, 3);

context.font = " 'TangerineRegular'";
context.fillText("Graph", 500, 400);
</script>

您有两个基本问题:

  1. 使用基于字符串的超时处理程序将不起作用,因为xy (和ctx )参数不在范围内

  2. setTimeout不会延迟执行所有操作,它只是意味着“在将来的某个时间执行此操作 ,同时继续进行下去”。

我在http://jsfiddle.net/alnitak/tGv4x/上创建了一个小提琴,它显示了如何解决此问题。 它的核心是:

var hor_x = 0.5;
var vert_y = 0.5;
var delay = 100;

function vertix() {
    ctx.beginPath();
    ctx.moveTo(0, vert_y);
    ctx.lineTo(500, vert_y);
    ctx.stroke();
    if (vert_y < 375) {
        vert_y += 10;
        setTimeout(vertix, delay);
    }
}

function horizons() {
    ctx.beginPath();
    ctx.moveTo(hor_x, 0);
    ctx.lineTo(hor_x, 375);
    ctx.stroke();
    if (hor_x < 500) {
        hor_x += 10;
        setTimeout(horizons, delay);
    } else {
        setTimeout(vertix, delay);
    }
}

horizons();

本质上-使用外部作用域变量存储当前坐标,调用单个函数( horizons )重复触发自身。 一旦完成,它将控制权移交给另一功能( vertix ),该功能对另一根轴执行相同的操作。

试试这个它的工作:

<canvas id="myCanvas"></canvas>
<script>
var canvas=document.getElementById('myCanvas');
var ctx=canvas.getContext('2d');
var context=ctx;
var i;

function vertix(y){
  ctx.moveTo(0, y);
  ctx.lineTo(500, y);
}
function horizons(x){
    ctx.moveTo(x, 0);
    ctx.lineTo(x, 375);
}


ctx.fillStyle='#FF0000';
canvas.setAttribute('align', 'center');
canvas.setAttribute('width', '800px');
canvas.setAttribute('height', '800px'); // clears the canvas

for (var x = 0.5; x < 500; x += 10) {        
 horizons(x)
}
for (var y = 0.5; y < 375; y += 10) {
 vertix(y)
}

ctx.strokeStyle = "#eee";
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, 40);
ctx.lineTo(240, 40);
ctx.moveTo(260, 40);
ctx.lineTo(500, 40);
ctx.moveTo(495, 35);
ctx.lineTo(500, 40);
ctx.lineTo(495, 45);
ctx.moveTo(60, 0);
ctx.lineTo(60, 153);
ctx.moveTo(60, 173);
ctx.lineTo(60, 375);
ctx.moveTo(65, 370);
ctx.lineTo(60, 375);
ctx.lineTo(55, 370);
ctx.strokeStyle = "#000";
ctx.stroke();
context.font = "bold 12px sans-serif";
context.fillText("x", 248, 43);
context.fillText("y", 58, 165);
context.font = "bold 12px sans-serif";
context.fillText("x", 248, 43);
context.fillText("y", 58, 165);
context.textBaseline = "top";
context.fillText("( 0 , 0 )", 8, 5);
context.textAlign = "right";
context.textBaseline = "bottom";
context.fillText("( 500 , 375 )", 492, 370);
context.fillRect(0, 0, 3, 3);
context.fillRect(497, 372, 3, 3);

context.font = " 'TangerineRegular'";
context.fillText("Graph", 500, 400);

</script>
<canvas id="myCanvas"></canvas>
<script type="text/javascript">
var hor_x = 0.5;
var vert_y = 0.5;
var delay = 100;
function vertix() {
    ctx.beginPath();
    ctx.moveTo(0, vert_y);
    ctx.lineTo(500, vert_y);
    ctx.stroke();
    if (vert_y < 375) {
        vert_y += 10;
        setTimeout(vertix, delay);
    }
}

function horizons() {
    ctx.beginPath();
    ctx.moveTo(hor_x, 0);
    ctx.lineTo(hor_x, 375);
    ctx.stroke();
    if (hor_x < 500) {
        hor_x += 10;
        setTimeout(horizons, delay);
    } else {
        setTimeout(vertix, delay);
    }
}
/*
function vertix(y,ctx){

  ctx.moveTo(0, y);
  ctx.lineTo(500, y);
}
function horizons(x,ctx){
    ctx.moveTo(x, 0);
    ctx.lineTo(x, 375);
}*/
var canvas=document.getElementById('myCanvas');
var ctx=canvas.getContext('2d');
var context=ctx;
var i;
ctx.fillStyle='#FF0000';
canvas.setAttribute('align', 'center');
canvas.setAttribute('width', '800px');
canvas.setAttribute('height', '800px'); // clears the canvas
horizons();
vertix();
/*
for (var x = 0.5; x < 500; x += 10) {
    setTimeout("horizons(x,ctx)",1000,'JavaScript');
}
for (var y = 0.5; y < 375; y += 10) {
    setTimeout("vertix(y,ctx)",1000,'JavaScript');
}*/
ctx.strokeStyle = "#eee";
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, 40);
ctx.lineTo(240, 40);
ctx.moveTo(260, 40);
ctx.lineTo(500, 40);
ctx.moveTo(495, 35);
ctx.lineTo(500, 40);
ctx.lineTo(495, 45);
ctx.moveTo(60, 0);
ctx.lineTo(60, 153);
ctx.moveTo(60, 173);
ctx.lineTo(60, 375);
ctx.moveTo(65, 370);
ctx.lineTo(60, 375);
ctx.lineTo(55, 370);
ctx.strokeStyle = "#000";
ctx.stroke();
context.font = "bold 12px sans-serif";
context.fillText("x", 248, 43);
context.fillText("y", 58, 165);
context.font = "bold 12px sans-serif";
context.fillText("x", 248, 43);
context.fillText("y", 58, 165);
context.textBaseline = "top";
context.fillText("( 0 , 0 )", 8, 5);
context.textAlign = "right";
context.textBaseline = "bottom";
context.fillText("( 500 , 375 )", 492, 370);
context.fillRect(0, 0, 3, 3);
context.fillRect(497, 372, 3, 3);

context.font = " 'TangerineRegular'";
context.fillText("Graph", 500, 400);
</script>

它完成了我想要的程度

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM