탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3153 · 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-26 조회 3,171
12-02-26 조회 3,488
12-02-26 조회 3,332
12-02-26 조회 3,414
12-02-26 조회 3,453
12-02-26 조회 3,307
12-02-26 조회 3,626
12-02-26 조회 3,457
12-02-26 조회 3,547
12-02-26 조회 4,031
12-02-26 조회 2,978
12-02-26 조회 5,169
12-02-25 조회 2,733
12-02-25 조회 3,207
12-02-24 조회 2,919
12-02-24 조회 3,300
12-02-24 조회 3,276
12-02-24 조회 3,590
12-02-24 조회 3,122
12-02-24 조회 3,920
12-02-24 조회 3,891
12-02-24 조회 3,497
12-02-24 조회 9,353
12-02-23 조회 2,865
12-02-23 조회 3,314
12-02-23 조회 3,214
12-02-23 조회 3,152
12-02-23 조회 3,800
12-02-23 조회 2,884
12-02-23 조회 3,362
12-02-23 조회 3,039
12-02-23 조회 2,802
12-02-22 조회 3,691
12-02-22 조회 5,457
12-02-22 조회 3,013
12-02-22 조회 4,412
12-02-22 조회 3,283
12-02-22 조회 3,110
12-02-22 조회 4,017
12-02-22 조회 3,911
12-02-22 조회 2,840
12-02-22 조회 3,878
12-02-22 조회 4,334
12-02-22 조회 3,759
12-02-22 조회 2,943
12-02-22 조회 4,592
12-02-21 조회 3,151
12-02-21 조회 2,928
12-02-21 조회 2,834
12-02-21 조회 3,746
12-02-21 조회 3,983
12-02-21 조회 3,410
12-02-21 조회 2,921
12-02-20 조회 3,160
12-02-20 조회 3,799
12-02-20 조회 3,744
12-02-20 조회 3,023
12-02-20 조회 5,812
12-02-20 조회 3,604
12-02-20 조회 6,822
12-02-20 조회 3,217
12-02-20 조회 4,191
12-02-19 조회 3,378
12-02-18 조회 3,111
12-02-18 조회 3,853
12-02-17 조회 2,989
12-02-17 조회 4,180
12-02-17 조회 3,062
12-02-17 조회 3,614
12-02-17 조회 3,392
12-02-17 조회 6,262
12-02-17 조회 4,456
12-02-17 조회 3,079
12-02-17 조회 3,104
12-02-17 조회 3,470
12-02-16 조회 3,300
12-02-16 조회 3,370
12-02-16 조회 4,729
12-02-16 조회 2,976
12-02-16 조회 5,362
12-02-16 조회 5,111
12-02-15 조회 3,661
12-02-15 조회 3,274
12-02-15 조회 4,819
12-02-15 조회 3,309
12-02-15 조회 3,709
12-02-15 조회 2,779
12-02-15 조회 4,051
12-02-14 조회 3,567
12-02-14 조회 3,676
12-02-14 조회 4,088
12-02-14 조회 3,123
12-02-14 조회 6,205
12-02-14 조회 3,153
12-02-14 조회 4,179
12-02-13 조회 3,462
12-02-13 조회 3,085
12-02-13 조회 7,224
12-02-12 조회 2,899
12-02-12 조회 5,275