index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Tencent is pleased to support the open source community by making vap available.
  3. *
  4. * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the MIT License (the "License"); you may not use this file except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. *
  9. * http://opensource.org/licenses/MIT
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed under the License is
  12. * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. * either express or implied. See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import WebglRenderVap from './webgl-render-vap'
  17. let isCanWebGL
  18. /**
  19. * @param options
  20. * @constructor
  21. * @return {null}
  22. */
  23. export default function(options) {
  24. if (canWebGL()) {
  25. return new WebglRenderVap(Object.assign({}, options))
  26. } else {
  27. throw new Error('your browser not support webgl')
  28. }
  29. }
  30. function canWebGL() {
  31. if (typeof isCanWebGL !== 'undefined') {
  32. return isCanWebGL
  33. }
  34. try {
  35. if (!window.WebGLRenderingContext) {
  36. return false
  37. }
  38. const canvas = document.createElement('canvas')
  39. let context = canvas.getContext('webgl') || canvas.getContext('experimental-webgl')
  40. isCanWebGL = !!context
  41. context = null
  42. } catch (err) {
  43. isCanWebGL = false
  44. }
  45. return isCanWebGL
  46. }