CSS3で回転アニメーション|HTML5

CSS3で回転アニメーション|HTML5

CSS3で回転アニメーション|HTML5(HTML5, JavaScript, CSS3) : デモ

CSS3で回転アニメーション|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(2kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>css3で回転アニメーション</title>
        <link href="css/base.css" rel="stylesheet" type="text/css">
        <script src="js/base.js"></script>
    </head>
    <body>
        <div id="contents">
            <canvas id="canvas" width="133" height="133">
            </canvas>
        </div>
    </body>
</html>

2.CSS

@charset "utf-8";
  
body
{
    margin:0;
    padding:0;
    background-color: #fff;
    font-family:Helvetica, HiraKakuProN-W3, sans-serif; 
    font-size:12px;
    color:#000;
}
  
#contents
{
    position: absolute;
    top: 0;
    left: 0;
    width: 275px;
    height:275px;
    border: 1px solid #000;
    overflow:hidden;
}
  
#canvas
{
    position: absolute;
    top: 0;
    left: 0;
    width: 133px;
    height:133px;
    -webkit-animation-duration:2s;
    -webkit-animation-iteration-count:infinite;
    -webkit-animation-timing-function:linear;
    -webkit-animation-name:rectRotate;
     
    -moz-animation-duration:2s;
    -moz-animation-iteration-count:infinite;
    -moz-animation-timing-function:linear;
    -moz-animation-name:rectRotate;
     
    -o-animation-duration:2s;
    -o-animation-iteration-count:infinite;
    -o-animation-timing-function:linear;
    -o-animation-name:rectRotate;
     
    -ms-animation-duration:2s;
    -ms-animation-iteration-count:infinite;
    -ms-animation-timing-function:linear;
    -ms-animation-name:rectRotate;
}
 
@-webkit-keyframes rectRotate
{
    0%{-webkit-transform:rotate(0deg);}
    99%,100%{-webkit-transform:rotate(360deg);}
}
 
@-moz-keyframes rectRotate
{
    0%{-moz-transform:rotate(0deg);}
    99%,100%{-moz-transform:rotate(360deg);}
}
 
@-o-keyframes rectRotate
{
    0%{-o-transform:rotate(0deg);}
    99%,100%{-o-transform:rotate(360deg);}
}
 
@-ms-keyframes rectRotate
{
     0%{-ms-transform:rotate(0deg);}
    99%,100%{-ms-transform:rotate(360deg);}
}

3.JavaScript

base.js

window.addEventListener("load", init, false);
  
function init()
{
    var theCanvas = document.getElementById("contents");
    var offsetX = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).width;
    offsetX = Number(offsetX.replace('px',''));
    offsetX =  offsetX / 2;
    var offsetY = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).height;
    offsetY = Number(offsetY.replace('px',''));
    offsetY =  offsetY / 2;
     
    theCanvas = document.getElementById("canvas");
    var ctx = theCanvas.getContext("2d");
      
    setDraw();
      
    function setDraw()
    {
        ctx.fillStyle="#000000";
        ctx.beginPath();
        ctx.strokeStyle="#000000";
        ctx.lineWidth=1;
        ctx.moveTo(0,0);
        ctx.lineTo(133,0);
        ctx.lineTo(133,133);
        ctx.lineTo(0,133);
         ctx.lineTo(0,0);
        ctx.fill();
        ctx.stroke();
        theCanvas.style.top=133/2+'px';
        theCanvas.style.left=133/2+'px';
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です