             thickness       : .4        # zero thickness disables the frame
             labels          : true      # whether or not to enable labels on the axes
 
-        math3d.load_threejs (error) =>
+        math3d.loadThreejs (error) =>
             if error
                 return @opts.callback? error
 


    attach_to_dom: (parent_element) ->
        if @element?
            remove_element @element
        else
            @element = document.createElement 'span'
            @element.className = 'math-3d-viewer'


rgb_to_hex = (rgb) -> '#'+rgb.map(rescale_to_hex).join('')

removeElement = (element) ->
    if element? and (parent = element.parentElement)?
        parent.removeChild element

loadScript = (script_src, callback) ->
    runCallback = true

    script = document.createElement 'script'

    script.onload = ->
            removeElement script
            if runCallback
                runCallback = false
                callback()
    script.onerror = ->
            removeElement script
            if runCallback
                runCallback = false
                callback "error loading script #{script.src}"

    script.type = 'text/javascript'
    script.charset = 'utf-8'
    script.async = true
    script.src = script_src

    document.head.appendChild script

_loadingThreejsCallbacks = []
_orbitControlsSetup = false

math3d.threejsSource = "//cdnjs.cloudflare.com/ajax/libs/three.js/r68/three.min.js"
math3d.fontSources = []

math3d.loadThreejs = (callback) ->
    if THREE? and _orbitControlsSetup and not math3d.fontsSources.length
        return callback()

    _loadingThreejsCallbacks.push callback
    if _loadingThreejsCallbacks.length > 1
        return

    runCallbacks = (error) ->
        while callback = _loadingThreejsCallbacks.shift()
            callback error

    setupOrbitControls = (callback) ->
        if not _orbitControlsSetup
            OrbitControls.prototype = Object.create THREE.EventDispatcher.prototype
            _orbitControlsSetup = true
        callback()

    setupFonts = (callback) ->
        if math3d.fontSources.length
            loadScript math3d.fontSources.shift(), (error) ->
                if error
                    runCallbacks error
                else
                    setupFonts callback
        else
            callback()

    setupThreejs = (callback) ->
        if THREE?
            callback()
        else
            loadScript math3d.threejsSource, (error) ->
                if error
                    runCallbacks error
                else
                    callback()

    setupThreejs (-> setupFonts (-> setupOrbitControls runCallbacks))

_scene_using_renderer = undefined
_renderer = {}
