탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3120 · 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,633
12-04-24 조회 2,691
12-04-24 조회 3,848
12-04-24 조회 2,973
12-04-24 조회 3,339
12-04-24 조회 2,738
12-04-24 조회 3,249
12-04-24 조회 2,777
12-04-24 조회 2,742
12-04-24 조회 2,920
12-04-24 조회 4,623
12-04-23 조회 3,065
12-04-23 조회 3,880
12-04-23 조회 2,700
12-04-23 조회 2,702
12-04-23 조회 2,985
12-04-23 조회 2,619
12-04-23 조회 3,656
12-04-23 조회 2,548
12-04-23 조회 2,619
12-04-23 조회 2,977
12-04-23 조회 2,782
12-04-23 조회 2,627
12-04-23 조회 2,824
12-04-23 조회 2,829
12-04-23 조회 2,590
12-04-23 조회 4,575
12-04-23 조회 3,612
12-04-23 조회 3,475
12-04-22 조회 2,613
12-04-22 조회 2,565
12-04-22 조회 2,650
12-04-22 조회 2,872
12-04-22 조회 2,673
12-04-22 조회 3,264
12-04-22 조회 3,325
12-04-22 조회 2,917
12-04-21 조회 3,171
12-04-21 조회 4,112
12-04-21 조회 4,550
12-04-21 조회 7,896
12-04-21 조회 2,702
12-04-21 조회 3,104
12-04-21 조회 2,604
12-04-21 조회 2,842
12-04-20 조회 3,639
12-04-20 조회 3,148
12-04-20 조회 2,812
12-04-20 조회 2,888
12-04-20 조회 2,688
12-04-20 조회 2,811
12-04-20 조회 3,930
12-04-20 조회 4,138
12-04-20 조회 2,998
12-04-20 조회 3,273
12-04-20 조회 4,362
12-04-20 조회 3,301
12-04-19 조회 3,359
12-04-19 조회 2,988
12-04-18 조회 4,427
12-04-18 조회 2,899
12-04-18 조회 3,121
12-04-17 조회 3,396
12-04-17 조회 3,568
12-04-17 조회 5,151
12-04-17 조회 5,456
12-04-17 조회 2,892
12-04-17 조회 3,388
12-04-16 조회 2,865
12-04-16 조회 3,579
12-04-16 조회 3,270
12-04-16 조회 3,167
12-04-16 조회 3,557
12-04-16 조회 2,803
12-04-16 조회 4,394
12-04-15 조회 3,975
12-04-14 조회 3,673
12-04-14 조회 3,054
12-04-14 조회 3,709
12-04-14 조회 3,754
12-04-13 조회 3,377
12-04-13 조회 4,563
12-04-13 조회 3,183
12-04-13 조회 2,910
12-04-13 조회 3,126
12-04-13 조회 6,204
12-04-12 조회 4,824
12-04-12 조회 3,369
12-04-11 조회 3,582
12-04-10 조회 5,074
12-04-10 조회 4,027
12-04-10 조회 3,133
12-04-09 조회 3,182
12-04-09 조회 4,760
12-04-09 조회 3,115
12-04-08 조회 3,433
12-04-07 조회 2,769
12-04-06 조회 2,923
12-04-06 조회 3,670
12-04-06 조회 5,443