Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit e6433e3

Browse files
committed
feat: elementMgr codes, modify API
BREAKING CHANGE: change userConfig.display.AA to userConfig.display.antialias
1 parent d3c7ab5 commit e6433e3

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

src/config/configMgr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const defaultConfig = {
2929
myDefine: [],
3030
},
3131
display: {
32-
AA: 2,
32+
antialias: 2,
3333
widght: 150,
3434
height: 300,
3535
position: 'right',

src/config/configValidater.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const configPropTypes = {
2424
myDefine: PropTypes.array,
2525
}),
2626
display: PropTypes.shape({
27-
AA: PropTypes.number,
27+
antialias: PropTypes.number,
2828
widght: PropTypes.number,
2929
height: PropTypes.number,
3030
scale: PropTypes.number,

src/elementManager.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,68 @@
44

55

66
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+
*/
813

914
let currWebGL = undefined;
15+
16+
/**
17+
* The current canvas element
18+
* @type {DOMString}
19+
*/
20+
1021
let currCanvas;
1122

12-
function createCanvas(){
23+
/**
24+
* Create the canvas and styles using DOM
25+
* @return {null}
26+
*/
27+
28+
function createElement(){
1329

1430
let newElem = document.createElement('div');
1531
newElem.id = config.name.div;
1632
newElem.innerHTML = htmlTemplate(config);
1733
document.body.appendChild(newElem);
34+
currCanvas = document.getElementById(config.name.canvas);
35+
initWebGL();
1836

1937
}
2038

21-
function getCurrCanvas(){
22-
return currCanvas;
23-
}
24-
2539
/**
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
2841
* @return {null}
2942
*/
3043

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(){
4345

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.');
5362
}
54-
return null;
63+
return;
64+
}
5565
};
5666

5767

5868
export{
59-
createCanvas,
60-
getCurrCanvas,
61-
setCurrWebGL,
62-
getCurrWebGL,
69+
createElement,
70+
currWebGL,
6371
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if (process.env.NODE_ENV === 'development'){
2626
* @param {Number} [userConfig.model.hHeadPos = 0.5] Horizontal position of model's head 模型头部横坐标
2727
* @param {Number} [userConfig.model.vHeadPos = 0.618] Vertical position of model's head 模型头部纵坐标
2828
* @param {Array} [userConfig.model.myDefine = []] User's custom Defines which will override LDefine 自定义的LDefine
29-
* @param {Number} [userConfig.display.AA = 2] Antialiasing grade 抗锯齿
29+
* @param {Number} [userConfig.display.antialias = 2] Antialiasing grade 抗锯齿级别
3030
* @param {Number} [userConfig.display.widget = 150] Widget to the canvas which shows the model canvas的长度
3131
* @param {Number} [userConfig.display.height = 300] Height to the canvas which shows the model canvas的高度
3232
* @param {String} [userConfig.display.position = 'right'] Left of right side to show 显示位置:左或右

0 commit comments

Comments
 (0)