탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3107 · 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건
+
제목 글쓴이 날짜 조회
13-03-03 조회 6,248
13-03-01 조회 5,577
13-03-01 조회 8,319
13-03-01 조회 6,318
13-02-26 조회 3,923
13-02-26 조회 3,869
13-02-26 조회 4,764
13-02-24 조회 8,181
13-02-24 조회 4,261
13-02-24 조회 3,504
13-02-24 조회 5,318
13-02-24 조회 5,310
13-02-24 조회 4,377
13-02-24 조회 5,713
13-02-24 조회 9,403
13-02-14 조회 5,119
13-02-13 조회 9,046
13-01-29 조회 7,693
13-01-24 조회 5,034
13-01-24 조회 4,911
13-01-17 조회 4,323
13-01-16 조회 4,356
13-01-08 조회 4,404
12-12-27 조회 4,065
12-12-20 조회 1.1만
12-12-20 조회 3,384
12-12-06 조회 4,378
12-12-01 조회 4,138
12-11-28 조회 5,702
12-11-25 조회 5,406
12-10-20 조회 4,671
12-10-03 조회 4,833
12-09-26 조회 4,997
12-09-15 조회 8,450
12-09-14 조회 5,464
12-09-11 조회 3,972
12-09-10 조회 3,983
12-09-05 조회 8,865
12-08-29 조회 4,664
12-08-26 조회 3,796
12-08-23 조회 3,494
12-08-22 조회 3,579
12-08-22 조회 4,393
12-08-21 조회 3,540
12-08-20 조회 3,465
12-08-20 조회 9,268
12-08-15 조회 4,386
12-08-13 조회 3,301
12-08-11 조회 4,721
12-08-05 조회 4,572
12-08-03 조회 3,350
12-08-03 조회 4,043
12-08-02 조회 5,025
12-07-29 조회 4,139
12-07-26 조회 3,716
12-07-26 조회 3,586
12-07-22 조회 3,497
12-07-20 조회 3,333
12-07-19 조회 4,196
12-07-19 조회 3,453
12-07-16 조회 3,565
12-07-16 조회 4,485
12-07-15 조회 3,566
12-07-12 조회 7,391
12-07-12 조회 5,036
12-07-11 조회 3,083
12-07-09 조회 3,240
12-07-05 조회 3,855
12-07-04 조회 4,424
12-07-04 조회 3,829
12-07-04 조회 4,352
12-06-29 조회 3,822
12-06-27 조회 3,410
12-06-24 조회 3,882
12-06-22 조회 4,194
12-06-22 조회 8,605
12-06-22 조회 3,260
12-06-21 조회 3,289
12-06-17 조회 3,920
12-06-14 조회 4,018
12-06-14 조회 3,305
12-06-08 조회 4,448
12-06-06 조회 3,412
12-06-04 조회 4,165
12-05-26 조회 4,981
12-05-26 조회 3,937
12-05-25 조회 3,234
12-05-24 조회 3,336
12-05-22 조회 3,383
12-05-20 조회 3,009
12-05-16 조회 3,141
12-05-15 조회 3,534
12-05-10 조회 3,966
12-05-08 조회 3,490
12-05-04 조회 3,344
12-05-03 조회 4,297
12-05-02 조회 3,431
12-05-02 조회 3,205
12-05-02 조회 3,304
12-04-26 조회 3,453