탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3175 · 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,187건
+
제목 글쓴이 날짜 조회
12-08-02 조회 3,918
12-08-02 조회 3,830
12-08-01 조회 4,805
12-08-01 조회 4,699
12-07-31 조회 9,592
12-07-31 조회 3,837
12-07-31 조회 3,387
12-07-31 조회 3,174
12-07-31 조회 3,458
12-07-30 조회 3,642
12-07-30 조회 3,676
12-07-30 조회 3,326
12-07-30 조회 3,793
12-07-30 조회 4,229
12-07-29 조회 3,566
12-07-29 조회 3,291
12-07-29 조회 3,639
12-07-29 조회 4,228
12-07-27 조회 3,322
12-07-26 조회 4,882
12-07-26 조회 3,793
12-07-26 조회 3,399
12-07-26 조회 3,658
12-07-26 조회 4,079
12-07-25 조회 4,181
12-07-25 조회 3,326
12-07-24 조회 4,211
12-07-24 조회 3,683
12-07-23 조회 5,158
12-07-22 조회 3,684
12-07-22 조회 3,583
12-07-21 조회 3,471
12-07-21 조회 4,237
12-07-21 조회 3,202
12-07-20 조회 4,448
12-07-20 조회 3,751
12-07-20 조회 3,493
12-07-20 조회 3,404
12-07-20 조회 1만
12-07-19 조회 3,579
12-07-19 조회 3,407
12-07-19 조회 4,785
12-07-19 조회 4,277
12-07-19 조회 3,276
12-07-19 조회 3,082
12-07-19 조회 4,023
12-07-19 조회 3,534
12-07-19 조회 3,979
12-07-19 조회 5,634
12-07-19 조회 3,562
12-07-18 조회 3,361
12-07-18 조회 5,276
12-07-18 조회 3,558
12-07-18 조회 3,675
12-07-17 조회 4,399
12-07-17 조회 4,401
12-07-17 조회 3,598
12-07-17 조회 3,287
12-07-17 조회 4,162
12-07-17 조회 3,652
12-07-16 조회 3,987
12-07-16 조회 3,649
12-07-16 조회 3,841
12-07-16 조회 3,326
12-07-16 조회 3,310
12-07-16 조회 3,209
12-07-16 조회 4,565
12-07-15 조회 3,627
12-07-15 조회 4,347
12-07-15 조회 3,576
12-07-15 조회 3,554
12-07-15 조회 3,361
12-07-15 조회 3,834
12-07-15 조회 3,718
12-07-15 조회 3,567
12-07-15 조회 3,678
12-07-15 조회 4,271
12-07-15 조회 3,850
12-07-15 조회 4,195
12-07-15 조회 7,128
12-07-15 조회 3,758
12-07-15 조회 5,252
12-07-14 조회 3,914
12-07-14 조회 4,413
12-07-14 조회 3,462
12-07-14 조회 3,312
12-07-13 조회 8,508
12-07-13 조회 3,736
12-07-13 조회 3,700
12-07-12 조회 7,458
12-07-12 조회 4,436
12-07-12 조회 5,142
12-07-12 조회 4,585
12-07-11 조회 3,866
12-07-11 조회 3,579
12-07-11 조회 3,543
12-07-11 조회 3,153
12-07-11 조회 3,599
12-07-10 조회 1만
12-07-10 조회 5,774