탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3162 · 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-02-11 조회 3,184
12-02-11 조회 3,069
12-02-11 조회 3,511
12-02-10 조회 1만
12-02-10 조회 3,668
12-02-10 조회 4,629
12-02-09 조회 4,002
12-02-09 조회 3,559
12-02-09 조회 6,498
12-02-09 조회 3,210
12-02-08 조회 3,654
12-02-08 조회 3,055
12-02-08 조회 3,117
12-02-08 조회 4,064
12-02-08 조회 4,774
12-02-08 조회 3,109
12-02-07 조회 3,437
12-02-07 조회 3,373
12-02-07 조회 3,382
12-02-07 조회 2,684
12-02-07 조회 3,431
12-02-07 조회 2,977
12-02-06 조회 3,154
12-02-06 조회 3,048
12-02-06 조회 3,610
12-02-06 조회 4,046
12-02-06 조회 3,394
12-02-06 조회 1.2만
12-02-06 조회 4,324
12-02-06 조회 5,770
12-02-05 조회 2,854
12-02-05 조회 3,632
12-02-05 조회 3,035
12-02-04 조회 2,956
12-02-04 조회 2,947
12-02-04 조회 6,778
12-02-03 조회 3,954
12-02-03 조회 4,345
12-02-03 조회 2,847
12-02-03 조회 3,390
12-02-03 조회 3,974
12-02-02 조회 3,014
12-02-02 조회 3,786
12-02-02 조회 3,819
12-02-02 조회 5,485
12-02-02 조회 3,440
12-02-02 조회 4,700
12-02-02 조회 3,552
12-02-01 조회 3,376
12-02-01 조회 3,622
12-02-01 조회 4,052
12-02-01 조회 3,339
12-02-01 조회 2,887
12-01-31 조회 3,312
12-01-31 조회 3,562
12-01-31 조회 2,962
12-01-30 조회 9,736
12-01-30 조회 8,903
12-01-30 조회 2,879
12-01-30 조회 3,295
12-01-30 조회 3,384
12-01-30 조회 2,894
12-01-30 조회 3,059
12-01-29 조회 3,385
12-01-29 조회 3,247
12-01-29 조회 4,711
12-01-29 조회 2,892
12-01-29 조회 3,601
12-01-29 조회 3,892
12-01-28 조회 3,513
12-01-28 조회 2,745
12-01-28 조회 3,559
12-01-27 조회 2,950
12-01-27 조회 2,865
12-01-27 조회 2,910
12-01-27 조회 3,357
12-01-26 조회 6,651
12-01-26 조회 3,712
12-01-26 조회 3,495
12-01-26 조회 3,026
12-01-26 조회 3,125
12-01-25 조회 3,200
12-01-25 조회 3,003
12-01-25 조회 2,870
12-01-25 조회 2,678
12-01-25 조회 2,547
12-01-25 조회 4,279
12-01-25 조회 4,147
12-01-25 조회 2,752
12-01-25 조회 4,112
12-01-25 조회 3,590
12-01-24 조회 3,304
12-01-24 조회 3,508
12-01-24 조회 3,724
12-01-24 조회 4,427
12-01-22 조회 3,520
12-01-22 조회 2,863
12-01-22 조회 3,276
12-01-22 조회 3,767
12-01-21 조회 3,122