이동하는 글자

· 13년 전 · 875 · 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>
감사합니다!
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 1,584
13년 전 조회 504
13년 전 조회 533
13년 전 조회 784
13년 전 조회 542
13년 전 조회 1,451
13년 전 조회 438
13년 전 조회 482
13년 전 조회 789
13년 전 조회 585
13년 전 조회 508
13년 전 조회 652
13년 전 조회 650
13년 전 조회 497
13년 전 조회 2,308
13년 전 조회 1,406
13년 전 조회 1,298
13년 전 조회 505
13년 전 조회 467
13년 전 조회 554
13년 전 조회 783
13년 전 조회 580
13년 전 조회 1,157
13년 전 조회 1,238
13년 전 조회 832
13년 전 조회 943
13년 전 조회 1,988
13년 전 조회 2,294
13년 전 조회 5,579
13년 전 조회 485
13년 전 조회 927
13년 전 조회 487
13년 전 조회 691
13년 전 조회 1,353
13년 전 조회 905
13년 전 조회 552
13년 전 조회 657
13년 전 조회 488
13년 전 조회 631
13년 전 조회 1,562
13년 전 조회 1,911
13년 전 조회 784
13년 전 조회 1,014
13년 전 조회 2,107
13년 전 조회 1,015
13년 전 조회 1,055
13년 전 조회 745
13년 전 조회 2,105
13년 전 조회 728
13년 전 조회 876
13년 전 조회 1,201
13년 전 조회 1,359
13년 전 조회 2,961
13년 전 조회 3,182
13년 전 조회 685
13년 전 조회 1,183
13년 전 조회 671
13년 전 조회 1,243
13년 전 조회 1,029
13년 전 조회 913
13년 전 조회 1,210
13년 전 조회 495
13년 전 조회 1,888
13년 전 조회 1,629
13년 전 조회 1,367
13년 전 조회 809
13년 전 조회 1,282
13년 전 조회 1,489
13년 전 조회 1,104
13년 전 조회 2,280
13년 전 조회 936
13년 전 조회 670
13년 전 조회 861
13년 전 조회 3,646
13년 전 조회 2,526
13년 전 조회 1,080
13년 전 조회 1,248
13년 전 조회 2,647
13년 전 조회 1,656
13년 전 조회 808
13년 전 조회 875
13년 전 조회 2,273
13년 전 조회 774
13년 전 조회 1,020
13년 전 조회 523
13년 전 조회 669
13년 전 조회 1,402
13년 전 조회 2,946
13년 전 조회 713
13년 전 조회 960
13년 전 조회 1,352
13년 전 조회 772
13년 전 조회 2,397
13년 전 조회 990
13년 전 조회 1,671
13년 전 조회 1,904
13년 전 조회 1,727
13년 전 조회 1,046
13년 전 조회 1,328
13년 전 조회 843
🐛 버그신고