首页 技术 正文
技术 2022年11月7日
0 收藏 843 点赞 336 浏览 2682 个字
JQuery插件让图片旋转任意角度且代码极其简单
2012-04-01 09:57:03     我来说两句     
收藏    JQuery插件让图片旋转任意角度且代码极其简单 – 摘自网友我要投稿

引入下方的jquery.rotate.js文件,然后通过$(“选择器”).rotate(角度);可以旋转任意角度,

例如$(“#rotate-image”).rotate(45);把这句放在$(document).ready(function(){ });中

就是将id为rotate-image的图片旋转45度。

不过,貌似在Chrome中总是不显示。

唉,找了两个小时,才发现Chrome太坑爹了,没法获取图片的长宽。

解决办法是,把$(“#rotate-image”).rotate(45);放在

$(window).load(function(){ });中,因为在Chrome中图片在执行$(document).ready(function(){ });中的语句时并没有加载完图片,坑爹啊。

另外可以更方便的通过调用$(“选择器”).rotateRight()和$(“选择器”).rotateLeft()来分别向右旋转90度和向左旋转90度。

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); 
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919