context_webgl_wasm.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //go:build js && wasm
  2. // +build js,wasm
  3. package glfw
  4. import (
  5. "errors"
  6. "syscall/js"
  7. )
  8. func newContext(canvas js.Value, ca *contextAttributes) (context js.Value, err error) {
  9. if js.Global().Get("WebGLRenderingContext").Equal(js.Undefined()) {
  10. return js.Value{}, errors.New("Your browser doesn't appear to support WebGL.")
  11. }
  12. attrs := map[string]interface{}{
  13. "alpha": ca.Alpha,
  14. "depth": ca.Depth,
  15. "stencil": ca.Stencil,
  16. "antialias": ca.Antialias,
  17. "premultipliedAlpha": ca.PremultipliedAlpha,
  18. "preserveDrawingBuffer": ca.PreserveDrawingBuffer,
  19. "preferLowPowerToHighPerformance": ca.PreferLowPowerToHighPerformance,
  20. "failIfMajorPerformanceCaveat": ca.FailIfMajorPerformanceCaveat,
  21. }
  22. if gl := canvas.Call("getContext", "webgl", attrs); !gl.Equal(js.Null()) {
  23. debug := js.Global().Get("WebGLDebugUtils")
  24. if debug.Equal(js.Undefined()) {
  25. return gl, errors.New("No debugging for WebGL.")
  26. }
  27. gl = debug.Call("makeDebugContext", gl)
  28. return gl, nil
  29. } else if gl := canvas.Call("getContext", "experimental-webgl", attrs); gl.Equal(js.Null()) {
  30. return gl, nil
  31. } else {
  32. return js.Value{}, errors.New("Creating a WebGL context has failed.")
  33. }
  34. }
  35. type contextAttributes struct {
  36. Alpha bool
  37. Depth bool
  38. Stencil bool
  39. Antialias bool
  40. PremultipliedAlpha bool
  41. PreserveDrawingBuffer bool
  42. PreferLowPowerToHighPerformance bool
  43. FailIfMajorPerformanceCaveat bool
  44. }
  45. func defaultAttributes() *contextAttributes {
  46. return &contextAttributes{
  47. Alpha: false,
  48. Depth: true,
  49. Stencil: false,
  50. Antialias: false,
  51. PremultipliedAlpha: false,
  52. PreserveDrawingBuffer: false,
  53. }
  54. }