プログラミングとイラストレーション » Canvas要素 イージング|Canvas Element Easing|HTML5
プログラミングとイラストレーション > HTML5 > Canvas要素 イージング|Canvas Element Easing|HTML5

Canvas要素 イージング|Canvas Element Easing|HTML5

Canvas要素 イージング|Canvas Element Easing|HTML5

Canvas要素 イージング|Canvas Element Easing|HTML5(HTML5, JavaScript, CSS3) : デモ

Canvas要素 イージング|Canvas Element Easing|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(6kb)

画面をタッチするとCanvas要素がイージングします。

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>Canvas Easing</title>
        <link href="css/base.css" rel="stylesheet" type="text/css">
        <script src="js/devicecheck.js"></script>
        <script src="js/base.js"></script>
    </head>
    <body>
        <div id="contents">
            <canvas id="canvas" width="275" height="550"></canvas>
        </div>
    </body>
</html>

2.CSS

@charset "utf-8";
   
body
{
    margin: 0;
    padding: 0;
    background-color: #fff;
}
   
#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: 275px;
    height: 550px;
}

3.JavaScript

base.js

window.addEventListener("load", init, false);
   
function init()
{
    var offsetX = 0,
    offsetY = 0,
    ctx,
    mouseX = 0,
    mouseY = 0,
    buttonDown,
    buttonMove,
    buttonUp,
    targetY = 0,
    easing = 0.1,
    canvasTop = 0,
    canvas,
    motion,
    theContents;
      
    theContents = document.getElementById("contents");
    offsetX = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).width;
    offsetX = Number(offsetX.replace('px', ''));
   
    offsetY = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).height;
    offsetY = Number(offsetY.replace('px', ''));
     
    ctx = document.getElementById('canvas').getContext("2d");
      
    //ボタンタイプ
    if(g_mobiledevice === true)
    {
        buttonDown = 'touchstart';
        buttonMove = 'touchmove';
        buttonUp = 'touchend';
    }
    else
    {
        buttonDown = 'mousedown';
        buttonMove = 'mousemove';
        buttonUp = 'mouseup';
    }
      
    document.getElementById('canvas').addEventListener('touchstart', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchmove', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchend', function() 
    {
        event.preventDefault();
    });
     
    canvas = document.getElementById('canvas');
    canvas.addEventListener(buttonDown, buttonDownHandler, false);
     
    ctx.fillStyle = "#000000";
    ctx.fillRect(0, 275, 275, 550);
     
    darwVectorArrowDown();
    darwVectorArrowUp();
     
    function animationEasing()
    {
        animation();
        motion = requestAnimationFrame(animationEasing);
    }
     
    function animation()
    {
        var top = (canvas.currentStyle || document.defaultView.getComputedStyle(canvas,'')).top;
        top = Number(top.replace('px', ''));
        if(Math.abs(targetY - top) < 0.1)
        {
            cancelAnimationFrame(motion);//アニメーションの破棄
            motion;
            return;
        }
         
        var vy = (targetY - top) * easing;
        canvasTop += vy;
        canvas.style.top = canvasTop + "px";
    }
     
    function buttonDownHandler(e)
    {
         
        cancelAnimationFrame(motion);//アニメーションの破棄
        motion;
        animationEasing();//アニメーションの実行
        mouseXY(e);
        if(mouseX > 0 && mouseX < 275 && mouseY > 0 && mouseY < 275)
        {
            targetY = -275;
            canvasTop = 0;
        }
        else
        if(mouseX > 0 && mouseX < 275 && mouseY > 275 && mouseY < 550)
        {
            targetY = 0;
            canvasTop = -275;
        }
    }
     
    function mouseXY(e)
    {
        if(g_mobiledevice)
        {
            var rect = event.currentTarget.getBoundingClientRect();
            mouseX = event.touches[0].pageX - rect.left;
            mouseY = event.touches[0].pageY - rect.top;
        }
        else
        if(g_browsername == 'MSIE'||g_browsername == 'Opera')
        {
            rect = document.getElementById("canvas").getBoundingClientRect();
            mouseX = event.clientX - rect.left;
            mouseY = event.clientY - rect.top;
        }
        else
        if(g_browsername == 'Firefox')
        {
            rect = document.getElementById("canvas").getBoundingClientRect();
            mouseX = e.clientX - rect.left;
            mouseY = e.clientY - rect.top;
        }
        else
        {
            rect = event.currentTarget.getBoundingClientRect();
            mouseX = event.clientX - rect.left;
            mouseY = event.clientY - rect.top;
        }
    }
     
    function darwVectorArrowDown()
    {
        // selectedItems/
        ctx.beginPath();
        ctx.moveTo(187.5, 147.5);
        ctx.lineTo(137.5, 197.5);
        ctx.lineTo(87.5, 147.5);
        ctx.lineTo(87.5, 117.5);
        ctx.lineTo(127.5, 157.5);
        ctx.lineTo(127.5, 77.5);
        ctx.lineTo(147.5, 77.5);
        ctx.lineTo(147.5, 157.5);
        ctx.lineTo(187.5, 117.5);
        ctx.lineTo(187.5, 147.5);
        ctx.closePath();
        ctx.fillStyle = "rgb(0, 0, 0)";
        ctx.fill();
         
        // selectedItems/
        ctx.beginPath();
        ctx.moveTo(1.0, 0.0);
        ctx.lineTo(1.0, 1.0);
        ctx.lineTo(0.0, 1.0);
        ctx.lineTo(0.0, 0.0);
        ctx.lineTo(1.0, 0.0);
        ctx.closePath();
        ctx.fill();
    }
     
    function darwVectorArrowUp()
    {
        // selectedItems/
        ctx.beginPath();
        ctx.moveTo(187.5, 431.5);
        ctx.lineTo(147.5, 391.5);
        ctx.lineTo(147.5, 471.5);
        ctx.lineTo(127.5, 471.5);
        ctx.lineTo(127.5, 391.5);
        ctx.lineTo(87.5, 431.5);
        ctx.lineTo(87.5, 401.5);
        ctx.lineTo(137.5, 351.5);
        ctx.lineTo(187.5, 401.5);
        ctx.lineTo(187.5, 431.5);
        ctx.closePath();
        ctx.fillStyle = "rgb(255, 255, 255)";
        ctx.fill();
         
        // selectedItems/
        ctx.beginPath();
        ctx.moveTo(1.0, 0.0);
        ctx.lineTo(1.0, 1.0);
        ctx.lineTo(0.0, 1.0);
        ctx.lineTo(0.0, 0.0);
        ctx.lineTo(1.0, 0.0);
        ctx.closePath();
        ctx.fill();
    }
}
  
