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

Commit aed9c26

Browse files
committed
fix: achieve frameshot, recover event system
1 parent 59909df commit aed9c26

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

src/cLive2DApp.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { MatrixStack } from "./utils/MatrixStack";
2626
import { cDefine } from "./cDefine";
2727

2828
let live2DMgr = new cManager();
29+
let captureFrameCB = undefined;
2930
let isDrawStart = false;
3031
let dragMgr = null;
3132
let viewMatrix = null;
@@ -48,9 +49,10 @@ let opacityHover = 1;
4849
function theRealInit (){
4950

5051
createElement();
52+
initEvent();
5153

5254
dragMgr = new L2DTargetPoint();
53-
let ratio = config.display.height / config.display.width;
55+
let ratio = currCanvas.height / currCanvas.width;
5456
let left = cDefine.VIEW_LOGICAL_LEFT;
5557
let right = cDefine.VIEW_LOGICAL_RIGHT;
5658
let bottom = -ratio;
@@ -66,41 +68,47 @@ function theRealInit (){
6668
cDefine.VIEW_LOGICAL_MAX_TOP);
6769

6870
projMatrix = new L2DMatrix44();
69-
projMatrix.multScale(1, (config.display.width / config.display.height));
71+
projMatrix.multScale(1, (currCanvas.width / currCanvas.height));
7072

7173
deviceToScreen = new L2DMatrix44();
72-
deviceToScreen.multTranslate(-config.display.width / 2.0, -config.display.height / 2.0); // #32
73-
deviceToScreen.multScale(2 / config.display.width, -2 / config.display.height); // #32
74+
deviceToScreen.multTranslate(-currCanvas.width / 2.0, -currCanvas.height / 2.0); // #32
75+
deviceToScreen.multScale(2 / currCanvas.width, -2 / currCanvas.height); // #32
7476

7577

7678
Live2D.setGL(currWebGL);
7779
currWebGL.clearColor(0.0, 0.0, 0.0, 0.0);
7880
changeModel(config.model.jsonPath);
7981
startDraw();
8082

83+
8184
}
85+
8286
/**
83-
* Return the data URI of current frame, MINE type is image/png.
84-
* @return {DOMString} Which contains data URI, MINE type is image/png
87+
* Capture current frame to png file
88+
* @param {Function} callback The callback function which will receive the current frame
89+
* @return {null}
8590
* @example
8691
* You can use codes below to let the user download the current frame
8792
*
88-
* let link = document.createElement('a');
89-
* document.body.appendChild(link);
90-
* link.setAttribute('type', 'hidden');
91-
* link.href = L2Dwidget.captureFrame();
92-
* link.download = 'live2d.png';
93-
* link.click();
93+
* L2Dwidget.captureFrame(
94+
* function(e){
95+
* let link = document.createElement('a');
96+
* document.body.appendChild(link);
97+
* link.setAttribute('type', 'hidden');
98+
* link.href = e;
99+
* link.download = 'live2d.png';
100+
* link.click();
101+
* }
102+
* );
94103
*
95104
* @description Thanks to @journey-ad https://github.com/journey-ad/live2d_src/commit/97356a19f93d2abd83966f032a53b5ca1109fbc3
96-
* @todo Seems feedback empty image only
97105
*/
98106

99-
function captureFrame(){
100-
return currCanvas.toDataURL();
107+
function captureFrame(callback){
108+
captureFrameCB = callback;
101109
}
102110

103-
function initEvent(){/*
111+
function initEvent(){
104112
if (currCanvas.addEventListener) {
105113
window.addEventListener("click", mouseEvent);
106114
window.addEventListener("mousedown", mouseEvent);
@@ -110,7 +118,7 @@ function initEvent(){/*
110118
window.addEventListener("touchstart", touchEvent);
111119
window.addEventListener("touchend", touchEvent);
112120
window.addEventListener("touchmove", touchEvent);
113-
}*/
121+
}
114122
}
115123

116124
function startDraw() {
@@ -125,6 +133,10 @@ function startDraw() {
125133
window.msRequestAnimationFrame;
126134

127135
requestAnimationFrame(tick, currCanvas);
136+
if(captureFrameCB !== undefined){
137+
captureFrameCB(currCanvas.toDataURL());
138+
captureFrameCB = undefined;
139+
}
128140
})();
129141
}
130142
}

src/config/configMgr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let currConfig = {};
2222

2323
const defaultConfig = {
2424
model: {
25-
jsonPath: 'https://unpkg.com/live2d-widget-model-shizuku@1.0.0/assets/shizuku.model.json',
25+
jsonPath: 'https://unpkg.com/live2d-widget-model-shizuku@latest/assets/shizuku.model.json',
2626
scale: 1,
2727
hHeadPos: 0.5,
2828
vHeadPos: 0.618,
@@ -76,7 +76,7 @@ function configApplyer(userConfig){
7676
// }
7777

7878
currConfig = _.defaultsDeep(userConfig, defaultConfig);
79-
console.log('currConfig:', currConfig);
79+
// console.log('Live2Dwidget: currConfig', currConfig);
8080

8181
}
8282

src/wpPublicPath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function getCurrentPath(){
9797
// and wp will finish the following work
9898
__webpack_public_path__ = getCurrentPath().replace(/[^/\\\\]+$/, '');
9999
if (process.env.NODE_ENV === 'development'){
100-
console.log(`wpPP: publicPath: ${__webpack_public_path__}`);
100+
console.log(`Live2Dwidget: publicPath: ${__webpack_public_path__}`);
101101
}
102102

103103
export {

0 commit comments

Comments
 (0)