이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3018 · 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,188건
+
제목 글쓴이 날짜 조회
16-06-03 조회 2,744
15-07-30 조회 3,237
15-06-08 조회 5,293
15-05-27 조회 4,245
15-01-08 조회 6,544
14-12-30 조회 5,169
14-10-10 조회 5,216
14-08-13 조회 6,188
14-08-11 조회 4,958
14-08-06 조회 6,312
14-03-19 조회 7,256
13-04-26 조회 9,459
12-12-15 조회 5,012
12-12-15 조회 7,248
12-12-11 조회 5,347
12-11-21 조회 5,833
12-10-11 조회 4,954
12-10-03 조회 4,638
12-09-20 조회 5,976
12-09-02 조회 4,124
12-08-29 조회 4,944
12-08-29 조회 3,951
12-08-24 조회 4,080
12-08-18 조회 4,321
12-08-17 조회 4,282
12-08-16 조회 3,612
12-08-06 조회 5,380
12-08-05 조회 4,629
12-08-03 조회 4,405
12-08-03 조회 4,542
12-07-29 조회 3,372
12-07-24 조회 3,981
12-07-22 조회 3,474
12-07-20 조회 4,128
12-07-19 조회 4,447
12-07-19 조회 3,804
12-07-19 조회 3,703
12-07-19 조회 5,386
12-07-19 조회 3,346
12-07-18 조회 4,905
12-07-15 조회 3,905
12-07-14 조회 3,648
12-07-12 조회 4,329
12-07-11 조회 3,633
12-07-11 조회 3,365
12-07-11 조회 3,307
12-07-10 조회 4,489
12-07-09 조회 3,214
12-07-09 조회 4,097
12-07-09 조회 4,496
12-07-08 조회 4,323
12-07-07 조회 5,345
12-07-06 조회 4,631
12-07-06 조회 3,889
12-07-06 조회 3,634
12-07-06 조회 3,649
12-07-02 조회 5,365
12-07-02 조회 4,044
12-06-29 조회 3,563
12-06-28 조회 4,076
12-06-26 조회 3,666
12-06-26 조회 4,789
12-06-21 조회 3,753
12-06-20 조회 4,396
12-06-20 조회 3,934
12-06-15 조회 4,441
12-06-15 조회 4,193
12-06-13 조회 7,209
12-06-05 조회 7,184
12-06-04 조회 3,794
12-06-03 조회 3,639
12-05-31 조회 3,643
12-05-28 조회 6,464
12-05-27 조회 3,679
12-05-25 조회 3,866
12-05-23 조회 4,102
12-05-21 조회 4,967
12-05-17 조회 3,924
12-05-17 조회 3,797
12-05-09 조회 3,816
12-05-07 조회 4,064
12-05-02 조회 4,373
12-05-01 조회 4,442
12-04-30 조회 3,285
12-04-30 조회 4,233
12-04-25 조회 4,288
12-04-25 조회 4,728
12-04-24 조회 3,716
12-04-24 조회 4,462
12-04-23 조회 3,501
12-04-20 조회 3,130
12-04-17 조회 5,328
12-04-16 조회 4,211
12-04-14 조회 3,519
12-04-14 조회 3,559
12-04-14 조회 3,628
12-04-13 조회 3,253
12-04-13 조회 4,444
12-04-13 조회 6,034
12-04-12 조회 3,194