捏与CSS3放大(Pinch to zoom with CSS3)

2019-06-24 22:15发布

我想实现双指缩放手势正是因为在谷歌地图。 我看着斯蒂芬·伍兹谈话- “创建响应HTML5触摸界面” -关于这个问题,并使用所提到的技术,我们的想法是设定在(0,0)和规模在该点的转换目标元素的起源变换,然后将图像转换为保持其中心位于转换点。

在我的测试代码缩放工作正常。 图像放大和缩小精细后续转换之间。 问题是我没有正确计算转换值。 我使用jQuery和Hammer.js触摸事件。 我如何调整我在变换回调计算,使图像保持在变换的点为中心?

所述的CoffeeScript( #test-resize是一个div与背景图像)

image = $('#test-resize')

hammer = image.hammer ->
  prevent_default: true
  scale_treshold: 0

width = image.width()
height = image.height()
toX = 0
toY = 0
translateX = 0
translateY = 0
prevScale = 1
scale = 1

hammer.bind 'transformstart', (event) ->

  toX = (event.touches[0].x + event.touches[0].x) / 2
  toY = (event.touches[1].y + event.touches[1].y) / 2

hammer.bind 'transform', (event) ->

  scale = prevScale * event.scale
  shiftX = toX * ((image.width() * scale) - width) / (image.width() * scale)
  shiftY = toY * ((image.height() * scale) - height) / (image.height() * scale)
  width = image.width() * scale
  height = image.height() * scale

  translateX -= shiftX
  translateY -= shiftY

  css = 'translateX(' + @translateX + 'px) translateY(' + @translateY + 'px) scale(' + scale + ')'
  image.css '-webkit-transform', css
  image.css '-webkit-transform-origin', '0 0'

hammer.bind 'transformend', () ->
  prevScale = scale

Answer 1:

我设法得到它的工作。

的jsfiddle演示

在演示的jsfiddle,点击图像上表示在点击点为中心的捏合手势。 随后点击由恒定量增加的比例因子。 为了使这个有用的,你想做出规模,上的转换事件更经常转换计算(hammer.js提供一个)。

它让工作的关键是要正确计算规模的点坐标相对于图像。 我用event.clientX/Y得到屏幕坐标。 下面的几行从屏幕转换为图像坐标:

x -= offset.left + newX
y -= offset.top + newY

然后,我们计算了图像的新的大小和发现的距离由翻译。 翻译方程式摘自斯蒂芬·伍兹的谈话 。

newWidth = image.width() * scale
newHeight = image.height() * scale

newX += -x * (newWidth - image.width) / newWidth
newY += -y * (newHeight - image.height) / newHeight

最后,我们扩展和翻译

image.css '-webkit-transform', "scale3d(#{scale}, #{scale}, 1)"         
wrap.css '-webkit-transform', "translate3d(#{newX}px, #{newY}px, 0)"

我们做我们的包装元素上所有的翻译,以确保翻译产停留在我们的图像的左上角。



Answer 2:

我成功地使用了片段上的PhoneGap调整图像,使用锤子和jQuery。

如果有人感兴趣,我翻译这JS。

function attachPinch(wrapperID,imgID)
{
    var image = $(imgID);
    var wrap = $(wrapperID);

    var  width = image.width();
    var  height = image.height();
    var  newX = 0;
    var  newY = 0;
    var  offset = wrap.offset();

    $(imgID).hammer().on("pinch", function(event) {
        var photo = $(this);

        newWidth = photo.width() * event.gesture.scale;
        newHeight = photo.height() * event.gesture.scale;

        // Convert from screen to image coordinates
        var x;
        var y;
        x -= offset.left + newX;
        y -= offset.top + newY;

        newX += -x * (newWidth - width) / newWidth;
        newY += -y * (newHeight - height) / newHeight;

        photo.css('-webkit-transform', "scale3d("+event.gesture.scale+", "+event.gesture.scale+", 1)");      
        wrap.css('-webkit-transform', "translate3d("+newX+"px, "+newY+"px, 0)");

        width = newWidth;
        height = newHeight;
    });

}


Answer 3:

我看着所有在互联网上,不管,直到我遇到的唯一的工作插件/库来outernet - http://cubiq.org/iscroll-4

var myScroll;
myScroll = new iScroll('wrapper');

其中包装是你的ID在ID =“包装”

<div id="wrapper">
    <img src="smth.jpg" />
</div>


Answer 4:

这是我写了几年前在Java和最近转换为JavaScript

function View()
{
 this.pos = {x:0,y:0};
 this.Z = 0;
 this.zoom = 1;
 this.scale = 1.1;
 this.Zoom = function(delta,x,y)
 {
  var X = x-this.pos.x;
  var Y = y-this.pos.y;
  var scale = this.scale;
  if(delta>0) this.Z++;
  else
  {
   this.Z--;
   scale = 1/scale;
  }
  this.zoom = Math.pow(this.scale, this.Z);
  this.pos.x+=X-scale*X;
  this.pos.y+=Y-scale*Y;
 }
}

所述this.Zoom = function(delta,x,y)发生:

  • delta =放大或缩小
  • x = X缩放原点位置
  • y =缩放原点y位置

一个小例子:

<script>
var view = new View();
var DivStyle = {x:-123,y:-423,w:300,h:200};
function OnMouseWheel(event)
{
 event.preventDefault();
 view.Zoom(event.wheelDelta,event.clientX,event.clientY);
 div.style.left = (DivStyle.x*view.zoom+view.pos.x)+"px";
 div.style.top = (DivStyle.y*view.zoom+view.pos.y)+"px";
 div.style.width = (DivStyle.w*view.zoom)+"px";
 div.style.height = (DivStyle.h*view.zoom)+"px";
}
function OnMouseMove(event)
{
 view.pos = {x:event.clientX,y:event.clientY};
 div.style.left = (DivStyle.x*view.zoom+view.pos.x)+"px";
 div.style.top = (DivStyle.y*view.zoom+view.pos.y)+"px";
 div.style.width = (DivStyle.w*view.zoom)+"px";
 div.style.height = (DivStyle.h*view.zoom)+"px";
}
</script>
<body onmousewheel="OnMouseWheel(event)" onmousemove="OnMouseMove(event)">
<div id="div" style="position:absolute;left:-123px;top:-423px;width:300px;height:200px;border:1px solid;"></div>
</body>

这是用帆布和图形所用的意图提出,但它应该很好地工作在正常的HTML布局



Answer 5:

没有一个真正的答案,但有一个插头的链接=在做这一切为您服务。 做得好!

https://github.com/timmywil/jquery.panzoom

(感谢“Timmywil”,你是谁,曾经)



文章来源: Pinch to zoom with CSS3