탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3159 · 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-06-21 조회 4,007
12-06-21 조회 3,554
12-06-20 조회 3,724
12-06-20 조회 4,289
12-06-20 조회 3,376
12-06-20 조회 4,715
12-06-20 조회 3,356
12-06-20 조회 3,318
12-06-20 조회 4,131
12-06-20 조회 4,344
12-06-18 조회 4,887
12-06-18 조회 3,541
12-06-18 조회 3,661
12-06-18 조회 3,454
12-06-18 조회 4,206
12-06-18 조회 4,873
12-06-17 조회 4,073
12-06-17 조회 3,987
12-06-17 조회 3,624
12-06-16 조회 3,409
12-06-15 조회 3,236
12-06-15 조회 3,938
12-06-15 조회 4,690
12-06-15 조회 4,419
12-06-14 조회 4,066
12-06-14 조회 4,081
12-06-14 조회 3,158
12-06-14 조회 3,574
12-06-14 조회 4,875
12-06-14 조회 3,364
12-06-14 조회 3,224
12-06-13 조회 7,441
12-06-13 조회 3,700
12-06-12 조회 5,666
12-06-11 조회 3,534
12-06-11 조회 3,565
12-06-11 조회 3,560
12-06-11 조회 3,457
12-06-11 조회 4,022
12-06-11 조회 3,333
12-06-08 조회 3,405
12-06-08 조회 4,900
12-06-08 조회 3,206
12-06-08 조회 4,976
12-06-08 조회 3,990
12-06-08 조회 3,529
12-06-08 조회 3,489
12-06-08 조회 4,497
12-06-08 조회 3,118
12-06-08 조회 3,805
12-06-07 조회 4,086
12-06-07 조회 3,978
12-06-06 조회 3,391
12-06-06 조회 3,457
12-06-06 조회 3,769
12-06-06 조회 6,449
12-06-06 조회 6,016
12-06-06 조회 6,764
12-06-05 조회 5,880
12-06-05 조회 3,290
12-06-05 조회 3,366
12-06-05 조회 3,388
12-06-05 조회 1.2만
12-06-05 조회 5,035
12-06-05 조회 5,555
12-06-05 조회 5,312
12-06-05 조회 5,664
12-06-05 조회 3,613
12-06-05 조회 3,547
12-06-05 조회 7,416
12-06-04 조회 3,385
12-06-04 조회 4,235
12-06-04 조회 3,960
12-06-04 조회 4,016
12-06-03 조회 3,859
12-06-02 조회 3,253
12-06-02 조회 3,977
12-06-01 조회 3,407
12-06-01 조회 4,132
12-06-01 조회 3,714
12-06-01 조회 3,355
12-06-01 조회 4,867
12-06-01 조회 3,719
12-06-01 조회 3,342
12-05-31 조회 5,021
12-05-31 조회 3,818
12-05-31 조회 3,756
12-05-31 조회 3,757
12-05-31 조회 3,553
12-05-30 조회 3,212
12-05-30 조회 3,266
12-05-30 조회 4,077
12-05-30 조회 3,326
12-05-29 조회 4,526
12-05-29 조회 3,853
12-05-28 조회 3,238
12-05-28 조회 6,681
12-05-28 조회 3,328
12-05-27 조회 4,652
12-05-27 조회 3,902