탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3070 · 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건
+
제목 글쓴이 날짜 조회
12-04-24 조회 2,582
12-04-24 조회 2,620
12-04-24 조회 3,788
12-04-24 조회 2,913
12-04-24 조회 3,252
12-04-24 조회 2,670
12-04-24 조회 3,180
12-04-24 조회 2,710
12-04-24 조회 2,670
12-04-24 조회 2,862
12-04-24 조회 4,549
12-04-23 조회 3,006
12-04-23 조회 3,813
12-04-23 조회 2,630
12-04-23 조회 2,646
12-04-23 조회 2,923
12-04-23 조회 2,559
12-04-23 조회 3,613
12-04-23 조회 2,511
12-04-23 조회 2,570
12-04-23 조회 2,930
12-04-23 조회 2,723
12-04-23 조회 2,582
12-04-23 조회 2,768
12-04-23 조회 2,787
12-04-23 조회 2,534
12-04-23 조회 4,529
12-04-23 조회 3,562
12-04-23 조회 3,415
12-04-22 조회 2,560
12-04-22 조회 2,514
12-04-22 조회 2,597
12-04-22 조회 2,818
12-04-22 조회 2,615
12-04-22 조회 3,219
12-04-22 조회 3,286
12-04-22 조회 2,873
12-04-21 조회 3,121
12-04-21 조회 4,071
12-04-21 조회 4,505
12-04-21 조회 7,849
12-04-21 조회 2,662
12-04-21 조회 3,064
12-04-21 조회 2,552
12-04-21 조회 2,796
12-04-20 조회 3,596
12-04-20 조회 3,109
12-04-20 조회 2,765
12-04-20 조회 2,846
12-04-20 조회 2,633
12-04-20 조회 2,755
12-04-20 조회 3,878
12-04-20 조회 4,091
12-04-20 조회 2,932
12-04-20 조회 3,197
12-04-20 조회 4,295
12-04-20 조회 3,242
12-04-19 조회 3,298
12-04-19 조회 2,920
12-04-18 조회 4,361
12-04-18 조회 2,838
12-04-18 조회 3,071
12-04-17 조회 3,341
12-04-17 조회 3,510
12-04-17 조회 5,100
12-04-17 조회 5,393
12-04-17 조회 2,828
12-04-17 조회 3,325
12-04-16 조회 2,799
12-04-16 조회 3,511
12-04-16 조회 3,206
12-04-16 조회 3,091
12-04-16 조회 3,498
12-04-16 조회 2,719
12-04-16 조회 4,321
12-04-15 조회 3,915
12-04-14 조회 3,612
12-04-14 조회 2,997
12-04-14 조회 3,647
12-04-14 조회 3,696
12-04-13 조회 3,317
12-04-13 조회 4,501
12-04-13 조회 3,111
12-04-13 조회 2,833
12-04-13 조회 3,046
12-04-13 조회 6,125
12-04-12 조회 4,747
12-04-12 조회 3,295
12-04-11 조회 3,507
12-04-10 조회 4,999
12-04-10 조회 3,938
12-04-10 조회 3,061
12-04-09 조회 3,115
12-04-09 조회 4,688
12-04-09 조회 3,048
12-04-08 조회 3,355
12-04-07 조회 2,672
12-04-06 조회 2,838
12-04-06 조회 3,597
12-04-06 조회 5,369