탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3063 · 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-26 조회 3,768
12-04-24 조회 3,167
12-04-23 조회 4,518
12-04-22 조회 3,215
12-04-19 조회 3,285
12-04-18 조회 4,352
12-04-18 조회 3,064
12-04-17 조회 3,331
12-04-17 조회 5,094
12-04-16 조회 3,498
12-04-13 조회 3,038
12-04-09 조회 3,104
12-04-04 조회 3,884
12-04-01 조회 3,170
12-03-31 조회 3,546
12-03-30 조회 3,167
12-03-30 조회 3,825
12-03-26 조회 3,444
12-03-26 조회 3,006
12-03-22 조회 3,172
12-03-21 조회 3,024
12-03-19 조회 3,442
12-03-17 조회 3,470
12-03-14 조회 3,197
12-03-10 조회 3,876
12-03-09 조회 3,460
12-03-09 조회 3,284
12-03-08 조회 3,759
12-03-08 조회 3,915
12-03-08 조회 4,023
12-03-03 조회 3,246
12-03-02 조회 4,599
12-02-28 조회 3,114
12-02-27 조회 2,920
12-02-25 조회 3,101
12-02-24 조회 3,800
12-02-23 조회 3,074
12-02-22 조회 3,600
12-02-22 조회 5,370
12-02-22 조회 3,938
12-02-22 조회 4,255
12-02-18 조회 3,741
12-02-17 조회 2,953
12-02-17 조회 3,366
12-02-14 조회 3,442
12-02-13 조회 3,321
12-02-10 조회 1만
12-02-08 조회 2,983
12-02-07 조회 3,329
12-02-06 조회 3,041
12-02-05 조회 3,521
12-01-31 조회 3,423
12-01-30 조회 9,626
12-01-29 조회 3,751
12-01-26 조회 3,391
12-01-25 조회 3,979
12-01-24 조회 3,375
12-01-10 조회 3,706
12-01-10 조회 4,445
12-01-09 조회 3,242
12-01-08 조회 7,103
12-01-08 조회 3,625
12-01-07 조회 5,140
12-01-04 조회 3,860
12-01-02 조회 3,077
12-01-02 조회 3,137
12-01-02 조회 3,447
12-01-01 조회 3,075
11-12-31 조회 3,373
11-12-31 조회 3,050
11-12-30 조회 3,226
11-12-30 조회 3,368
11-12-29 조회 3,567
11-12-28 조회 3,679
11-12-26 조회 3,241
11-12-23 조회 3,471
11-12-22 조회 3,002
11-12-15 조회 4,096
11-12-08 조회 3,928
11-12-05 조회 3,256
11-12-04 조회 5,681
11-12-03 조회 3,530
11-12-02 조회 3,311
11-12-01 조회 5,375
11-11-30 조회 2,867
11-11-30 조회 2,773
11-11-28 조회 3,116
11-11-26 조회 3,284
11-11-25 조회 3,309
11-11-24 조회 2,927
11-11-22 조회 3,002
11-11-22 조회 2,854
11-11-17 조회 2,783
11-11-11 조회 2,797
11-11-09 조회 2,855
11-11-08 조회 3,220
11-11-08 조회 2,514
11-11-04 조회 3,594
11-11-01 조회 2,906
11-10-26 조회 2,484