js写真の回転を実現する3つの方法

7092 ワード

1 jQueryRotate.jsを使用して例示コードを実現します。
 
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#div1 {
width: 800px;
height: 600px;
background-color: #ff0;
position: absolute;
}
.imgRotate {
width: 100px;
height: 80px;
position: absolute;
top: 50%;
left: 50%;
margin: -40px 0 0 -50px;
}
</style>
</head>
<body>
<div id="div1">
<img id="img1" class="imgRotate" src="http://www.baidu.com/img/logo-yy.gif" />
<input id="input2" type="button" value="btn2"></input>
</div>
</body>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jQueryRotate.js"></script>
<script type="text/javascript">
var num = 0;
$("#input2").click(function(){
num ++;
$("#img1").rotate(90*num);
});
</script>
</html>
テスト結果:chromeの効果は正常で、回転後も対象はimgです。ie 8の効果は正常ですが、回転した後に、対象が次のようになります。対象が変化しますので、回転しても元の方法で対象を取得すると、jsエラーが発生します。イメージオブジェクトを取得したいなら、クラスによって取得できます。画像が回転した後、他の操作が行われない場合には、この方法があります。他の操作を行うと、画像を拡大、縮小するなど、この方法がより複雑になります。
 
<span ...>
<rvml:group class="rvml"...>
<rvml:image class="rvml".../>
</rvml:group>
</span>
2 Microsoftが提供するMatrixオブジェクトの例コードを使用して、
 
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#div1 {
width: 800px;
height: 600px;
background-color: #ff0;
position: absolute;
}
.imgRotate {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;
}
#imgRotate {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;
}
</style>
</head>
<body>
<div id="div1">
<img id="img1" class="imgRotate" src="http://www.baidu.com/img/logo-yy.gif" />
<input id="input1" type="button" value="btn1"></input>
</div>
</body>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function rotate(id,angle,whence) {
var p = document.getElementById(id);

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

function rotateRight(id,angle) {
rotate(id,angle==undefined?90:angle);
}

function rotateLeft(id,angle) {
rotate(id,angle==undefined?-90:-angle);
}
$("#input1").click(function(){
$("img.imgRotate").attr("id","imgRotate");
rotateLeft("imgRotate",90);
$("#imgRotate").attr("top","50%");
$("#imgRotate").attr("left","50%");
$("#imgRotate").attr("margin","-50px 0 0 -50px");
});
</script>
</html>
のテスト結果:chromeでは効果は正常ですが、回転した後に、対象はcanvasオブジェクトになります。ie 8の効果は正常で、回転した後も対象は依然としてimg対象です。Matrix()はパラメータが多く、使用する場合は多くの計算が必要です。3 Microsoftを使用して提供されるBaicImageオブジェクトの例コード:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<img id="image" src="http://www.baidu.com/img/logo-yy.gif" />
<input id="input2" type="button" value="btn2"></input>
</body>
<script type="text/javascript" src="jquery.min.js"></script>

<script type="text/javascript">
var num = 0;
$("#input2").click(function(){
num = (num + 1) % 4;
document.getElementById('image').style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+num+')';
});
</script>
</html>
テスト結果:chromeでは回転できません。ie 8の効果は正常で、回転した後も対象は依然としてimg対象です。BaicImage()は一つのパラメータだけです。これらの3つの方法のコードを見ると、本質的には解決策であることが分かります。chromeでcanvasオブジェクトを使用して実現され、ie 8でVMLまたはMatrix()またはBaicImage()を使用して実現されます。本人は最近1つのコンポーネントを改造します。その中で写真を回転して拡大することに関連しています。jQueryRotate.jsはie 8の下で新しいオブジェクトを生成するので、拡大する前に写真を選ぶ時は特殊な処理が必要です。後にchrome、ie 8に対して別れて処理することを決定して、chromeの下でjQueryRotateを使って実現して、ie 8の下でBaicImageを使って実現して、コードの簡潔性と可読性を保証しました。