关闭

2013.12.05 js实现图片的旋转 兼容IE

506人阅读 评论(0) 收藏 举报
分类:

旋转效果:


包括两部分内容,1.展示图片的文件showIm.jsp 2.控制图片旋转的js文件rotateImage.js

showImg.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/jsp/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title>${title }</title>
<script src="${ctx }/assets/dwz/js/jquery-1.7.1.js" type="text/javascript"></script>
<script src="${ctx }/resource/js/rotateImage.js"></script>
</head>
<body>
<div onclick="rotateRight('image',90);" id="div"> 
   <img id="image" border="0" src="${ctx }/<%=request.getParameter("action") %>/getImgByte?id=<%=request.getParameter("id") %>"/>
</div>
<script>
var width = $('#image').width();
var height = $('#image').height();
$('#div').width(width)
$('#div').height(height)
</script>
</body>
</html>


rotateImage.js

/**
 * 图片旋转
 * @param id img的id值
 * @param angle 旋转的角度(90,-90...)
 * @param whence
 */
function rotate(id,angle,whence) {
var p = document.getElementById(id);// 取得IMG对象

// 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.cos(rotation);
var sintheta = Math.sin(rotation);

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);
}
/**
 * 右旋
 * @param id
 * @param angle
 */
function rotateRight(id,angle) {
rotate(id,angle==undefined?90:angle);
}
/**
 * 左旋
 * @param id
 * @param angle
 */
function rotateLeft(id,angle) {
rotate(id,angle==undefined?-90:-angle);
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:74563次
    • 积分:1928
    • 等级:
    • 排名:千里之外
    • 原创:96篇
    • 转载:5篇
    • 译文:28篇
    • 评论:5条
    最新评论