如何用JavaScript实现移动端双指缩放和旋转功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计722个文字,预计阅读时间需要3分钟。
实现移动端双指缩放和旋转的功能,以下是一个简化后的代码示例:
javascriptvar initHeading=0;var rotation=0;var lastTime;
function setGesture(el) { var obj={}; var isTouch=false; var start=[];
el.addEventListener('touchstart', function(e) { if (e.touches.length==2) { isTouch=true; start=[e.touches[0].clientX, e.touches[0].clientY, e.touches[1].clientX, e.touches[1].clientY]; lastTime=new Date().getTime(); } });
el.addEventListener('touchmove', function(e) { if (isTouch && e.touches.length==2) { var now=new Date().getTime(); var deltaTime=now - lastTime; lastTime=now;
var dx=e.touches[0].clientX - start[0]; var dy=e.touches[0].clientY - start[1]; var distance=Math.sqrt(dx * dx + dy * dy);
var cx=(start[0] + start[2]) / 2; var cy=(start[1] + start[3]) / 2; var angle=Math.atan2(e.touches[0].clientY - cy, e.touches[0].clientX - cx) * 180 / Math.PI;
var angleDiff=angle - initHeading; rotation +=angleDiff;
obj.scale=distance / 100; // 假设初始距离为100px obj.rotation=rotation;
// 更新视图 el.style.transform='scale(' + obj.scale + ') rotate(' + obj.rotation + 'deg)'; } });
el.addEventListener('touchend', function(e) { isTouch=false; });}
js实现移动端双指缩放和旋转,具体代码如下所示:
var initHeading = 0; var rotation = 0; var lastTime; function setGesture(el){ var obj={}; var istouch=false; var start=[]; el.addEventListener("touchstart",function(e){ if(e.touches.length>=2){ //判断是否有两个点在屏幕上 istouch=true; start=e.touches; //得到第一组两个点 obj.gesturestart&&obj.gesturestart.call(el); //执行gesturestart方法 }; },false); document.addEventListener("touchmove",function(e){ e.preventDefault(); if(e.touches.length>=2&&istouch){ var now=e.touches; //得到第二组两个点 var scale=getDistance(now[0],now[1])/getDistance(start[0],start[1]); //得到缩放比例,getDistance是勾股定理的一个方法 var rotation=getAngle(now[0],now[1])-getAngle(start[0],start[1]); //得到旋转角度,getAngle是得到夹角的一个方法 e.scale=scale.toFixed(2); e.rotation=rotation.toFixed(2); obj.gesturemove&&obj.gesturemove.call(el,e); //执行gesturemove方法 }; },false); document.addEventListener("touchend",function(e){ if(istouch){ istouch=false; obj.gestureend&&obj.gestureend.call(el); //执行gestureend方法 }; },false); return obj; }; function getDistance(p1, p2) { var x = p2.pageX - p1.pageX, y = p2.pageY - p1.pageY; return Math.sqrt((x * x) + (y * y)); }; function getAngle(p1, p2) { var x = p1.pageX - p2.pageX, y = p1.pageY- p2.pageY; return Math.atan2(y, x) * 180 / Math.PI; }; var box=document.querySelector("#map3d"); var boxGesture=setGesture(box); //得到一个对象 boxGesture.gesturestart=function(){ //双指开始 /*box.style.backgroundColor="yellow";*/ initHeading = map25D._coreMap.map.position.heading; }; boxGesture.gesturemove=function(e){ //双指移动 rotation = parseInt(e.rotation); var time = new Date().getTime(); var realRotation = changeAngle(rotation,time); if(realRotation){ //TODO 得到旋转角度后想实现的功能 map25D._coreMap.map.position.setHeading(realRotation); map25D._coreMap.map.renderer.update(); } }; boxGesture.gestureend=function(){ //双指结束 }; //通过时间判断解决叠加初始方向 var changeAngle = function (heading,newTime) { if((newTime - lastTime) < 2){ return false; } lastTime = newTime; return (initHeading + heading) }
ps:下面看下js缩放网页比例
将当前网页缩放比变为200%
document.getElementsByTagName('body')[0].style.zoom=2
总结
以上所述是小编给大家介绍的JS实现移动端双指缩放和旋转方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对易盾网络网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
本文共计722个文字,预计阅读时间需要3分钟。
实现移动端双指缩放和旋转的功能,以下是一个简化后的代码示例:
javascriptvar initHeading=0;var rotation=0;var lastTime;
function setGesture(el) { var obj={}; var isTouch=false; var start=[];
el.addEventListener('touchstart', function(e) { if (e.touches.length==2) { isTouch=true; start=[e.touches[0].clientX, e.touches[0].clientY, e.touches[1].clientX, e.touches[1].clientY]; lastTime=new Date().getTime(); } });
el.addEventListener('touchmove', function(e) { if (isTouch && e.touches.length==2) { var now=new Date().getTime(); var deltaTime=now - lastTime; lastTime=now;
var dx=e.touches[0].clientX - start[0]; var dy=e.touches[0].clientY - start[1]; var distance=Math.sqrt(dx * dx + dy * dy);
var cx=(start[0] + start[2]) / 2; var cy=(start[1] + start[3]) / 2; var angle=Math.atan2(e.touches[0].clientY - cy, e.touches[0].clientX - cx) * 180 / Math.PI;
var angleDiff=angle - initHeading; rotation +=angleDiff;
obj.scale=distance / 100; // 假设初始距离为100px obj.rotation=rotation;
// 更新视图 el.style.transform='scale(' + obj.scale + ') rotate(' + obj.rotation + 'deg)'; } });
el.addEventListener('touchend', function(e) { isTouch=false; });}
js实现移动端双指缩放和旋转,具体代码如下所示:
var initHeading = 0; var rotation = 0; var lastTime; function setGesture(el){ var obj={}; var istouch=false; var start=[]; el.addEventListener("touchstart",function(e){ if(e.touches.length>=2){ //判断是否有两个点在屏幕上 istouch=true; start=e.touches; //得到第一组两个点 obj.gesturestart&&obj.gesturestart.call(el); //执行gesturestart方法 }; },false); document.addEventListener("touchmove",function(e){ e.preventDefault(); if(e.touches.length>=2&&istouch){ var now=e.touches; //得到第二组两个点 var scale=getDistance(now[0],now[1])/getDistance(start[0],start[1]); //得到缩放比例,getDistance是勾股定理的一个方法 var rotation=getAngle(now[0],now[1])-getAngle(start[0],start[1]); //得到旋转角度,getAngle是得到夹角的一个方法 e.scale=scale.toFixed(2); e.rotation=rotation.toFixed(2); obj.gesturemove&&obj.gesturemove.call(el,e); //执行gesturemove方法 }; },false); document.addEventListener("touchend",function(e){ if(istouch){ istouch=false; obj.gestureend&&obj.gestureend.call(el); //执行gestureend方法 }; },false); return obj; }; function getDistance(p1, p2) { var x = p2.pageX - p1.pageX, y = p2.pageY - p1.pageY; return Math.sqrt((x * x) + (y * y)); }; function getAngle(p1, p2) { var x = p1.pageX - p2.pageX, y = p1.pageY- p2.pageY; return Math.atan2(y, x) * 180 / Math.PI; }; var box=document.querySelector("#map3d"); var boxGesture=setGesture(box); //得到一个对象 boxGesture.gesturestart=function(){ //双指开始 /*box.style.backgroundColor="yellow";*/ initHeading = map25D._coreMap.map.position.heading; }; boxGesture.gesturemove=function(e){ //双指移动 rotation = parseInt(e.rotation); var time = new Date().getTime(); var realRotation = changeAngle(rotation,time); if(realRotation){ //TODO 得到旋转角度后想实现的功能 map25D._coreMap.map.position.setHeading(realRotation); map25D._coreMap.map.renderer.update(); } }; boxGesture.gestureend=function(){ //双指结束 }; //通过时间判断解决叠加初始方向 var changeAngle = function (heading,newTime) { if((newTime - lastTime) < 2){ return false; } lastTime = newTime; return (initHeading + heading) }
ps:下面看下js缩放网页比例
将当前网页缩放比变为200%
document.getElementsByTagName('body')[0].style.zoom=2
总结
以上所述是小编给大家介绍的JS实现移动端双指缩放和旋转方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对易盾网络网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

