탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3032 · 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 조회 4,783
12-04-26 조회 2,643
12-04-26 조회 2,876
12-04-26 조회 2,694
12-04-26 조회 3,002
12-04-25 조회 2,436
12-04-25 조회 2,645
12-04-25 조회 2,624
12-04-25 조회 2,684
12-04-25 조회 2,541
12-04-25 조회 2,880
12-04-24 조회 4,010
12-04-24 조회 2,537
12-04-24 조회 2,450
12-04-24 조회 2,674
12-04-24 조회 2,619
12-04-24 조회 2,506
12-04-24 조회 2,824
12-04-24 조회 2,781
12-04-24 조회 2,535
12-04-24 조회 2,566
12-04-24 조회 3,154
12-04-24 조회 2,486
12-04-24 조회 2,649
12-04-24 조회 2,532
12-04-23 조회 2,513
12-04-23 조회 2,536
12-04-23 조회 2,844
12-04-23 조회 2,531
12-04-23 조회 3,442
12-04-23 조회 2,410
12-04-23 조회 2,489
12-04-23 조회 2,617
12-04-23 조회 2,494
12-04-23 조회 2,633
12-04-23 조회 2,683
12-04-23 조회 2,473
12-04-23 조회 3,363
12-04-22 조회 2,512
12-04-22 조회 2,472
12-04-22 조회 2,557
12-04-22 조회 2,781
12-04-22 조회 2,568
12-04-22 조회 3,229
12-04-21 조회 4,024
12-04-21 조회 7,676
12-04-21 조회 2,571
12-04-21 조회 3,025
12-04-21 조회 2,514
12-04-21 조회 2,756
12-04-20 조회 2,717
12-04-20 조회 2,811
12-04-20 조회 2,596
12-04-20 조회 2,723
12-04-20 조회 3,843
12-04-20 조회 2,889
12-04-16 조회 2,646
12-04-07 조회 2,613
12-04-03 조회 3,067
12-04-02 조회 3,175
12-03-28 조회 2,604
12-03-26 조회 6,034
12-03-25 조회 2,672
12-03-25 조회 2,803
12-03-24 조회 2,903
12-03-23 조회 2,766
12-03-22 조회 5,670
12-03-13 조회 2,647
12-03-11 조회 3,068
12-03-11 조회 3,996
12-03-11 조회 4,813
12-03-11 조회 3,706
12-03-11 조회 3,671
12-03-11 조회 5,325
12-03-06 조회 2,785
12-03-06 조회 2,873
12-03-05 조회 2,939
12-03-02 조회 2,866
12-03-01 조회 6,388
12-03-01 조회 3,200
12-02-29 조회 5,342
12-02-17 조회 2,955
12-02-16 조회 2,759
12-02-14 조회 3,743
12-02-14 조회 2,923
12-02-08 조회 2,896
12-02-07 조회 2,529
12-02-06 조회 1.2만
12-02-04 조회 2,789
12-02-02 조회 2,871
12-01-29 조회 4,556
12-01-27 조회 2,724
12-01-25 조회 3,878
12-01-24 조회 4,231
12-01-21 조회 3,297
12-01-21 조회 3,059
12-01-16 조회 3,467
12-01-16 조회 3,949
12-01-14 조회 2,799
12-01-13 조회 4,354