탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3080 · 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,833
12-04-26 조회 2,708
12-04-26 조회 2,960
12-04-26 조회 2,774
12-04-26 조회 3,095
12-04-25 조회 2,505
12-04-25 조회 2,813
12-04-25 조회 2,678
12-04-25 조회 2,733
12-04-25 조회 2,697
12-04-25 조회 2,929
12-04-24 조회 4,049
12-04-24 조회 2,565
12-04-24 조회 2,480
12-04-24 조회 2,719
12-04-24 조회 2,649
12-04-24 조회 2,551
12-04-24 조회 2,919
12-04-24 조회 2,935
12-04-24 조회 2,589
12-04-24 조회 2,631
12-04-24 조회 3,271
12-04-24 조회 2,681
12-04-24 조회 2,727
12-04-24 조회 2,686
12-04-23 조회 2,650
12-04-23 조회 2,653
12-04-23 조회 2,935
12-04-23 조회 2,568
12-04-23 조회 3,619
12-04-23 조회 2,518
12-04-23 조회 2,575
12-04-23 조회 2,731
12-04-23 조회 2,589
12-04-23 조회 2,778
12-04-23 조회 2,799
12-04-23 조회 2,542
12-04-23 조회 3,426
12-04-22 조회 2,565
12-04-22 조회 2,524
12-04-22 조회 2,605
12-04-22 조회 2,828
12-04-22 조회 2,623
12-04-22 조회 3,292
12-04-21 조회 4,076
12-04-21 조회 7,858
12-04-21 조회 2,667
12-04-21 조회 3,076
12-04-21 조회 2,557
12-04-21 조회 2,801
12-04-20 조회 2,771
12-04-20 조회 2,852
12-04-20 조회 2,641
12-04-20 조회 2,761
12-04-20 조회 3,892
12-04-20 조회 2,942
12-04-16 조회 2,740
12-04-07 조회 2,691
12-04-03 조회 3,125
12-04-02 조회 3,257
12-03-28 조회 2,672
12-03-26 조회 6,147
12-03-25 조회 2,722
12-03-25 조회 2,879
12-03-24 조회 2,952
12-03-23 조회 2,845
12-03-22 조회 5,725
12-03-13 조회 2,711
12-03-11 조회 3,126
12-03-11 조회 4,056
12-03-11 조회 4,866
12-03-11 조회 3,756
12-03-11 조회 3,721
12-03-11 조회 5,377
12-03-06 조회 2,865
12-03-06 조회 2,962
12-03-05 조회 3,029
12-03-02 조회 2,944
12-03-01 조회 6,466
12-03-01 조회 3,264
12-02-29 조회 5,417
12-02-17 조회 3,015
12-02-16 조회 2,855
12-02-14 조회 3,982
12-02-14 조회 3,022
12-02-08 조회 2,998
12-02-07 조회 2,587
12-02-06 조회 1.2만
12-02-04 조회 2,848
12-02-02 조회 2,928
12-01-29 조회 4,630
12-01-27 조회 2,863
12-01-25 조회 4,044
12-01-24 조회 4,330
12-01-21 조회 3,475
12-01-21 조회 3,147
12-01-16 조회 3,522
12-01-16 조회 4,034
12-01-14 조회 2,869
12-01-13 조회 4,508