0

Friends, I need your help, the following code in three.js, all return null in this line:

_context || _canvas.getContext( 'webgl', attributes ) || _canvas.getContext( 'experimental-webgl', attributes );


function initGL() {

    try {

        var attributes = {
            alpha: _alpha,
            depth: _depth,
            stencil: _stencil,
            antialias: _antialias,
            premultipliedAlpha: _premultipliedAlpha,
            preserveDrawingBuffer: _preserveDrawingBuffer
        };

        _gl = _context || _canvas.getContext( 'webgl', attributes ) || _canvas.getContext( 'experimental-webgl', attributes );

        if ( _gl === null ) {

            throw 'Error creating WebGL context.';

        }

    } catch ( error ) {

        console.error( error );

    }

    _glExtensionTextureFloat = _gl.getExtension( 'OES_texture_float' );

the attributes value:

enter image description here

this is canvas:

enter image description here

Thanks a lot!

~Rosone

2
  • Shouldn't you mention which browser and OS you're using? Is it one of the ones supported? Does it support WebGL? Commented Dec 18, 2014 at 13:16
  • win2008 server, chrome, threejs example can work. I'm reviewing the code. actually just:this.scene = new THREE.Scene(); this.camera = new THREE.PerspectiveCamera(75, this.canvas.width / this.canvas.height, 0.1, 1000); this.renderer = new THREE.WebGLRenderer({ canvas: this.canvas, antialias: true }); Commented Dec 19, 2014 at 2:53

1 Answer 1

2

I found the reason: It's because another context (canvas.getContext('2d') ) already occupied this same canvas.

Sign up to request clarification or add additional context in comments.

1 Comment

IMHO, this can happen simply by creating new <canvas> instances in different pages in a single-page app. Do your best to "destroy" the previous <canvas> element and instances related libraries (e.g. a 3d engine).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.