github.com/cybriq/giocore@v0.0.7-0.20210703034601-cfb9cb5f3900/gpu/headless/headless_js.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package headless
     4  
     5  import (
     6  	"errors"
     7  	"syscall/js"
     8  
     9  	"github.com/cybriq/giocore/gpu"
    10  	"github.com/cybriq/giocore/internal/gl"
    11  )
    12  
    13  type jsContext struct {
    14  	ctx js.Value
    15  }
    16  
    17  func newGLContext() (context, error) {
    18  	doc := js.Global().Get("document")
    19  	cnv := doc.Call("createElement", "canvas")
    20  	ctx := cnv.Call("getContext", "webgl2")
    21  	if ctx.IsNull() {
    22  		ctx = cnv.Call("getContext", "webgl")
    23  	}
    24  	if ctx.IsNull() {
    25  		return nil, errors.New("headless: webgl is not supported")
    26  	}
    27  	c := &jsContext{
    28  		ctx: ctx,
    29  	}
    30  	return c, nil
    31  }
    32  
    33  func (c *jsContext) API() gpu.API {
    34  	return gpu.OpenGL{Context: gl.Context(c.ctx)}
    35  }
    36  
    37  func (c *jsContext) Release() {
    38  }
    39  
    40  func (c *jsContext) ReleaseCurrent() {
    41  }
    42  
    43  func (c *jsContext) MakeCurrent() error {
    44  	return nil
    45  }