이동하는 글자

· 13년 전 · 1462 · 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,145
13년 전 조회 998
13년 전 조회 1,076
13년 전 조회 1,309
13년 전 조회 1,046
13년 전 조회 1,962
13년 전 조회 961
13년 전 조회 990
13년 전 조회 1,363
13년 전 조회 1,101
13년 전 조회 1,007
13년 전 조회 1,136
13년 전 조회 1,181
13년 전 조회 1,021
13년 전 조회 2,870
13년 전 조회 1,982
13년 전 조회 1,831
13년 전 조회 1,049
13년 전 조회 1,013
13년 전 조회 1,107
13년 전 조회 1,310
13년 전 조회 1,096
13년 전 조회 1,684
13년 전 조회 1,758
13년 전 조회 1,336
13년 전 조회 1,491
13년 전 조회 2,544
13년 전 조회 2,871
13년 전 조회 6,134
13년 전 조회 1,022
13년 전 조회 1,483
13년 전 조회 1,021
13년 전 조회 1,298
13년 전 조회 1,931
13년 전 조회 1,515
13년 전 조회 1,150
13년 전 조회 1,228
13년 전 조회 1,081
13년 전 조회 1,248
13년 전 조회 2,183
13년 전 조회 2,468
13년 전 조회 1,353
13년 전 조회 1,618
13년 전 조회 2,715
13년 전 조회 1,642
13년 전 조회 1,650
13년 전 조회 1,356
13년 전 조회 2,724
13년 전 조회 1,319
13년 전 조회 1,463
13년 전 조회 1,823
13년 전 조회 1,980
13년 전 조회 3,575
13년 전 조회 3,805
13년 전 조회 1,285
13년 전 조회 1,773
13년 전 조회 1,257
13년 전 조회 1,875
13년 전 조회 1,644
13년 전 조회 1,503
13년 전 조회 1,844
13년 전 조회 1,077
13년 전 조회 2,493
13년 전 조회 2,296
13년 전 조회 1,968
13년 전 조회 1,420
13년 전 조회 1,921
13년 전 조회 2,117
13년 전 조회 1,728
13년 전 조회 2,910
13년 전 조회 1,542
13년 전 조회 1,279
13년 전 조회 1,467
13년 전 조회 4,275
13년 전 조회 3,191
13년 전 조회 1,696
13년 전 조회 1,931
13년 전 조회 3,267
13년 전 조회 2,264
13년 전 조회 1,407
13년 전 조회 1,504
13년 전 조회 2,875
13년 전 조회 1,383
13년 전 조회 1,710
13년 전 조회 1,103
13년 전 조회 1,255
13년 전 조회 2,021
13년 전 조회 3,561
13년 전 조회 1,333
13년 전 조회 1,581
13년 전 조회 1,992
13년 전 조회 1,403
13년 전 조회 3,001
13년 전 조회 1,601
13년 전 조회 2,268
13년 전 조회 2,515
13년 전 조회 2,342
13년 전 조회 1,652
13년 전 조회 1,918
13년 전 조회 1,429