Skip to content

Commit f522b25

Browse files
Optimize modelTurnHead
1 parent 3414574 commit f522b25

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

src/cubism2/index.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ import MatrixStack from './utils/MatrixStack.js';
55
import LAppLive2DManager from './LAppLive2DManager.js';
66
import logger from '../logger.js';
77

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+
830
class Cubism2Model {
931
constructor() {
1032
this.live2DMgr = new LAppLive2DManager();
@@ -22,9 +44,6 @@ class Cubism2Model {
2244
this.drag = false;
2345
this.oldLen = 0;
2446

25-
this.lastMouseX = 0;
26-
this.lastMouseY = 0;
27-
2847
this._boundMouseEvent = this.mouseEvent.bind(this);
2948
this._boundTouchEvent = this.touchEvent.bind(this);
3049
}
@@ -37,10 +56,10 @@ class Cubism2Model {
3756
this.canvas.addEventListener('click', this._boundMouseEvent, false);
3857

3958
this.canvas.addEventListener('mousedown', this._boundMouseEvent, false);
40-
this.canvas.addEventListener('mousemove', this._boundMouseEvent, false);
59+
document.addEventListener('mousemove', this._boundMouseEvent, false);
4160

4261
this.canvas.addEventListener('mouseup', this._boundMouseEvent, false);
43-
this.canvas.addEventListener('mouseout', this._boundMouseEvent, false);
62+
document.addEventListener('mouseout', this._boundMouseEvent, false);
4463
this.canvas.addEventListener('contextmenu', this._boundMouseEvent, false);
4564

4665
this.canvas.addEventListener('touchstart', this._boundTouchEvent, false);
@@ -210,12 +229,11 @@ class Cubism2Model {
210229
modelTurnHead(event) {
211230
this.drag = true;
212231

213-
const rect = event.target.getBoundingClientRect();
232+
const rect = this.canvas.getBoundingClientRect();
214233

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;
219237

220238
logger.trace(
221239
'onMouseDown device( x:' +
@@ -229,19 +247,13 @@ class Cubism2Model {
229247
')',
230248
);
231249

232-
this.lastMouseX = sx;
233-
this.lastMouseY = sy;
234-
235250
this.dragMgr.setPoint(vx, vy);
236-
237251
this.live2DMgr.tapEvent(vx, vy);
238252
}
239253

240254
followPointer(event) {
241255
const rect = event.target.getBoundingClientRect();
242256

243-
const sx = this.transformScreenX(event.clientX - rect.left);
244-
const sy = this.transformScreenY(event.clientY - rect.top);
245257
const vx = this.transformViewX(event.clientX - rect.left);
246258
const vy = this.transformViewY(event.clientY - rect.top);
247259

@@ -258,9 +270,6 @@ class Cubism2Model {
258270
);
259271

260272
if (this.drag) {
261-
this.lastMouseX = sx;
262-
this.lastMouseY = sy;
263-
264273
this.dragMgr.setPoint(vx, vy);
265274
}
266275
}
@@ -288,16 +297,8 @@ class Cubism2Model {
288297

289298
if (e.wheelDelta > 0) this.modelScaling(1.1);
290299
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);
295300
} 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);
301302
} else if (e.type == 'mouseout') {
302303
this.lookFront();
303304
}

0 commit comments

Comments
 (0)