http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 21721|回复: 0

[Jquery] Jquery.rotate实现图片自由旋转,左右90度,输入角度都可以

[复制链接]
发表于 2014-8-22 08:44:58 | 显示全部楼层 |阅读模式
Jquery.rotate实现图片自由旋转,左右90度,输入角度都可以


这个方法我相信在开发中是一个比较常用的方法,

用户上传的图片可能正的,但也可能是反的,还有可能是横的,我们想有好的体验就需要来旋转的看,如果是下载下来,那图片播放器会自带这样的功能,但是网页不会哦。

所以咱们一起来实现一下。
先看效果
1.jpg
QQ截图20140822084536.jpg
QQ截图20140822084528.jpg
QQ截图20140822084603.jpg

方法很简单,大家直接看Html代码
[HTML] 纯文本查看 复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>美图WEB开放平台</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="../Script/jquery-1.10.2.js" type="text/javascript"></script>
    <script src="../Script/rotate.js" type="text/javascript"></script>
    <script type="text/javascript">
        function Left() {
            $("#imgtest").rotateLeft()
        }
        function Right() {
            $("#imgtest").rotateRight()
        }
        function Rotate() {
            $("#imgtest").rotate($("#txtnumber").val());
        }
    </script>
    <style type="text/css">
        html, body { height: 100%; overflow: hidden; }
        body { margin: 0; }
    </style>
</head>
<body>
输入要旋转的角度:<input id="txtnumber" type="text" value="45" />

<button type="button">Rotate</button>
<button type="button">left</button>
<button type="button">right</button>
    <div id="">
        <img id="imgtest" src="http://www.sufeinet.com/template/veikei_dz_life_20130810_plus/images/logo.png?2014-06-06" />
    </div>
</body>
</html>


Jquery我就不提供了这个大家自己下载吧。
我提供一下rotate.js
如下
[JavaScript] 纯文本查看 复制代码
jQuery.fn.rotate = function (angle, whence) {
    var p = this.get(0);

    // we store the angle inside the image tag for persistence  
    if (!whence) {
        p.angle = ((p.angle == undefined ? 0 : p.angle) + angle) % 360;
    } else {
        p.angle = angle;
    }

    if (p.angle >= 0) {
        var rotation = Math.PI * p.angle / 180;
    } else {
        var rotation = Math.PI * (360 + p.angle) / 180;
    }
    var costheta = Math.round(Math.cos(rotation) * 1000) / 1000;
    var sintheta = Math.round(Math.sin(rotation) * 1000) / 1000;
    //alert(costheta+","+sintheta);  

    if (document.all && !window.opera) {
        var canvas = document.createElement('img');

        canvas.src = p.src;
        canvas.height = p.height;
        canvas.width = p.width;

        canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11=" + costheta + ",M12=" + (-sintheta) + ",M21=" + sintheta + ",M22=" + costheta + ",SizingMethod='auto expand')";
    } else {
        var canvas = document.createElement('canvas');
        if (!p.oImage) {
            canvas.oImage = new Image();
            canvas.oImage.src = p.src;
        } else {
            canvas.oImage = p.oImage;
        }

        canvas.style.width = canvas.width = Math.abs(costheta * canvas.oImage.width) + Math.abs(sintheta * canvas.oImage.height);
        canvas.style.height = canvas.height = Math.abs(costheta * canvas.oImage.height) + Math.abs(sintheta * canvas.oImage.width);

        var context = canvas.getContext('2d');
        context.save();
        if (rotation <= Math.PI / 2) {
            context.translate(sintheta * canvas.oImage.height, 0);
        } else if (rotation <= Math.PI) {
            context.translate(canvas.width, -costheta * canvas.oImage.height);
        } else if (rotation <= 1.5 * Math.PI) {
            context.translate(-costheta * canvas.oImage.width, canvas.height);
        } else {
            context.translate(0, -sintheta * canvas.oImage.width);
        }
        context.rotate(rotation);
        context.drawImage(canvas.oImage, 0, 0, canvas.oImage.width, canvas.oImage.height);
        context.restore();
    }
    canvas.id = p.id;
    canvas.angle = p.angle;
    p.parentNode.replaceChild(canvas, p);
}

jQuery.fn.rotateRight = function (angle) {
    this.rotate(angle == undefined ? 90 : angle);
}

jQuery.fn.rotateLeft = function (angle) {
    this.rotate(angle == undefined ? -90 : -angle);
} 

本帖被以下淘专辑推荐:



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-8 03:25

© 2014-2021

快速回复 返回顶部 返回列表