我目前正在编写 JavaScript,它会随机生成带有在我的表单框值中输入的框数的框,并使它们旋转等。
<input>
加载 JavaScript 时,我点击 start() 时生成的框是标签中设置的默认值。如果我更改脚本中的值并刷新出现的新框数,否则如果我使用浏览器更改值并单击开始一个 gizillion 时间它只会刷新画布但一切都保持在原位,换句话说,相同即使我减少或增加它们的数量,框也会出现在同一个地方:
<!DOCTYPE HTML>
<html>
<head><title>Rectangles</title></head>
<body>
<div id="rectangles" STYLE = "background-color: #fff68f; width: 600px; height: 420px;
border-style: outset;position: absolute;">
<form name="pGame">
<center>
<b>Canvas Application</b><br>
<input type= "button" value= "Start" onclick= "start()" />
<input id="box" type="text" size="2" value="20" style="border-style:inset;
color:red; background-color: black" />
</center>
</form>
<script>
var shouldClear = 0;
这是我认为所有重置应该在的地方:
function start()
{
//tried making a counter, if its the first time to click save() context
// else restore
// context.restore(); but it didnt work, I think its because canvas and context
//arent declared here, but if I declare them in this scope or as global variable i get an error saying:Cannot call method 'getContext' of null
if (shouldClear<=0) {
setInterval(rotateShape, 250);
var degrees = 0.0;
shouldClear += 1;
}
if (shouldClear==1){
var canvas = document.getElementById('gameId');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, 640, 480);
for (i=0; i < allShapes.length; ++i) {
allShapes[i].draw(ctx);
}
if (shouldClear==1) {
ctx.clearRect(0, 0, 640, 480);
construct();
//shouldClear= shouldClear - 10;
}
}
}
}
var chooseColor =0;
function construct()
{
var RotatingRectangle = function(x, y, width, height, rot, r,g,b){
var rotation = rot;
var rotationState = 0;
this.draw = function(ctx){
rotationState += rotation;
ctx.save();
ctx.translate(x+(width/2), y+(height/2));
ctx.rotate(rotationState);
var randomRed = Math.floor( Math.random() * 256 );
var randomGreen = Math.floor( Math.random() * 256 );
var randomBlue = Math.floor( Math.random() * 256 );
ctx.fillStyle = "rgb(" + randomRed + ", " + randomGreen + ", " + randomBlue +")";
ctx.fillRect(0-(width/2), 0-(height/2), width, height);
ctx.restore();
}
}
var count = parseInt(pGame.box.value);
var allShapes = [];
for (i=0; i < count; ++i) {
if (chooseColor == 0) {
var rotation = Math.random()*.10
var x = Math.floor(Math.random() * 640);
var y = Math.floor(Math.random() * 480);
var randomRed = Math.floor( Math.random() * 256 );
var randomGreen = Math.floor( Math.random() * 256 );
var randomBlue = Math.floor( Math.random() * 256 );
var rect = new RotatingRectangle(x, y, 15 + (Math.random() * 50), 15 + (Math.random() * 30), rotation, "rgb(" + randomRed + ", " + randomGreen + ", " + randomBlue +")");
allShapes.push(rect);
}
}
chooseColor = 1;
return allShapes;
}
var allShapes = construct();
function rotateShape()
{
var canvas = document.getElementById('gameId');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, 640, 480);
for (i=0; i < allShapes.length; ++i) {
allShapes[i].draw(ctx);
}
if (shouldClear==1) {
ctx.clearRect(0, 0, 640, 480);
for (i=0; i < allShapes.length; ++i) {
allShapes[i].draw(ctx);
}
}
}
}
</script>
<canvas id="gameId" width="598" height="300"
STYLE = "border:1px solid; background-color: #fff68f; position: absolute;">
</canvas>
<div style="font-size:12; position: relative; text-align: center; top: 320px;">
Copyright © 2005 <span style="font-family: cursive;">do it@knowHow</span></div>
</div>
</body>
</html>