index.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 { VapConfig } from './type';
  17. import WebglRenderVap from './webgl-render-vap';
  18. let isCanWebGL: boolean;
  19. /**
  20. * @param options
  21. * @constructor
  22. * @return {null}
  23. */
  24. export default function (options?: VapConfig) {
  25. if (canWebGL()) {
  26. return new WebglRenderVap(options);
  27. } else {
  28. throw new Error('your browser not support webgl');
  29. }
  30. }
  31. export function canWebGL(): boolean {
  32. if (typeof isCanWebGL !== 'undefined') {
  33. return isCanWebGL;
  34. }
  35. try {
  36. // @ts-ignore
  37. if (!window.WebGLRenderingContext) {
  38. return false;
  39. }
  40. const canvas = document.createElement('canvas');
  41. let context = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
  42. isCanWebGL = !!context;
  43. context = null;
  44. } catch (err) {
  45. isCanWebGL = false;
  46. }
  47. return isCanWebGL;
  48. }