탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3102 · 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건
+
제목 글쓴이 날짜 조회
16-06-03 조회 2,997
15-07-30 조회 3,391
15-06-08 조회 5,542
15-05-27 조회 4,400
15-01-08 조회 6,690
14-12-30 조회 5,421
14-10-10 조회 5,415
14-08-13 조회 6,327
14-08-11 조회 5,188
14-08-06 조회 6,518
14-03-19 조회 7,415
13-04-26 조회 9,576
12-12-15 조회 5,097
12-12-15 조회 7,331
12-12-11 조회 5,461
12-11-21 조회 5,996
12-10-11 조회 5,076
12-10-03 조회 4,772
12-09-20 조회 6,075
12-09-02 조회 4,252
12-08-29 조회 5,249
12-08-29 조회 4,136
12-08-24 조회 4,232
12-08-18 조회 4,458
12-08-17 조회 4,503
12-08-16 조회 3,758
12-08-06 조회 5,532
12-08-05 조회 4,824
12-08-03 조회 4,541
12-08-03 조회 4,680
12-07-29 조회 3,539
12-07-24 조회 4,125
12-07-22 조회 3,599
12-07-20 조회 4,348
12-07-19 조회 4,699
12-07-19 조회 3,929
12-07-19 조회 3,876
12-07-19 조회 5,542
12-07-19 조회 3,487
12-07-18 조회 5,168
12-07-15 조회 4,138
12-07-14 조회 3,821
12-07-12 조회 4,500
12-07-11 조회 3,772
12-07-11 조회 3,493
12-07-11 조회 3,456
12-07-10 조회 4,638
12-07-09 조회 3,393
12-07-09 조회 4,216
12-07-09 조회 4,633
12-07-08 조회 4,556
12-07-07 조회 5,474
12-07-06 조회 4,731
12-07-06 조회 4,001
12-07-06 조회 3,885
12-07-06 조회 3,812
12-07-02 조회 5,494
12-07-02 조회 4,195
12-06-29 조회 3,718
12-06-28 조회 4,347
12-06-26 조회 3,833
12-06-26 조회 4,958
12-06-21 조회 3,933
12-06-20 조회 4,638
12-06-20 조회 4,261
12-06-15 조회 4,622
12-06-15 조회 4,353
12-06-13 조회 7,365
12-06-05 조회 7,346
12-06-04 조회 3,923
12-06-03 조회 3,772
12-05-31 조회 3,775
12-05-28 조회 6,614
12-05-27 조회 3,846
12-05-25 조회 3,986
12-05-23 조회 4,399
12-05-21 조회 5,103
12-05-17 조회 4,045
12-05-17 조회 3,968
12-05-09 조회 4,039
12-05-07 조회 4,289
12-05-02 조회 4,486
12-05-01 조회 4,655
12-04-30 조회 3,404
12-04-30 조회 4,347
12-04-25 조회 4,434
12-04-25 조회 4,859
12-04-24 조회 3,828
12-04-24 조회 4,593
12-04-23 조회 3,595
12-04-20 조회 3,239
12-04-17 조회 5,428
12-04-16 조회 4,370
12-04-14 조회 3,653
12-04-14 조회 3,682
12-04-14 조회 3,732
12-04-13 조회 3,355
12-04-13 조회 4,540
12-04-13 조회 6,177
12-04-12 조회 3,345