@@ -5,6 +5,28 @@ import MatrixStack from './utils/MatrixStack.js';
5
5
import LAppLive2DManager from './LAppLive2DManager.js' ;
6
6
import logger from '../logger.js' ;
7
7
8
+ function normalizePoint ( x , y , x0 , y0 , w , h ) {
9
+ const dx = x - x0 ;
10
+ const dy = y - y0 ;
11
+
12
+ let targetX = 0 , targetY = 0 ;
13
+
14
+ if ( dx >= 0 ) {
15
+ targetX = dx / ( w - x0 ) ;
16
+ } else {
17
+ targetX = dx / x0 ;
18
+ }
19
+ if ( dy >= 0 ) {
20
+ targetY = dy / ( h - y0 ) ;
21
+ } else {
22
+ targetY = dy / y0 ;
23
+ }
24
+ return {
25
+ x : targetX ,
26
+ y : - targetY
27
+ } ;
28
+ }
29
+
8
30
class Cubism2Model {
9
31
constructor ( ) {
10
32
this . live2DMgr = new LAppLive2DManager ( ) ;
@@ -22,9 +44,6 @@ class Cubism2Model {
22
44
this . drag = false ;
23
45
this . oldLen = 0 ;
24
46
25
- this . lastMouseX = 0 ;
26
- this . lastMouseY = 0 ;
27
-
28
47
this . _boundMouseEvent = this . mouseEvent . bind ( this ) ;
29
48
this . _boundTouchEvent = this . touchEvent . bind ( this ) ;
30
49
}
@@ -37,10 +56,10 @@ class Cubism2Model {
37
56
this . canvas . addEventListener ( 'click' , this . _boundMouseEvent , false ) ;
38
57
39
58
this . canvas . addEventListener ( 'mousedown' , this . _boundMouseEvent , false ) ;
40
- this . canvas . addEventListener ( 'mousemove' , this . _boundMouseEvent , false ) ;
59
+ document . addEventListener ( 'mousemove' , this . _boundMouseEvent , false ) ;
41
60
42
61
this . canvas . addEventListener ( 'mouseup' , this . _boundMouseEvent , false ) ;
43
- this . canvas . addEventListener ( 'mouseout' , this . _boundMouseEvent , false ) ;
62
+ document . addEventListener ( 'mouseout' , this . _boundMouseEvent , false ) ;
44
63
this . canvas . addEventListener ( 'contextmenu' , this . _boundMouseEvent , false ) ;
45
64
46
65
this . canvas . addEventListener ( 'touchstart' , this . _boundTouchEvent , false ) ;
@@ -210,12 +229,11 @@ class Cubism2Model {
210
229
modelTurnHead ( event ) {
211
230
this . drag = true ;
212
231
213
- const rect = event . target . getBoundingClientRect ( ) ;
232
+ const rect = this . canvas . getBoundingClientRect ( ) ;
214
233
215
- const sx = this . transformScreenX ( event . clientX - rect . left ) ;
216
- const sy = this . transformScreenY ( event . clientY - rect . top ) ;
217
- const vx = this . transformViewX ( event . clientX - rect . left ) ;
218
- const vy = this . transformViewY ( event . clientY - rect . top ) ;
234
+ const target = normalizePoint ( event . clientX , event . clientY , rect . left + rect . width / 2 , rect . top + rect . height / 2 , window . innerWidth , window . innerHeight ) ;
235
+ const vx = target . x ;
236
+ const vy = target . y ;
219
237
220
238
logger . trace (
221
239
'onMouseDown device( x:' +
@@ -229,19 +247,13 @@ class Cubism2Model {
229
247
')' ,
230
248
) ;
231
249
232
- this . lastMouseX = sx ;
233
- this . lastMouseY = sy ;
234
-
235
250
this . dragMgr . setPoint ( vx , vy ) ;
236
-
237
251
this . live2DMgr . tapEvent ( vx , vy ) ;
238
252
}
239
253
240
254
followPointer ( event ) {
241
255
const rect = event . target . getBoundingClientRect ( ) ;
242
256
243
- const sx = this . transformScreenX ( event . clientX - rect . left ) ;
244
- const sy = this . transformScreenY ( event . clientY - rect . top ) ;
245
257
const vx = this . transformViewX ( event . clientX - rect . left ) ;
246
258
const vy = this . transformViewY ( event . clientY - rect . top ) ;
247
259
@@ -258,9 +270,6 @@ class Cubism2Model {
258
270
) ;
259
271
260
272
if ( this . drag ) {
261
- this . lastMouseX = sx ;
262
- this . lastMouseY = sy ;
263
-
264
273
this . dragMgr . setPoint ( vx , vy ) ;
265
274
}
266
275
}
@@ -288,16 +297,8 @@ class Cubism2Model {
288
297
289
298
if ( e . wheelDelta > 0 ) this . modelScaling ( 1.1 ) ;
290
299
else this . modelScaling ( 0.9 ) ;
291
- } else if ( e . type == 'mousedown' ) {
292
- if ( 'button' in e && e . button != 0 ) return ;
293
-
294
- this . modelTurnHead ( e ) ;
295
300
} else if ( e . type == 'mousemove' ) {
296
- this . followPointer ( e ) ;
297
- } else if ( e . type == 'mouseup' ) {
298
- if ( 'button' in e && e . button != 0 ) return ;
299
-
300
- this . lookFront ( ) ;
301
+ this . modelTurnHead ( e ) ;
301
302
} else if ( e . type == 'mouseout' ) {
302
303
this . lookFront ( ) ;
303
304
}
0 commit comments