“Uncaught ReferenceError: DOM is not defined”

后端 未结 2 1632
-上瘾入骨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:35

    To expand on the previous answer, DOM is indeed part of the standard library in Observable. You can use it and the rest of the standard library methods outside of Observable by exporting or embedding a notebook, as described in the Downloading and Embedding Notebooks documentation page.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题