js实现双指缩放图片 手机端双指缩放图片

首先引入js文件,需要jq,pinchzoom.js。pinchzoom.js需要在jq环境下使用,可以

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes" />

<script type="text/javascript" src="../dependencies/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="../src/pinchzoom.js"></script>

script调用插件为:

<script type="text/javascript">
        $(function () {
            $('div.pinch-zoom').each(function () {
                new RTP.PinchZoom($(this), {});
            });
        })
    </script>

body结构:

 

 <div class="page">
        <div class="pinch-zoom">
            <div class="description">
                <h1>Pinchzoom.js</h1>
                <p>
                    Multi-touch zoom in Javascript
                </p>
            </div>
            <img src="frog.jpg"/>
            <a class="image" href="http://www.flickr.com/photos/59446027@N00/5258583198" target="_blank">by Peter Nijenhuis on Flickr; Some rights reserved</a>
        </div>
    </div>

 

看调用插件的函数就知道

 $('div.pinch-zoom')下的每一个图片元素都可以进行双指缩放,双击放大。

 

  

转载于:https://www.cnblogs.com/well-nice/p/4891119.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在小程序的 canvas 中实现双指缩放图片的功能,你可以按照以下步骤进行操作: 1. 创建一个 canvas 组件,并设置好宽度和高度。 ``` <canvas id="myCanvas" style="width: 300px; height: 300px;"></canvas> ``` 2. 在小程序的 js 文件中获取到 canvas 组件的上下文对象。 ``` const ctx = wx.createCanvasContext('myCanvas'); ``` 3. 加载并绘制图片到 canvas 上。 ``` const imgPath = '图片路径'; // 替换为你的图片路径 wx.getImageInfo({ src: imgPath, success(res) { const imgWidth = res.width; // 图片原始宽度 const imgHeight = res.height; // 图片原始高度 // 计算图片缩放后的初始宽度和高度 const initialWidth = 300; const initialHeight = (imgHeight / imgWidth) * initialWidth; ctx.drawImage(imgPath, 0, 0, initialWidth, initialHeight); ctx.draw(); } }); ``` 4. 监听 canvas 组件的触摸事件,实现双指缩放功能。 ``` let touchStartDistance = 0; // 记录触摸开始时两指之间的距离 let scale = 1; // 记录当前缩放比例 wx.canvasTouchStart((e) => { if (e.touches.length >= 2) { const x1 = e.touches[0].x; const y1 = e.touches[0].y; const x2 = e.touches[1].x; const y2 = e.touches[1].y; // 计算两指之间的距离 touchStartDistance = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2); } }); wx.canvasTouchMove((e) => { if (e.touches.length >= 2) { const x1 = e.touches[0].x; const y1 = e.touches[0].y; const x2 = e.touches[1].x; const y2 = e.touches[1].y; // 计算两指之间的距离 const touchMoveDistance = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2); // 计算缩放比例 const newScale = touchMoveDistance / touchStartDistance; // 更新缩放比例 scale = newScale; // 清空 canvas ctx.clearRect(0, 0, 300, 300); // 根据缩放比例绘制图片 const newWidth = initialWidth * scale; const newHeight = initialHeight * scale; ctx.drawImage(imgPath, 0, 0, newWidth, newHeight); ctx.draw(); } }); ``` 请根据你的实际需求,将上述代码片段进行适当修改和组合。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值