我正在使用Two.JS將形狀呈現到已使用Two.js從SVG解釋的舞臺上解釋方法。Two.JS:'嘗試刪除從SVG解釋的形狀時,無法在'Node'上執行'removeChild'
這些有添加壽命屬性,並在二的渲染循環我檢查插圖,並刪除它,如果時間到了。
這是工作的大部分時間(> 99%),但偶爾的形狀被卡住,我得到這個錯誤:
未捕獲NotFoundError:未能執行上「節點」 removeChild之「:節點被刪除的不是該節點的子節點。
showIllustration: (which) =>
alreadyIllustration = false
for shape, i in @_shapes
if shape.isIllustration is true
alreadyIllustration = true
if alreadyIllustration is false
switch which
when 'food'
if Math.random() > 0.49
id = 'currywurst'
else
id = 'pretzel'
when 'mascot'
if Math.random() > 0.49
id = 'ample'
else
id = 'bear'
when 'landmark'
if Math.random() > 0.49
id = 'tower'
else
id = 'tor'
illustration = @_two.interpret document.getElementById id
illustration.center().translation.set @_two.width/2, @_two.height/2
@_foreGround.add illustration
illustration.lifeSpan = 100
illustration.creationTime = new Date().getTime()
illustration.isIllustration = true
@_shapes.push illustration
,這裏是我的循環刪除插圖:
removeShapes:() =>
time = new Date().getTime()
for shape, i in @_shapes by -1
if shape.lifeSpan
if time - shape.creationTime >= shape.lifeSpan
shape.remove()
@_shapes.splice i, 1
這裏是two.js相關的代碼,其中的錯誤發生。
removeChild: function(id) {
var elem = this.domElement.querySelector('#' + id);
if (elem) {
this.elem.removeChild(elem);
}
},
這隻發生在解釋的svg的,而不是形狀。這兩種形狀和解釋形狀返回two.polygon對象,所以這似乎很奇怪。
我能想到的一件事是,two.js使用它所解釋的元素的id作爲多邊形的id,並且如果有兩個元素具有相同的id,那麼這會在嘗試刪除時導致錯誤。但是,如果有任何存在的話,那麼已經進行的插圖檢查似乎每次都能正確地工作以停止添加插圖。
我也嘗試將id設置爲創建時間而不是元素的id,因此每次都是唯一的,但這會導致其他問題和錯誤。
非常感謝。
它看起來像你有一個類似的問題,因爲以前的帖子http://stackoverflow.com/questions/24155757/some-shapes-not-being-removed-in-two-js-when-looping-through-array通過遍歷正在修改的數組遍歷所有形狀。無論如何,我認爲這可能是Two.js中的錯誤,因爲它看起來像要移除的元素不是該組的孩子。我現在要解決這個問題。 – jonobr1
master分支上的最新版本的two.js應該有這樣的解決方法:https://raw.githubusercontent.com/jonobr1/two.js/master/build/two.js給出一個嘗試 - 它不應該' t錯誤了。 – jonobr1
真棒...爲客戶服務如何。我會在今晚發佈它,讓你知道它是否修復了它。 –