github.com/utopiagio/gio@v0.0.8/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/utopiagio/gio/gpu"
    10  	"github.com/utopiagio/gio/internal/gl"
    11  )
    12  
    13  type jsContext struct {
    14  	ctx js.Value
    15  }
    16  
    17  func init() {
    18  	newContextPrimary = func() (context, error) {
    19  		doc := js.Global().Get("document")
    20  		cnv := doc.Call("createElement", "canvas")
    21  		ctx := cnv.Call("getContext", "webgl2")
    22  		if ctx.IsNull() {
    23  			ctx = cnv.Call("getContext", "webgl")
    24  		}
    25  		if ctx.IsNull() {
    26  			return nil, errors.New("headless: webgl is not supported")
    27  		}
    28  		c := &jsContext{
    29  			ctx: ctx,
    30  		}
    31  		return c, nil
    32  	}
    33  }
    34  
    35  func (c *jsContext) API() gpu.API {
    36  	return gpu.OpenGL{Context: gl.Context(c.ctx)}
    37  }
    38  
    39  func (c *jsContext) Release() {
    40  }
    41  
    42  func (c *jsContext) ReleaseCurrent() {
    43  }
    44  
    45  func (c *jsContext) MakeCurrent() error {
    46  	return nil
    47  }