@@ -26,6 +26,7 @@ import { MatrixStack } from "./utils/MatrixStack";
2626import { cDefine } from "./cDefine" ;
2727
2828let live2DMgr = new cManager ( ) ;
29+ let captureFrameCB = undefined ;
2930let isDrawStart = false ;
3031let dragMgr = null ;
3132let viewMatrix = null ;
@@ -48,9 +49,10 @@ let opacityHover = 1;
4849function 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
116124function 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}
0 commit comments