이동하는 글자

· 13년 전 · 1567 · 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>
감사합니다!
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 2,251
13년 전 조회 1,098
13년 전 조회 1,172
13년 전 조회 1,410
13년 전 조회 1,139
13년 전 조회 2,054
13년 전 조회 1,056
13년 전 조회 1,086
13년 전 조회 1,448
13년 전 조회 1,207
13년 전 조회 1,110
13년 전 조회 1,239
13년 전 조회 1,268
13년 전 조회 1,131
13년 전 조회 2,974
13년 전 조회 2,079
13년 전 조회 1,940
13년 전 조회 1,143
13년 전 조회 1,123
13년 전 조회 1,207
13년 전 조회 1,411
13년 전 조회 1,200
13년 전 조회 1,777
13년 전 조회 1,854
13년 전 조회 1,444
13년 전 조회 1,596
13년 전 조회 2,656
13년 전 조회 2,975
13년 전 조회 6,242
13년 전 조회 1,124
13년 전 조회 1,590
13년 전 조회 1,121
13년 전 조회 1,394
13년 전 조회 2,033
13년 전 조회 1,622
13년 전 조회 1,262
13년 전 조회 1,332
13년 전 조회 1,182
13년 전 조회 1,342
13년 전 조회 2,286
13년 전 조회 2,573
13년 전 조회 1,437
13년 전 조회 1,712
13년 전 조회 2,816
13년 전 조회 1,755
13년 전 조회 1,744
13년 전 조회 1,436
13년 전 조회 2,844
13년 전 조회 1,426
13년 전 조회 1,568
13년 전 조회 1,921
13년 전 조회 2,082
13년 전 조회 3,678
13년 전 조회 3,901
13년 전 조회 1,381
13년 전 조회 1,860
13년 전 조회 1,353
13년 전 조회 1,968
13년 전 조회 1,732
13년 전 조회 1,619
13년 전 조회 1,934
13년 전 조회 1,185
13년 전 조회 2,589
13년 전 조회 2,354
13년 전 조회 2,057
13년 전 조회 1,508
13년 전 조회 2,015
13년 전 조회 2,207
13년 전 조회 1,805
13년 전 조회 3,015
13년 전 조회 1,634
13년 전 조회 1,373
13년 전 조회 1,581
13년 전 조회 4,368
13년 전 조회 3,245
13년 전 조회 1,795
13년 전 조회 1,977
13년 전 조회 3,370
13년 전 조회 2,369
13년 전 조회 1,508
13년 전 조회 1,620
13년 전 조회 2,966
13년 전 조회 1,488
13년 전 조회 1,777
13년 전 조회 1,210
13년 전 조회 1,355
13년 전 조회 2,125
13년 전 조회 3,664
13년 전 조회 1,432
13년 전 조회 1,683
13년 전 조회 2,101
13년 전 조회 1,496
13년 전 조회 3,119
13년 전 조회 1,688
13년 전 조회 2,376
13년 전 조회 2,607
13년 전 조회 2,446
13년 전 조회 1,742
13년 전 조회 2,030
13년 전 조회 1,535