탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3174 · 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-08-06 조회 2,993
11-08-06 조회 2,659
11-08-06 조회 3,373
11-08-05 조회 3,046
11-08-05 조회 2,901
11-08-05 조회 3,242
11-08-05 조회 3,182
11-08-05 조회 2,650
11-08-05 조회 2,563
11-08-05 조회 2,350
11-08-05 조회 2,560
11-08-05 조회 3,400
11-08-05 조회 4,030
11-08-04 조회 4,071
11-08-04 조회 3,606
11-08-04 조회 3,086
11-08-04 조회 3,095
11-08-04 조회 2,959
11-08-04 조회 3,214
11-08-04 조회 2,196
11-08-04 조회 3,099
11-08-04 조회 2,234
11-08-04 조회 2,740
11-08-04 조회 2,518
11-08-04 조회 3,715
11-08-03 조회 2,424
11-08-03 조회 4,223
11-08-03 조회 2,762
11-08-03 조회 2,561
11-08-03 조회 4,619
11-08-03 조회 2,273
11-08-03 조회 2,634
11-08-03 조회 2,091
11-08-02 조회 2,603
11-08-02 조회 2,178
11-08-02 조회 2,933
11-08-02 조회 2,273
11-08-02 조회 2,776
11-08-02 조회 2,324
11-08-02 조회 2,235
11-08-01 조회 2,154
11-08-01 조회 3,056
11-08-01 조회 2,367
11-08-01 조회 2,216
11-08-01 조회 2,243
11-07-30 조회 2,976
11-07-30 조회 2,948
11-07-29 조회 2,138
11-07-29 조회 4,081
11-07-28 조회 2,018
11-07-28 조회 2,248
11-07-28 조회 4,843
11-07-27 조회 2,290
11-07-27 조회 2,722
11-07-27 조회 3,017
11-07-26 조회 2,152
11-07-26 조회 2,272
11-07-26 조회 2,336
11-07-26 조회 2,683
11-07-26 조회 2,262
11-07-26 조회 2,572
11-07-26 조회 4,518
11-07-26 조회 2,914
11-07-26 조회 2,327
11-07-26 조회 3,134
11-07-25 조회 2,425
11-07-25 조회 2,012
11-07-25 조회 3,910
11-07-25 조회 2,315
11-07-25 조회 2,439
11-07-23 조회 2,662
11-07-23 조회 2,602
11-07-23 조회 2,753
11-07-22 조회 2,167
11-07-22 조회 4,095
11-07-22 조회 2,428
11-07-21 조회 3,078
11-07-21 조회 2,615
11-07-21 조회 2,715
11-07-21 조회 2,381
11-07-20 조회 2,611
11-07-20 조회 2,179
11-07-20 조회 2,235
11-07-20 조회 2,988
11-07-20 조회 2,811
11-07-20 조회 2,598
11-07-20 조회 2,652
11-07-20 조회 3,555
11-07-20 조회 3,235
11-07-19 조회 2,170
11-07-19 조회 2,998
11-07-19 조회 2,868
11-07-19 조회 2,879
11-07-18 조회 2,408
11-07-18 조회 2,394
11-07-18 조회 2,361
11-07-18 조회 2,355
11-07-18 조회 3,036
11-07-18 조회 2,409
11-07-17 조회 2,651