I am using Two.JS to render shapes to the stage which have been interpreted from an SVG using the Two.js interpret method.
These have an lifespan property added, and in Two's render loop I check for the illustration and remove it if the time is up.
This is working most of the time (>99%), but occasionally the shape gets stuck and I get this error:
Uncaught NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
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
and here is my loop to remove the illustrations:
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
here is the relevant code from two.js where the error is occurring.
removeChild: function(id) {
var elem = this.domElement.querySelector('#' + id);
if (elem) {
this.elem.removeChild(elem);
}
},
this only happens on interpreted svg's, not with shapes. both shapes and interpreted shapes return two.polygon objects so this seems odd.
One thing I can think of is that two.js uses the id of the element it interprets as the id of the polygon made, and if there are two elements with the same id then this causes an error when trying to remove. But the alreadyIllustration check seems to work correctly every time to stop adding illustrations if there are any existing.
I also tried setting the id to the time it was created instead of the id of the element so it was unique every time, but this causes other problems and errors.
Many thanks.