// 各ブラウザ対応
(function(){
    var requestAnimationFrame = window.requestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.oRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            function(callback){
                setTimeout(callback, 1000 / 60);
            };
 
    var cancelAnimationFrame = window.cancelAnimationFrame ||
            window.webkitCancelRequestAnimationFrame ||
            window.mozCancelRequestAnimationFrame ||
            window.oCancelRequestAnimationFrame ||
            window.msCancelRequestAnimationFrame ||
            function(id){
                clearTimeout(id);
            };
 
    window.requestAnimationFrame = requestAnimationFrame;
    window.cancelAnimationFrame = cancelAnimationFrame;
})();

devicecheck.js

var g_mobiledevice = false; //iPhone、iPad、Androidのときtrue
var g_browsername = 'unknown';
var g_browserver = -1;
 
if(checkUserAgent() == false)
{
    window.alert('このブラウザは対象外です');
}
 
function checkUserAgent()
{
    var ua = navigator.userAgent;
 
    //iPhoneか?  
    var mstr = ua.match(/iPhone OS \d+/);
    if(mstr != null){
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 3) 
        {
            g_mobiledevice = true;
            g_browsername = 'iPhone';
            //alert('major-version ' + vstr);
            return true;
        }
    }
    //iPadか?
    if(ua.indexOf('iPad') > -1)
    {
        mstr = ua.match(/CPU OS \d+/);
        if(mstr != null)
        {
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 3) {
                g_mobiledevice = true;
                g_browsername = 'iPad';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }   
    //Androidか? 
    var mstr = ua.match(/Android \d+\.\d+/);
    if(mstr != null)
    {
        g_browsername = 'Android';
        var vstr = mstr[0].match(/\d+\.\d+/);
        g_browserver = parseFloat(vstr[0]);
        g_mobiledevice = true;
        if(pg_browserver > 2.1) 
        {
            //alert('version ' + vstr);
            return true;
        }
    }
    //Chromeか?
    mstr = ua.match(/Chrome\/\d+/);
    if(mstr != null)
    {
        g_browsername = 'Chrome';
        var vstr = mstr[0].match(/\d+/);
        g_browserver = parseInt(vstr[0]);
        if(g_browserver >= 9) 
        {
            //alert('major-version ' + vstr);
             
            return true;
        }       
    }
    //Safariか?
    if(ua.indexOf('Safari') > -1)
    {
        mstr = ua.match(/Version\/\d+/);
        if(mstr != null){
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 5) 
            {
                g_browsername = 'Safari';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }
    //Internet Explorerか?
    mstr = ua.match(/MSIE \d+/);
    if(mstr != null)
    {
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 9) 
        {
            g_browsername = 'MSIE';
            //alert('major-version ' + vstr);
            return true;
        }       
    }
    //Firefoxか?
    mstr = ua.match(/Firefox\/\d+/);
    if(mstr != null){
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 4) 
        {
            g_browsername = 'Firefox';
            //alert('major-version ' + vstr);
            return true;
        }       
    }
    //Operaか?
    if(ua.indexOf('Opera') > -1)
    {
        mstr = ua.match(/Version\/\d+/);
        if(mstr != null){
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 11) 
            {
                g_browsername = 'Opera';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }
     
    return false;
}

コメントを残す

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

コメントフィード

トラックバック URL : http://www.htmlcode.jp/canvas%e8%a6%81%e7%b4%a0-%e3%82%a4%e3%83%bc%e3%82%b8%e3%83%b3%e3%82%b0%ef%bd%9ccanvas-element-easing%ef%bd%9chtml5/trackback/