我花了大約12個小時看這個代碼,並擺弄它,試圖找出哪裏有遞歸問題,因爲我得到了「最大調用堆棧大小超過「,錯誤,並沒有找到它。有人比我聰明,請幫助我!超過最大調用堆棧大小 - 沒有明顯的遞歸
到目前爲止,我發現的是,當我將對象,spot
,一個circle
,對象,問題消失,但是當我使它成爲'點',我得到這個堆棧溢出錯誤。我用friggin'顯微鏡去了pip課,但仍然不知道爲什麼會發生這種情況!
var canvas = document.getElementById('myCanvas');
//-------------------------------------------------------------------------------------
// Classes
//-------------------------------------------------------------------------------------
//=====================================================================================
//CLASS - point
function point(x,y){
this.x = x;
this.y = y;
}
//=====================================================================================
// CLASS - drawableItem
function drawableItem() {
var size = 0;
this.center = new point(0,0);
this.lineWidth = 1;
this.dependentDrawableItems = new Array();
}
//returns the size
drawableItem.prototype.getSize = function getSize(){
return this.size;
}
// changes the size of this item and the relative size of all dependents
drawableItem.prototype.changeSize = function(newSize){
var relativeItemSizes = new Array;
relativeItemSizes.length = this.dependentDrawableItems.length;
// get the relative size of all dependent items
for (var i = 0; i < this.dependentDrawableItems.length; i++){
relativeItemSizes[i] = this.dependentDrawableItems[i].getSize()/this.size;
}
// change the size
this.size = newSize;
// apply the ratio of change back to all dependent items
for (var i = 0; i < relativeItemSizes.length; i++){
this.dependentDrawableItems[i].changeSize(relativeItemSizes[i] * newSize);
}
}
//moves all the vertices and every dependent to an absolute point based on center
drawableItem.prototype.moveTo = function(moveX,moveY){
//record relative coordinates
var relativeItems = new Array;
relativeItems.length = this.dependentDrawableItems.length;
for (var i = 0; i < relativeItems.length; i++){
relativeItems[i] = new point;
relativeItems[i].x = this.dependentDrawableItems[i].center.x - this.center.x;
relativeItems[i].y = this.dependentDrawableItems[i].center.y - this.center.y;
}
//move the center
this.center.x = moveX;
this.center.y = moveY;
//move all the items relative to the center
for (var i = 0; i < relativeItems.length; i++){
this.dependentDrawableItems[i].moveItemTo(this.center.x + relativeItems[i].x,
this.center.y + relativeItems[i].y);
}
}
// draws every object in dependentDrawableItems
drawableItem.prototype.draw = function(ctx){
for (var i = 0; i < this.dependentDrawableItems.length; i++) {
this.dependentDrawableItems[i].draw(ctx);
}
}
//=====================================================================================
//CLASS - circle
function circle(isFilledCircle){
drawableItem.call(this);
this.isFilled = isFilledCircle
}
circle.prototype = new drawableItem();
circle.prototype.parent = drawableItem.prototype;
circle.prototype.constructor = circle;
circle.prototype.draw = function(ctx){
ctx.moveTo(this.center.x,this.center.y);
ctx.beginPath();
ctx.arc(this.center.x, this.center.y, this.size, 0, 2*Math.PI);
ctx.closePath();
ctx.lineWidth = this.lineWidth;
ctx.strokeStyle = this.outlineColor;
if (this.isFilled === true){
ctx.fill();
}else {
ctx.stroke();
}
this.parent.draw.call(this,ctx);
}
//=====================================================================================
//CLASS - pip
function pip(size){
circle.call(this,true);
}
pip.prototype = new circle(false);
pip.prototype.parent = circle.prototype;
pip.prototype.constructor = pip;
//----------------------------------------------------------------------
// Objects/variables - top layer is last (except drawable area is first)
//----------------------------------------------------------------------
var drawableArea = new drawableItem();
var spot = new pip();
spot.changeSize(20);
drawableArea.dependentDrawableItems[drawableArea.dependentDrawableItems.length] = spot;
//------------------------------------------
// Draw loop
//------------------------------------------
function drawScreen() {
var context = canvas.getContext('2d');
context.canvas.width = window.innerWidth;
context.canvas.height = window.innerHeight;
spot.moveTo(context.canvas.width/2, context.canvas.height/2);
drawableArea.draw(context);
}
window.addEventListener('resize', drawScreen);
這裏的演示:http://jsfiddle.net/DSU8w/
還不夠好的答案,但我有一個偷偷摸摸的懷疑你有一些非構造函數試圖作爲構造函數。 Google閉包拋出了一些關於'Property getSize never defined'的東西,一些函數不是構造函數!看看http://stackoverflow.com/a/5991265/588079和http://stackoverflow.com/a/4613017/588079和http://stackoverflow.com/q/6095530/588079。看看是否有幫助,否則試着削減代碼的一部分,直到你得到這個溢出的最小值,然後把它分享到一個小提琴中。 – GitaarLAB
***警告***(對於同行專家):這段代碼可能會導致瀏覽器崩潰(我的兩次崩潰),所以如果你有重要的東西打開/不打開(例如其他jsfiddle),請不要隨意擺弄它。 @jgrant:儘量不要讓小提琴自動運行,而是在按下按鈕之類的簡單用戶操作之後,這樣人們就有機會真正地加載小提琴並在發生崩潰之前檢查源代碼。 – GitaarLAB
我想「drawableItem.prototype.changeSize」導致你的問題。 –