탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3034 · 2
<script>
   function nextRandomInteger(limit) {
     return Math.round(Math.random() * limit);
   }
   var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
   function randomAlphabet() {
     return alphabet.charAt(nextRandomInteger(25));
   }
   function randomSpeed(maxSpeed) {
     return Math.random() * maxSpeed - Math.random() * maxSpeed;
   }
  var canvasWidth = 700;
  var canvasHeight = 500;
  function MovingText() {
  this.x = nextRandomInteger(canvasWidth);
  this.y = nextRandomInteger(canvasHeight);
  this.vX = randomSpeed(10);
  this.vY = randomSpeed(10);
  this.body = document.createElement('h1');
  this.body.innerHTML = randomAlphabet();
  this.body.style.position = 'absolute';
 
  MovingText.prototype.move = function () {
  }
    if (this.x < 0 || this.x > canvasWidth) {this.vX *= -1;}
    if (this.y < 0 || this.y > canvasHeight) {this.vY *= -1;}
   
    this.x += this.vX;
    this.y += this.vY;
    this.body.style.left = this.x + 'px';
    this.body.style.top = this.y + 'px';
   };
   window.onload = function () {
  var movingTexts = [];
  for (var i = 0; i < 100; i++) {
     movingTexts.push(new MovingText());
     }
  setInterval(function () {
     for (var i in movingTexts) {
     movingTexts[i].move();
  }
     }, 1000 / 60);
    };
</script>
 
오류도 없는데 빈화면으로밖에 나오질않네요...
뭐가문제인가요?
|

댓글 2개

소스가 약간씩 모자라거나 열고 닫는 부분이 잘못 되어있네요.
소스를 작성하실때 function 의 시작과 끝을 잘 파악하셔야 하고 무조건 따라 작성하시기 보다는 코드의 흐름을 파악하시고 작성하시는것이 좋을 것 같습니다.

완성소스는 아래와 같습니다.
(참고문헌 : 모던 웹을 위한 javascript jQuery 입문 (윤인성 저) )

<!DOCTYPE html>
<html>
<head>
<script>
function nextRandomInteger(limit) {
return Math.round(Math.random() * limit);
}

var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
function randomAlphabet() {
return alphabet.charAt(nextRandomInteger(25));
}

function randomSpeed(maxSpeed) {
return Math.random() * maxSpeed - Math.random() * maxSpeed;
}
</script>
<script>

var canvasWidth = 700;
var canvasHeight = 500;
function MovingText() {
this.x = nextRandomInteger(canvasWidth);
this.y = nextRandomInteger(canvasHeight);
this.vX = randomSpeed(10);
this.vY = randomSpeed(10);

this.body = document.createElement('h1');
this.body.innerHTML = randomAlphabet();
this.body.style.position = 'absolute';

document.body.appendChild(this.body);
}

MovingText.prototype.move = function () {
if (this.x < 0 || this.x > canvasWidth) {this.vX *= -1;}
if (this.y < 0 || this.y > canvasHeight) {this.vY *= -1;}

this.x += this.vX;
this.y += this.vY;

this.body.style.left = this.x + 'px';
this.body.style.top = this.y + 'px';
};
</script>
<script>
window.onload = function () {
var movingTexts = [];
for (var i = 0; i < 100; i++) {
movingTexts.push(new MovingText());
}
setInterval(function () {
for (var i in movingTexts) {
movingTexts[i].move();
}
}, 1000 / 60);
};
</script>
</head>
<body>
</body>
</html>
감사합니다!
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
12-04-24 조회 2,541
12-04-24 조회 2,571
12-04-24 조회 3,728
12-04-24 조회 2,766
12-04-24 조회 3,185
12-04-24 조회 2,500
12-04-24 조회 3,129
12-04-24 조회 2,658
12-04-24 조회 2,570
12-04-24 조회 2,813
12-04-24 조회 4,493
12-04-23 조회 2,927
12-04-23 조회 3,665
12-04-23 조회 2,555
12-04-23 조회 2,573
12-04-23 조회 2,871
12-04-23 조회 2,536
12-04-23 조회 3,457
12-04-23 조회 2,448
12-04-23 조회 2,518
12-04-23 조회 2,879
12-04-23 조회 2,655
12-04-23 조회 2,533
12-04-23 조회 2,657
12-04-23 조회 2,716
12-04-23 조회 2,476
12-04-23 조회 4,389
12-04-23 조회 3,513
12-04-23 조회 3,367
12-04-22 조회 2,520
12-04-22 조회 2,476
12-04-22 조회 2,565
12-04-22 조회 2,784
12-04-22 조회 2,576
12-04-22 조회 3,179
12-04-22 조회 3,232
12-04-22 조회 2,840
12-04-21 조회 3,062
12-04-21 조회 4,025
12-04-21 조회 4,454
12-04-21 조회 7,693
12-04-21 조회 2,609
12-04-21 조회 3,029
12-04-21 조회 2,519
12-04-21 조회 2,759
12-04-20 조회 3,553
12-04-20 조회 3,074
12-04-20 조회 2,725
12-04-20 조회 2,815
12-04-20 조회 2,602
12-04-20 조회 2,727
12-04-20 조회 3,846
12-04-20 조회 4,055
12-04-20 조회 2,893
12-04-20 조회 3,150
12-04-20 조회 4,242
12-04-20 조회 3,193
12-04-19 조회 3,247
12-04-19 조회 2,864
12-04-18 조회 4,319
12-04-18 조회 2,791
12-04-18 조회 3,035
12-04-17 조회 3,298
12-04-17 조회 3,462
12-04-17 조회 5,066
12-04-17 조회 5,343
12-04-17 조회 2,794
12-04-17 조회 3,292
12-04-16 조회 2,750
12-04-16 조회 3,446
12-04-16 조회 3,149
12-04-16 조회 3,038
12-04-16 조회 3,359
12-04-16 조회 2,653
12-04-16 조회 4,255
12-04-15 조회 3,849
12-04-14 조회 3,549
12-04-14 조회 2,928
12-04-14 조회 3,583
12-04-14 조회 3,643
12-04-13 조회 3,270
12-04-13 조회 4,455
12-04-13 조회 3,075
12-04-13 조회 2,773
12-04-13 조회 2,982
12-04-13 조회 6,051
12-04-12 조회 4,681
12-04-12 조회 3,210
12-04-11 조회 3,432
12-04-10 조회 4,910
12-04-10 조회 3,845
12-04-10 조회 2,967
12-04-09 조회 3,019
12-04-09 조회 4,597
12-04-09 조회 2,979
12-04-08 조회 3,282
12-04-07 조회 2,616
12-04-06 조회 2,775
12-04-06 조회 3,516
12-04-06 조회 5,295