context_webgl.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //go:build js && !wasm
  2. // +build js,!wasm
  3. package glfw
  4. import (
  5. "errors"
  6. "github.com/gopherjs/gopherjs/js"
  7. )
  8. func newContext(canvas *js.Object, ca *contextAttributes) (context *js.Object, err error) {
  9. if js.Global.Get("WebGLRenderingContext") == js.Undefined {
  10. return nil, errors.New("Your browser doesn't appear to support WebGL.")
  11. }
  12. attrs := map[string]bool{
  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 != nil {
  23. return gl, nil
  24. } else if gl := canvas.Call("getContext", "experimental-webgl", attrs); gl != nil {
  25. return gl, nil
  26. } else {
  27. return nil, errors.New("Creating a WebGL context has failed.")
  28. }
  29. }
  30. type contextAttributes struct {
  31. Alpha bool
  32. Depth bool
  33. Stencil bool
  34. Antialias bool
  35. PremultipliedAlpha bool
  36. PreserveDrawingBuffer bool
  37. PreferLowPowerToHighPerformance bool
  38. FailIfMajorPerformanceCaveat bool
  39. }
  40. func defaultAttributes() *contextAttributes {
  41. return &contextAttributes{
  42. Alpha: false,
  43. Depth: true,
  44. Stencil: false,
  45. Antialias: false,
  46. PremultipliedAlpha: false,
  47. PreserveDrawingBuffer: false,
  48. }
  49. }