탈

이동하는 글자

· 2012-04-18 (수) 18:14:13 · 3030 · 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-06-05 조회 3,120
12-06-04 조회 3,157
12-06-02 조회 3,065
12-06-02 조회 3,800
12-06-01 조회 3,535
12-05-25 조회 3,516
12-05-25 조회 3,029
12-05-21 조회 3,449
12-05-19 조회 2,783
12-05-18 조회 3,202
12-05-16 조회 3,257
12-05-11 조회 2,809
12-05-07 조회 2,868
12-05-05 조회 3,088
12-05-05 조회 2,988
12-05-05 조회 2,879
12-05-04 조회 3,931
12-05-03 조회 3,040
12-04-30 조회 3,056
12-04-26 조회 3,979
12-04-25 조회 2,798
12-04-23 조회 2,887
12-04-23 조회 2,830
12-04-20 조회 4,233
12-04-20 조회 3,187
12-04-18 조회 2,786
12-04-17 조회 3,458
12-04-17 조회 2,788
12-04-17 조회 3,284
12-04-16 조회 2,741
12-04-16 조회 3,034
12-04-13 조회 2,765
12-04-06 조회 3,507
12-04-05 조회 2,891
12-04-03 조회 3,145
12-04-03 조회 2,782
12-03-30 조회 2,768
12-03-30 조회 3,234
12-03-29 조회 3,793
12-03-27 조회 2,927
12-03-26 조회 4,009
12-03-24 조회 6,255
12-03-24 조회 3,759
12-03-23 조회 2,908
12-03-22 조회 3,141
12-03-22 조회 3,420
12-03-22 조회 3,606
12-03-22 조회 4,960
12-03-21 조회 3,062
12-03-21 조회 2,761
12-03-21 조회 4,778
12-03-20 조회 5,539
12-03-20 조회 2,724
12-03-17 조회 3,235
12-03-16 조회 3,268
12-03-15 조회 2,649
12-03-14 조회 2,813
12-03-13 조회 2,861
12-03-12 조회 3,906
12-03-09 조회 2,965
12-03-09 조회 3,901
12-03-07 조회 2,787
12-03-07 조회 2,952
12-03-07 조회 2,632
12-03-02 조회 2,995
12-03-02 조회 3,250
12-03-01 조회 5,034
12-02-29 조회 3,823
12-02-27 조회 4,224
12-02-27 조회 3,114
12-02-26 조회 2,845
12-02-26 조회 5,044
12-02-25 조회 2,590
12-02-23 조회 2,915
12-02-23 조회 2,680
12-02-22 조회 3,765
12-02-21 조회 2,793
12-02-20 조회 3,018
12-02-20 조회 2,859
12-02-20 조회 5,564
12-02-20 조회 3,473
12-02-19 조회 3,235
12-02-18 조회 2,963
12-02-17 조회 3,457
12-02-15 조회 3,008
12-02-14 조회 2,913
12-02-11 조회 2,885
12-02-09 조회 6,321
12-02-09 조회 3,005
12-02-06 조회 2,868
12-02-05 조회 2,881
12-02-04 조회 6,630
12-02-02 조회 5,267
12-01-31 조회 2,785
12-01-30 조회 2,723
12-01-30 조회 2,889
12-01-29 조회 3,100
12-01-27 조회 2,775
12-01-26 조회 2,824
12-01-25 조회 2,734