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

Commit 59909df

Browse files
committed
style: change L2Dwidget into Class
1 parent a2d04e8 commit 59909df

File tree

1 file changed

+43
-32
lines changed

1 file changed

+43
-32
lines changed

src/index.js

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ import device from 'current-device';
1515
import { config, configApplyer }from './config/configMgr';
1616

1717
if (process.env.NODE_ENV === 'development'){
18-
console.log('--- --- --- --- ---\nHey that, notice that you are now in DEV MODE.\n--- --- --- --- ---');
18+
console.log('--- --- --- --- ---\nLive2Dwidget: Hey that, notice that you are now in DEV MODE.\n--- --- --- --- ---');
1919
}
2020

2121
let coreApp;
2222
/**
23-
* The main entry point, which ... is nothing
23+
* The main entry point, which is ... nothing
2424
*/
2525

26-
function L2Dwidget(){};
26+
class L2Dwidget{
2727

2828
/**
29-
* The public entry point
29+
* The init function
3030
* @param {Object} [userConfig] User's custom config 用户自定义设置
3131
* @param {String} [userConfig.model.jsonPath = ''] Path to Live2D model's main json eg. `https://test.com/miku.model.json` model主文件路径
3232
* @param {Number} [userConfig.model.scale = 1] Scale between the model and the canvas 模型与canvas的缩放
@@ -54,42 +54,53 @@ function L2Dwidget(){};
5454
* @return {null}
5555
*/
5656

57-
L2Dwidget.init = (userConfig) => {
57+
init(userConfig = {}){
58+
configApplyer(userConfig);
59+
if((!config.mobile.show)&&(device.mobile())){
60+
return;
61+
}
62+
import(/* webpackMode: 'lazy' */ './cLive2DApp').then(f => {
63+
coreApp = f;
64+
coreApp.theRealInit();
65+
}).catch(err => {
66+
console.error(err);
67+
});
68+
}
5869

59-
userConfig = typeof userConfig === 'undefined' ? {} : userConfig;
6070

61-
configApplyer(userConfig);
71+
/**
72+
* Capture current frame to png file {@link captureFrame}
73+
* @param {Function} callback The callback function which will receive the current frame
74+
* @return {null}
75+
*/
6276

63-
if((!config.mobile.show)&&(device.mobile())){
64-
return;
77+
captureFrame(callback){
78+
return coreApp.captureFrame(callback);
6579
}
6680

67-
import(/* webpackMode: 'lazy' */ './cLive2DApp').then(f => {
68-
coreApp = f;
69-
coreApp.theRealInit();
70-
}).catch(err => {
71-
console.error(err);
72-
});
73-
74-
}
75-
7681
/**
77-
* Return the data URI of current frame, MINE type is image/png.
78-
* @return {DOMString} Which contains data URI, MINE type is image/png
79-
* @example
80-
* You can use codes below to let the user download the current frame
81-
*
82-
* let link = document.createElement('a');
83-
* link.innerHTML = 'Download image';
84-
* link.href = L2Dwidget.captureFrame();
85-
* link.download = 'live2d.png';
86-
* link.click();
87-
*
88-
* @description Thanks to @journey-ad https://github.com/journey-ad/live2d_src/commit/97356a19f93d2abd83966f032a53b5ca1109fbc3
82+
* download current frame {@link L2Dwidget.captureFrame}
83+
* @return {null}
8984
*/
9085

91-
L2Dwidget.captureFrame = () => {return coreApp.captureFrame()};
86+
downloadFrame(){
87+
this.captureFrame(
88+
function(e){
89+
let link = document.createElement('a');
90+
document.body.appendChild(link);
91+
link.setAttribute('type', 'hidden');
92+
link.href = e;
93+
link.download = 'live2d.png';
94+
link.click();
95+
}
96+
);
97+
}
98+
99+
};
100+
101+
let _ = new L2Dwidget();
102+
92103

93104
export {
94-
L2Dwidget,
105+
_ as L2Dwidget,
95106
}

0 commit comments

Comments
 (0)