Ver Mensaje Individual
  #276 (permalink)  
Antiguo 07/09/2008, 16:02
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 19 años, 10 meses
Puntos: 834
Respuesta: FAQs JavaScript

P: Se puede rotar una imagen usando javascript?
R: Sí, y más allá del camino chapucero que propuse en este post, la inclusión de soporte para el elemento html estandar llamado canvas en la mayoría de los navegadores modernos, junto a filtros propietarios del único navegador popular que no soporta dicho elemento, nos permiten tranquilamente realizar esta tarea. Ejemplo. Código del ejemplo:
Código PHP:
<!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=iso-8859-1" />
<
title>test</title>
<
script>
function 
rotar(obj,angulo){
    if (
angulo >= 0) {
        var 
rotation Math.PI angulo 180;
    } else {
        var 
rotation Math.PI * (360+angulo) / 180;
    }
    var 
costheta Math.cos(rotation);
    var 
sintheta Math.sin(rotation);
    if (
document.createElement("canvas").getContext) {
    
/* ---- canvas ---- */ 
        
var c=document.createElement('canvas');
        
c.width Math.abs(costheta*obj.width) + Math.abs(sintheta*obj.height);
        
c.style.width c.width+'px';
        
c.height Math.abs(costheta*obj.height) + Math.abs(sintheta*obj.width);
        
c.style.height=c.height+'px';
        
c.id=obj.id;
        
c.style.position='absolute';
        var 
ctx=c.getContext('2d');
        
ctx.save();
        if (
rotation <= Math.PI/2) {
            
ctx.translate(sintheta*obj.height,0);
        } else if (
rotation <= Math.PI) {
            
ctx.translate(c.width,-costheta*obj.height);
        } else if (
rotation <= 1.5*Math.PI) {
            
ctx.translate(-costheta*obj.width,c.height);
        } else {
            
ctx.translate(0,-sintheta*obj.width);
        }
        
ctx.rotate(rotation);
        
ctx.drawImage(obj00obj.widthobj.height);
        
obj.parentNode.replaceChild(c,obj);
        
ctx.restore();
    }else{
    
/* ---- DXImageTransform ---- */
        
obj.style.position='absolute';
        
obj.style.filter="progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand')";
        
obj.filters.item(0).M11 costheta;
        
obj.filters.item(0).M12 = -sintheta;
        
obj.filters.item(0).M21 sintheta;
        
obj.filters.item(0).M22 costheta;
    }
}
window.onload=function(){
    
rotar(document.getElementById('pp'),60);
    
}
</script>
</head>

<body>
<img src="1.jpg" width="180" height="127" />
<div id="ll" style="position:relative; "><img id="pp" src="1.jpg" width="180" height="127" /></div>
</body>
</html> 
(Ejemplo probado en Explorer, Safari, Ópera, Firefox y Chrome)

Última edición por Panino5001; 11/09/2008 a las 03:13 Razón: optimización