나는 함수 푸시의 사용법과 그것이 어떻게 도움이되는지 이해할 수 없다. 1 - 회선 코드가 필요한 이유가 무엇입니까?왜 푸시 기능이 필요합니까?
circles.push(newCircle);
2 -이 코드를 html 파일에 복사하고 코드가 실행되고 있지 않습니다. 들으
<html>
<head>
<title>Your title here</title>
<script type = "text/javascript" language = "Javascript">
<!-- Hide from older browsers;
var svgns = 'http://www.w3.org/2000/svg';
var svgElement = document.createElementNS(svgns, 'svg');
document.body.appendChild(svgElement);
var Circle = function(x,y,size){
this.element = document.createElementNS(svgns, 'circle');
this.x = x;
this.y = y;
this.size = size;
this.dx = 10*(Math.random()-0.5);
this.dy = 10*(Math.random()-0.5);
this.element.setAttribute('cx', this.x+'px');
this.element.setAttribute('cy', this.y+'px');
this.element.setAttribute('r', this.size+'px');
this.element.setAttribute('stroke', 'black');
this.element.setAttribute('stroke-width', '2px');
this.element.setAttribute('fill', 'red');
svgElement.appendChild(this.element);
};
Circle.prototype.update = function(){
this.x += this.dx;
this.y += this.dy;
this.element.setAttribute('cx', this.x+'px');
this.element.setAttribute('cy', this.y+'px');
};
var circles = [];
for (var i = 0; i< 10; i++) {
var newCircle = new Circle(100,100,10);
circles.push(newCircle);
}
window.setInterval(function(){
for (var i = 0; i< circles.length; i++) {
circles[i].update();
}
}, 30);
// end hide -->
</script>
</head>
<body>
<!-- Insert HTML here -->
</body>
</html>
보이는 :
이 함께 라인
document.body.appendChild(svgElement);
를 교체로드 이벤트를 기다립니다? – DdD실제로 작동합니다. http://jsfiddle.net/eU32w/ – DdD
@DimitriAdamou 상단 블록의 내용이 '
...'에 포함되어 있기 때문에 바이올린이 작동하고 있습니다. –