I'm trying to add a class and ID to specific Two.js objects in this project: http://itpblog.evejweinberg.com/Homework/Open/ (click a few times to play) If I console.log the word 'background' I see that these are two.js objects but I can't seem to use css or jquery to add a class/ID to them.
Any help is appreciated!
I even tried adding it to the whole project but that did not work:
$(document.body).addClass("dropshadow");
two.js entities are not DOM elements themselves, but each Scene, Group, or Polygon contains at least one reference to the DOM element that gets drawn when the entity is changed. To reference various DOM elements use the following syntaxes:
Here's a screenshot showing all three commands in an actual app along with the outcome of each, with one additional command add a class to the shape that was targeted. The syntax of the last command differs but I omitted the
var
statements so that the console would display the result instead of outputtingundefined
.If you'd like to create custom HTML IDs for individual shapes, use the
.id
setter before the initial render of your shape. Since most of this code is just setup, I offer a practical example on one of my own projects. In that snippet, ashape
variable holds a new instance ofTwo.Polygon
so callingshape.id = 'something-unique'
before callingtwo.update()
to draw the shape for the first time results in a DOM element with a custom HTML ID. Here is an incomplete block of setup code showing how to set the ID: