Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request #45 from journey-ad/master
Browse files Browse the repository at this point in the history
增加对点击事件的处理
  • Loading branch information
xiazeyu committed Nov 24, 2017
2 parents 7562ab0 + 8535f93 commit cdea4ba
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
3 changes: 2 additions & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bundle.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
</canvas>
</div>
<script type="text/javascript" src="device.min.js"></script>
<script type="text/javascript" src="bundle.js?64fe0900cd19ddfbda5b"></script></body>
<script type="text/javascript" src="bundle.js?0f7f527630fd90639e82"></script></body>
</html>
<!-- loadlive2d("live2dcanvas", "../assets/name/name.model.json", 0.5); -->
27 changes: 13 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,32 @@ hexo.extend.helper.register('live2d', function() {
<style>
#${config.id}{
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
ms-user-select: none;
user-select: none;
position: fixed;
width: ${config.width}px;
height: ${config.height}px;
opacity:${config.opacityDefault};
${config.opacityDefault != config.opacityHover ? `transition:opacity 0.95s ease-out;
-moz-transition:opacity 0.95s ease-out; /* Firefox 4 */
-webkit-transition:opacity 0.95s ease-out; /* Safari and Chrome */
-o-transition:opacity 0.95s ease-out; /* Opera */`: ``}
moz-transition:opacity 0.95s ease-out; /* Firefox 4 */
-webkit-transition:opacity 0.95s ease-out; /* Safari and Chrome */
-o-transition:opacity 0.95s ease-out; /* Opera */`: ``}
${config.position}: ${config.horizontalOffset}px;
z-index: 999;
pointer-events: none;
bottom: ${config.verticalOffset}px;
}
${config.opacityDefault != config.opacityHover ? `#${config.id}:hover{
opacity:${config.opacityHover};
${config.opacityDefault != config.opacityHover ? `#${config.id}:hover{
opacity:${config.opacityHover};
}`: ``}
</style>
<script src="/live2d/device.min.js"></script>
<script type="text/javascript">
(function(){
if(typeof(device) != "undefined"){
if(typeof(device) != "undefined"){
if(device.mobile()){
${config.mobileShow ? `document.getElementById("${config.id}").style.width = '${config.width * config.mobileScaling}px';
document.getElementById("${config.id}").style.height = '${config.height * config.mobileScaling}px';
Expand All @@ -83,10 +83,10 @@ hexo.extend.helper.register('live2d', function() {
document.write('<script type="text/javascript" src="/live2d/script.js"><\\/script>');
document.write('<script>loadlive2d(${JSON.stringify(config.id)}, ${JSON.stringify(url.resolve("/live2d/assets/", config.model + ".model.json"))}, 0.5)<\\/script>');
}
}else{
}else{
document.write('<script type="text/javascript" src="/live2d/script.js"><\\/script>');
document.write('<script>loadlive2d(${JSON.stringify(config.id)}, ${JSON.stringify(url.resolve("/live2d/assets/", config.model + ".model.json"))}, 0.5)<\\/script>');
}
}
})();
</script>
`
Expand Down Expand Up @@ -118,4 +118,3 @@ fs.exists(path.resolve(__dirname, './dist/device.min.js'), function(exists){
hexo.extend.generator.register('live2d', function (locals) {
return generators;
});

34 changes: 29 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,38 @@ function modelTurnHead(event)
let vy = transformViewY(target.y - rect.top);

if (LAppDefine.DEBUG_MOUSE_LOG)
console.log("onMouseDown device( x:" + event.clientX + " y:" + event.clientY + " ) view( x:" + vx + " y:" + vy + ")");
console.log("onMouseMove device( x:" + event.clientX + " y:" + event.clientY + " ) view( x:" + vx + " y:" + vy + ")");

lastMouseX = sx;
lastMouseY = sy;

dragMgr.setPoint(vx, vy);
dragMgr.setPoint(vx, vy);
}

function modelTapEvent(event)
{
drag = true;

let rect = canvas.getBoundingClientRect();

let sx = transformScreenX(event.clientX - rect.left);
let sy = transformScreenY(event.clientY - rect.top);
let target = transformRect({
x: rect.left + rect.width / 2,
y: rect.top + rect.height * head_pos
}, {
x: event.clientX,
y: event.clientY
}, rect)
let vx = transformViewX(target.x - rect.left);
let vy = transformViewY(target.y - rect.top);

if (LAppDefine.DEBUG_MOUSE_LOG)
console.log("onMouseDown device( x:" + event.clientX + " y:" + event.clientY + " ) view( x:" + vx + " y:" + vy + ")");

lastMouseX = sx;
lastMouseY = sy;

live2DMgr.tapEvent(vx, vy);
}

Expand Down Expand Up @@ -358,8 +383,7 @@ function mouseEvent(e)
// if (e.wheelDelta > 0) modelScaling(1.1);
// else modelScaling(0.9);
} else if (e.type == "mousedown") {
if("button" in e && e.button != 0) return;
// modelTurnHead(e);
modelTapEvent(e);
} else if (e.type == "mousemove") {
modelTurnHead(e);
} else if (e.type == "mouseup") {
Expand All @@ -374,7 +398,7 @@ function touchEvent(e)
{
var touch = e.touches[0];
if (e.type == "touchstart") {
if (e.touches.length == 1) modelTurnHead(touch);
if (e.touches.length == 1) modelTapEvent(touch);
// onClick(touch);
} else if (e.type == "touchmove") {
followPointer(touch);
Expand Down

0 comments on commit cdea4ba

Please sign in to comment.