【发布时间】:2014-04-15 12:55:53
【问题描述】:
我是个菜鸟,我正在使用 HTML5 Canvas 和 JS 制作“素数”绘图/动画。我很难使用“for”循环,因为浏览器仅在“for”循环完成后才显示绘图,而不是在它工作时(显示每个循环的输出,如动画)。
HTML/css:
<!doctype html>
<html>
<head>
</head>
<style>
body
{
background: #999999
}
#canvas
{
background: white;
border: 19px solid black;
}
</style>
<body>
<canvas id="canvas" width="4000" height="4000">
Bájate un nuevo navegador!!
</canvas>
<script src="jscode.js"></script>
</body>
</html>
JS (jscode.js):
var canvas1 = document.getElementById('canvas');
var context = canvas.getContext ('2d');
// Text properties
context.font = '32pt Arial';
context.fillStyle = 'DeepSkyBlue';
context.StrokeStyle = 'DarkSlateGray';
// Line properties
context.StrokeStyle = "rgb (200,0,0)";
context.lineWidth = 1;
// Each Natural number is represented by a segment
// "t" corresponds to the length of each segment.
var t = 3;
// Starting position within the canvas.
var x = 500;
var y = 500;
//Array stores every movement's direction.
//(We need to know the last segment direction in order "continue" the drawing)
var Dmemory = new Array();
// Drawing using a Path function.
// Drawing "manually".
context.beginPath();
//0
context.moveTo(x,y);
Dmemory [0] = "A";
//1
/*UP*/ var y = y - t;
context.lineTo(x,y);
Dmemory [1] = "A";
// Continue using a "for" loop.
// Variable "num" is the LIMIT. The program will run until the loop gets to this number.
var num = 10000;
// The program begins with 1 and starts counting adding 1+.
for (i=2 ; i < num ; i++ )
{
//Creating a "Divisors tracking variable".
//We are going to divide each number between all the natural numbers below to verify if its a prime.
var contador = 0;
for ( div = i ; div > 0 ; div--)
{
if (i%div == 0)
{
contador = contador + 1;
}
//Breaking condition: More than 2 divisors.
if (contador>2)
{
break;
}
}
//Primes Test: Primes have ONLY two divisors: Themselves and number One
if (contador == 2)
{
//If Prime: check the last movements direction and proceed.
switch (Dmemory[i - 1])
{
case "A":
var y = y - t;
context.lineTo(x,y);
context.stroke();
Dmemory[i]="D";
break;
case "B":
var y = y + t;
context.lineTo(x,y);
context.stroke();
Dmemory[i]="I";
break;
case "D":
var x = x + t;
context.lineTo(x,y);
context.stroke();
Dmemory[i]="B";
break;
case "I":
var x = x - t;
context.lineTo(x,y);
context.stroke();
Dmemory[i]="A";
}
}
else
{
//If not Prime: check the last movement direction and proceed.
switch (Dmemory[i - 1])
{
case "A":
var y = y - t;
context.lineTo(x,y);
context.stroke();
Dmemory[i]="A";
break;
case "B":
var y = y + t;
context.lineTo(x,y);
context.stroke();
Dmemory[i]="B";
break;
case "D":
var x = x + t;
context.lineTo(x,y);
context.stroke();
Dmemory[i]="D";
break;
case "I":
var x = x - t;
context.lineTo(x,y);
context.stroke();
Dmemory[i]="I";
break;
}
}
}
我一直在阅读其他答案,现在我明白了主要原因:网络浏览器中的 JavaScript 是单线程的。
但是,有什么方法可以完成我需要的吗? (我需要的是:随着循环的进行在屏幕上显示绘图)
提前谢谢你,
来自利马的问候 - 秘鲁
【问题讨论】:
标签: html loops canvas for-loop