탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3165 · 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-04-06 조회 3,061
12-04-06 조회 3,267
12-04-05 조회 3,693
12-04-05 조회 3,084
12-04-05 조회 4,733
12-04-05 조회 3,386
12-04-05 조회 4,068
12-04-04 조회 4,357
12-04-04 조회 4,001
12-04-03 조회 3,331
12-04-03 조회 3,906
12-04-03 조회 3,214
12-04-03 조회 2,948
12-04-02 조회 3,656
12-04-02 조회 3,361
12-04-01 조회 3,283
12-04-01 조회 3,118
12-04-01 조회 3,353
12-03-31 조회 3,201
12-03-31 조회 3,679
12-03-30 조회 3,286
12-03-30 조회 2,932
12-03-30 조회 3,602
12-03-30 조회 3,268
12-03-30 조회 3,955
12-03-30 조회 3,399
12-03-29 조회 6,153
12-03-29 조회 3,960
12-03-29 조회 4,693
12-03-29 조회 3,162
12-03-28 조회 2,762
12-03-28 조회 3,478
12-03-28 조회 4,403
12-03-28 조회 3,198
12-03-27 조회 4,694
12-03-27 조회 3,106
12-03-27 조회 3,495
12-03-26 조회 4,176
12-03-26 조회 3,565
12-03-26 조회 3,141
12-03-26 조회 3,872
12-03-26 조회 6,234
12-03-25 조회 2,824
12-03-25 조회 2,970
12-03-24 조회 3,043
12-03-24 조회 6,503
12-03-24 조회 3,672
12-03-24 조회 3,929
12-03-23 조회 3,883
12-03-23 조회 3,071
12-03-23 조회 2,955
12-03-23 조회 3,097
12-03-22 조회 3,949
12-03-22 조회 3,341
12-03-22 조회 3,587
12-03-22 조회 4,807
12-03-22 조회 3,289
12-03-22 조회 3,752
12-03-22 조회 3,640
12-03-22 조회 2,921
12-03-22 조회 5,833
12-03-22 조회 3,571
12-03-22 조회 5,124
12-03-21 조회 3,347
12-03-21 조회 3,140
12-03-21 조회 3,245
12-03-21 조회 2,936
12-03-21 조회 3,646
12-03-21 조회 3,629
12-03-21 조회 4,970
12-03-21 조회 6,514
12-03-21 조회 2,894
12-03-20 조회 5,700
12-03-20 조회 2,887
12-03-20 조회 3,410
12-03-20 조회 3,157
12-03-20 조회 3,417
12-03-20 조회 4,167
12-03-19 조회 3,637
12-03-19 조회 2,867
12-03-19 조회 3,559
12-03-18 조회 3,287
12-03-17 조회 3,601
12-03-17 조회 4,184
12-03-17 조회 3,405
12-03-16 조회 3,025
12-03-16 조회 4,181
12-03-16 조회 3,417
12-03-15 조회 3,030
12-03-15 조회 3,109
12-03-15 조회 3,429
12-03-15 조회 2,835
12-03-14 조회 4,053
12-03-14 조회 3,307
12-03-14 조회 3,372
12-03-14 조회 3,015
12-03-13 조회 3,670
12-03-13 조회 4,536
12-03-13 조회 2,889
12-03-13 조회 2,824