context_webgl_wasm.go 1.8 KB

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