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

旋转效果:


包括两部分内容,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 οnclick="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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值