탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3173 · 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건
+
제목 글쓴이 날짜 조회
11-12-23 조회 4,144
11-12-23 조회 3,072
11-12-23 조회 4,898
11-12-23 조회 4,485
11-12-23 조회 2,830
11-12-23 조회 8,164
11-12-22 조회 3,152
11-12-22 조회 3,889
11-12-22 조회 3,637
11-12-22 조회 3,545
11-12-22 조회 3,743
11-12-21 조회 3,596
11-12-21 조회 3,378
11-12-21 조회 3,677
11-12-21 조회 4,061
11-12-21 조회 3,088
11-12-21 조회 3,580
11-12-21 조회 4,507
11-12-21 조회 4,327
11-12-21 조회 3,355
11-12-20 조회 3,827
11-12-19 조회 6,322
11-12-19 조회 2,996
11-12-19 조회 5,074
11-12-17 조회 3,935
11-12-17 조회 4,307
11-12-17 조회 3,590
11-12-16 조회 3,201
11-12-16 조회 3,372
11-12-15 조회 3,431
11-12-15 조회 3,540
11-12-15 조회 4,350
11-12-15 조회 5,645
11-12-15 조회 4,623
11-12-15 조회 3,292
11-12-15 조회 4,071
11-12-15 조회 4,241
11-12-14 조회 2,995
11-12-14 조회 4,418
11-12-14 조회 5,663
11-12-13 조회 4,236
11-12-13 조회 5,130
11-12-13 조회 6,141
11-12-13 조회 6,434
11-12-13 조회 4,834
11-12-12 조회 3,348
11-12-12 조회 3,651
11-12-12 조회 6,321
11-12-12 조회 4,717
11-12-12 조회 3,865
11-12-12 조회 6,217
11-12-11 조회 4,746
11-12-10 조회 2,857
11-12-10 조회 4,795
11-12-09 조회 2,889
11-12-09 조회 3,311
11-12-09 조회 4,229
11-12-09 조회 3,835
11-12-09 조회 4,051
11-12-09 조회 3,597
11-12-09 조회 4,548
11-12-09 조회 3,239
11-12-09 조회 3,385
11-12-09 조회 3,056
11-12-09 조회 3,870
11-12-08 조회 3,641
11-12-08 조회 3,603
11-12-08 조회 7,904
11-12-08 조회 4,221
11-12-08 조회 3,711
11-12-08 조회 2,943
11-12-08 조회 1.2만
11-12-08 조회 4,029
11-12-07 조회 2,898
11-12-07 조회 2,930
11-12-07 조회 3,978
11-12-07 조회 4,586
11-12-07 조회 4,047
11-12-07 조회 3,144
11-12-07 조회 4,463
11-12-06 조회 4,374
11-12-06 조회 5,716
11-12-06 조회 3,770
11-12-06 조회 3,693
11-12-05 조회 3,117
11-12-05 조회 3,409
11-12-05 조회 4,637
11-12-05 조회 3,391
11-12-05 조회 5,006
11-12-05 조회 2,957
11-12-05 조회 6,030
11-12-05 조회 3,158
11-12-05 조회 4,482
11-12-04 조회 5,805
11-12-04 조회 3,040
11-12-04 조회 3,405
11-12-03 조회 3,710
11-12-03 조회 3,075
11-12-03 조회 3,677
11-12-03 조회 3,579