“Uncaught ReferenceError: DOM is not defined”

后端 未结 2 1634
-上瘾入骨i
-上瘾入骨i 2020-12-19 13:17

I understand this line of D3 code which adds an SVG element to the body of an HTML page, and stores a reference to the new element in the variable \'svg\':



        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 13:51

    That's neither a standard Javascript object nor a D3 method. That's an Observable method.

    If you look at the introduction, you'll see that DOM is a collection of functions:

    Object {
      canvas: ƒ(e, t)
      context2d: ƒ(e, t, n)
      download: ƒ(…)
      element: ƒ(e, t)
      input: ƒ(e)
      range: ƒ(e, t, n)
      select: ƒ(e)
      svg: ƒ(e, t)
      text: ƒ(e)
      uid: ƒ(e)
    }
    

    So, in an Observable notebook, one can do...

    DOM.text("I am a text node.")
    

    ...to create a text node or, as you just found,

    DOM.svg(500, 50)
    

    ...to create an SVG. However, that only works in an Observable notebook.

提交回复
热议问题