|
4 | 4 |
|
5 | 5 |
|
6 | 6 | import { config } from './config/configMgr'; |
7 | | -import { htmlTemplate } from './tmplate/innerHTML'; |
| 7 | +import htmlTemplate from './tmplate/innerHTML'; |
| 8 | + |
| 9 | +/** |
| 10 | + * The current WebGL element |
| 11 | + * @type {RenderingContext} |
| 12 | + */ |
8 | 13 |
|
9 | 14 | let currWebGL = undefined; |
| 15 | + |
| 16 | +/** |
| 17 | + * The current canvas element |
| 18 | + * @type {DOMString} |
| 19 | + */ |
| 20 | + |
10 | 21 | let currCanvas; |
11 | 22 |
|
12 | | -function createCanvas(){ |
| 23 | +/** |
| 24 | + * Create the canvas and styles using DOM |
| 25 | + * @return {null} |
| 26 | + */ |
| 27 | + |
| 28 | +function createElement(){ |
13 | 29 |
|
14 | 30 | let newElem = document.createElement('div'); |
15 | 31 | newElem.id = config.name.div; |
16 | 32 | newElem.innerHTML = htmlTemplate(config); |
17 | 33 | document.body.appendChild(newElem); |
| 34 | + currCanvas = document.getElementById(config.name.canvas); |
| 35 | + initWebGL(); |
18 | 36 |
|
19 | 37 | } |
20 | 38 |
|
21 | | -function getCurrCanvas(){ |
22 | | - return currCanvas; |
23 | | -} |
24 | | - |
25 | 39 | /** |
26 | | - * set the current WebGL element to the container |
27 | | - * @param {RenderingContext } e The WebGL element to be set |
| 40 | + * Find and set the current WebGL element to the container |
28 | 41 | * @return {null} |
29 | 42 | */ |
30 | 43 |
|
31 | | -function setCurrWebGL(e){ |
32 | | - currWebGL = e; |
33 | | -} |
34 | | - |
35 | | -/** |
36 | | - * get the current WebGL element in the container |
37 | | - * @return {RenderingContext} The current WebGL element |
38 | | - */ |
39 | | - |
40 | | -function getCurrWebGL(){ |
41 | | - return currWebGL; |
42 | | -} |
| 44 | +function initWebGL(){ |
43 | 45 |
|
44 | | -function getWebGLContext() |
45 | | -{ |
46 | | - var NAMES = [ "webgl" , "experimental-webgl" , "webkit-3d" , "moz-webgl"]; |
47 | | - for( var i = 0; i < NAMES.length; i++ ){ |
48 | | - try{ |
49 | | - var ctx = canvas.getContext(NAMES[i], {premultipliedAlpha : true}); |
50 | | - if(ctx) return ctx; |
51 | | - } |
52 | | - catch(e){} |
| 46 | + var NAMES = ['webgl2', 'webgl', 'experimental-webgl2', 'experimental-webgl', 'webkit-3d', 'moz-webgl']; |
| 47 | + for(let i = 0; i < NAMES.length; i++){ |
| 48 | + try{ |
| 49 | + let ctx = currCanvas.getContext(NAMES[i], { |
| 50 | + alpha: true, |
| 51 | + antialias: true, |
| 52 | + premultipliedAlpha: true, |
| 53 | + failIfMajorPerformanceCaveat: false, |
| 54 | + }); |
| 55 | + if(ctx) currWebGL = ctx; |
| 56 | + }catch(e){} |
| 57 | + } |
| 58 | + if(!gl){ |
| 59 | + console.error('Live2D widgets: Failed to create WebGL context.'); |
| 60 | + if(!window.WebGLRenderingContext){ |
| 61 | + console.error('Your browser may not support WebGL, check https://get.webgl.org/ for futher information.'); |
53 | 62 | } |
54 | | - return null; |
| 63 | + return; |
| 64 | + } |
55 | 65 | }; |
56 | 66 |
|
57 | 67 |
|
58 | 68 | export{ |
59 | | - createCanvas, |
60 | | - getCurrCanvas, |
61 | | - setCurrWebGL, |
62 | | - getCurrWebGL, |
| 69 | + createElement, |
| 70 | + currWebGL, |
63 | 71 | } |
0 commit comments