이동하는 글자

· 13년 전 · 1516 · 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,205
13년 전 조회 1,057
13년 전 조회 1,125
13년 전 조회 1,361
13년 전 조회 1,103
13년 전 조회 2,018
13년 전 조회 1,012
13년 전 조회 1,050
13년 전 조회 1,410
13년 전 조회 1,164
13년 전 조회 1,071
13년 전 조회 1,193
13년 전 조회 1,232
13년 전 조회 1,088
13년 전 조회 2,920
13년 전 조회 2,038
13년 전 조회 1,890
13년 전 조회 1,096
13년 전 조회 1,076
13년 전 조회 1,155
13년 전 조회 1,370
13년 전 조회 1,159
13년 전 조회 1,739
13년 전 조회 1,803
13년 전 조회 1,392
13년 전 조회 1,558
13년 전 조회 2,604
13년 전 조회 2,927
13년 전 조회 6,200
13년 전 조회 1,077
13년 전 조회 1,544
13년 전 조회 1,083
13년 전 조회 1,350
13년 전 조회 1,991
13년 전 조회 1,575
13년 전 조회 1,212
13년 전 조회 1,287
13년 전 조회 1,146
13년 전 조회 1,305
13년 전 조회 2,248
13년 전 조회 2,537
13년 전 조회 1,403
13년 전 조회 1,675
13년 전 조회 2,769
13년 전 조회 1,705
13년 전 조회 1,701
13년 전 조회 1,402
13년 전 조회 2,790
13년 전 조회 1,382
13년 전 조회 1,517
13년 전 조회 1,880
13년 전 조회 2,039
13년 전 조회 3,631
13년 전 조회 3,862
13년 전 조회 1,341
13년 전 조회 1,820
13년 전 조회 1,306
13년 전 조회 1,930
13년 전 조회 1,700
13년 전 조회 1,569
13년 전 조회 1,902
13년 전 조회 1,134
13년 전 조회 2,553
13년 전 조회 2,325
13년 전 조회 2,021
13년 전 조회 1,466
13년 전 조회 1,974
13년 전 조회 2,166
13년 전 조회 1,777
13년 전 조회 2,971
13년 전 조회 1,597
13년 전 조회 1,343
13년 전 조회 1,533
13년 전 조회 4,325
13년 전 조회 3,217
13년 전 조회 1,752
13년 전 조회 1,959
13년 전 조회 3,322
13년 전 조회 2,324
13년 전 조회 1,465
13년 전 조회 1,566
13년 전 조회 2,936
13년 전 조회 1,432
13년 전 조회 1,751
13년 전 조회 1,157
13년 전 조회 1,312
13년 전 조회 2,072
13년 전 조회 3,629
13년 전 조회 1,389
13년 전 조회 1,648
13년 전 조회 2,060
13년 전 조회 1,454
13년 전 조회 3,065
13년 전 조회 1,646
13년 전 조회 2,330
13년 전 조회 2,573
13년 전 조회 2,402
13년 전 조회 1,700
13년 전 조회 1,981
13년 전 조회 1,491