탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3155 · 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,187건
+
제목 글쓴이 날짜 조회
12-03-13 조회 3,020
12-03-12 조회 5,608
12-03-12 조회 3,090
12-03-12 조회 3,742
12-03-12 조회 3,489
12-03-12 조회 3,055
12-03-12 조회 1만
12-03-12 조회 3,423
12-03-12 조회 4,061
12-03-12 조회 3,043
12-03-12 조회 3,416
12-03-12 조회 3,310
12-03-12 조회 4,242
12-03-12 조회 5,854
12-03-12 조회 3,830
12-03-12 조회 5,648
12-03-12 조회 3,860
12-03-12 조회 6,266
12-03-11 조회 3,752
12-03-11 조회 3,197
12-03-11 조회 4,133
12-03-11 조회 4,941
12-03-11 조회 3,836
12-03-11 조회 3,811
12-03-11 조회 5,484
12-03-10 조회 3,489
12-03-10 조회 3,071
12-03-10 조회 3,980
12-03-10 조회 3,095
12-03-09 조회 7,442
12-03-09 조회 3,185
12-03-09 조회 3,591
12-03-09 조회 3,188
12-03-09 조회 3,202
12-03-09 조회 3,404
12-03-09 조회 4,178
12-03-08 조회 3,868
12-03-08 조회 3,756
12-03-08 조회 4,029
12-03-08 조회 6,149
12-03-08 조회 3,068
12-03-08 조회 4,148
12-03-08 조회 4,053
12-03-07 조회 2,968
12-03-07 조회 3,127
12-03-07 조회 2,804
12-03-06 조회 1.1만
12-03-06 조회 3,272
12-03-06 조회 4,322
12-03-06 조회 3,866
12-03-06 조회 3,207
12-03-06 조회 2,957
12-03-06 조회 6,378
12-03-06 조회 3,042
12-03-05 조회 3,136
12-03-04 조회 4,353
12-03-03 조회 3,733
12-03-03 조회 3,867
12-03-03 조회 3,411
12-03-03 조회 3,384
12-03-02 조회 3,204
12-03-02 조회 3,499
12-03-02 조회 3,339
12-03-02 조회 4,277
12-03-02 조회 3,436
12-03-02 조회 5,182
12-03-02 조회 3,058
12-03-02 조회 4,707
12-03-01 조회 5,217
12-03-01 조회 6,560
12-03-01 조회 3,369
12-02-29 조회 3,583
12-02-29 조회 4,061
12-02-29 조회 3,686
12-02-29 조회 3,719
12-02-29 조회 5,499
12-02-28 조회 3,655
12-02-28 조회 3,484
12-02-28 조회 3,647
12-02-28 조회 3,624
12-02-28 조회 3,250
12-02-27 조회 4,490
12-02-27 조회 4,345
12-02-27 조회 4,107
12-02-27 조회 3,545
12-02-27 조회 3,341
12-02-27 조회 3,068
12-02-27 조회 4,244
12-02-27 조회 5,162
12-02-27 조회 3,908
12-02-26 조회 4,162
12-02-26 조회 3,587
12-02-26 조회 3,623
12-02-26 조회 3,774
12-02-26 조회 3,488
12-02-26 조회 3,404
12-02-26 조회 3,349
12-02-26 조회 3,705
12-02-26 조회 3,821
12-02-26 조회 3,449