プログラミングとイラストレーション » マウスを追いかけるオブジェクト|HTML5
プログラミングとイラストレーション > HTML5 > マウスを追いかけるオブジェクト|HTML5

マウスを追いかけるオブジェクト|HTML5

マウスを追いかけるオブジェクト|HTML5

マウスを追いかけるオブジェクト|HTML5(HTML5, JavaScript, CSS3) : デモ

マウスを追いかけるオブジェクト|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(4kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>マウスを追いかける|Follow Mouse</title>
        <link href="css/base.css" rel="stylesheet" type="text/css">
        <script src="js/base.js"></script>
         <script src="js/devicecheck.js"></script>
    </head>
    <body>
        <div id="contents">
            <canvas id="canvas" width="300" height="300">
            </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: 300px;
    height:300px;
    border: 1px solid #000;
    overflow:hidden;
}
 
#canvas
{
    position: absolute;
    top: 0;
    left: 0;
    width: 300px;
    height:300px;
}

3.JavaScript

base.js

window.addEventListener("load", init, false);
 
var centerX=0;
var centerY=0;
var theCanvas;
var ctx;
var currentX0=0;
var currentY0=0;
var radianOffset=0;
var currentX1=0;
var currentY1=0;
var currentX2=0;
var currentY2=0;
var points = new Array();
var speed = 0;
 
var offsetX0 = 0;
var offsetY0 = 0;
var offsetX1 = 0;
var offsetY1 = 0;
var offsetX2 = 0;
var offsetY2 = 0;
 
var _animation = null;
 
function init()
{
    document.getElementById('canvas').addEventListener('touchstart', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchmove', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchend', function() 
    {
        event.preventDefault();
    });
     
    //ボタンタイプ
    if(g_mobiledevice==true)
    {
        buttonStart = 'touchstart';
        buttonMove = 'touchmove';
        buttonEnd = 'touchend';
    }
    else
    {
        buttonStart = 'mousedown';
        buttonMove = 'mousemove';
        buttonEnd = 'mouseup';
    }
     
    theCanvas = document.getElementById("contents");
    centerX = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).width;
    centerX = Number(centerX.replace('px',''));
    centerX =  centerX / 2;
    centerY = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).height;
    centerY = Number(centerY.replace('px',''));
    centerY =  centerY / 2;;
     
    theCanvas = document.getElementById("canvas");
     
    //theCanvas.addEventListener(buttonStart, mouseStartHandler, false);
    theCanvas.addEventListener(buttonMove, mouseMoveHandler, false);
    //theCanvas.addEventListener(buttonEnd, mouseEndHandler, false);
     
    ctx =  theCanvas.getContext("2d");
 
    var x = Math.cos(18*Math.PI/180)*25*1.62;
    var y = Math.sin(18*Math.PI/180)*25*1.62;
     
    var dx=0-(-(x/2));
    var dy=0-(-y);
    var rad1 = Math.atan2(dy,dx);
         
    points.push({x:-(x/2),y:-y,radian:rad1});
    points.push({x:(x/2),y:0,radian:0});
    points.push({x:-(x/2),y:y,radian:0});
     
    offsetX0 = -(x/2);
    offsetY0 = -y;
    offsetX1 = (x/2);
    offsetY1 = 0;
    offsetX2 = -(x/2);
    offsetY2 = y;
    mouseX = 0;
    mouseY = 0;
    speed=0;
     
    setMove();  
}
 
function mouseStartHandler()
{
}
 
function mouseMoveHandler(e) 
{
    speed = 0.5;
     
    if(g_mobiledevice)
    { 
        var rect = event.currentTarget.getBoundingClientRect();
        mouseX = event.touches[0].pageX - rect.left-centerX;
        mouseY = event.touches[0].pageY - rect.top-centerY;
    }
else
if(g_browsername == 'MSIE' || g_browsername == 'Opera')
{
    rect = document.getElementById("canvas").getBoundingClientRect();
    mouseX = event.clientX - rect.left - centerX;
    mouseY = event.clientY - rect.top - centerY;
}
else
if(g_browsername == 'Firefox')
{
    rect = document.getElementById("canvas").getBoundingClientRect();
    mouseX = e.clientX - rect.left - centerX;
    mouseY = e.clientY - rect.top - centerY;
}
    else
    {
        rect = event.currentTarget.getBoundingClientRect();
        mouseX =  event.clientX - rect.left-centerX;
        mouseY =  event.clientY - rect.top-centerY;
    }
}
 
function mouseEndHandler()
{
}
 
function setMove()
{
    function animation()
    {
        ctx.clearRect(0,0,300,300);
        if(centerX<-20)
        {
            mouseX*=-1;
        }
        else
        if(centerX>320)
        {
            mouseX*=-1;
        }
         
        if(centerY<-20)
        {
            mouseY*=-1;
        }
        else
        if(centerY>320)
        {
            mouseY*=-1;
        }
        var dx = mouseX;
        var dy = mouseY;
        var radian = Math.atan2(dy,dx);
        radian -= radianOffset;
         
        currentX0 = Math.cos(radian)*points[0].x-Math.sin(radian)*points[0].y;
        currentY0 = Math.sin(radian)*points[0].x+Math.cos(radian)*points[0].y;
        radianOffset = radian;
         
        currentX1 = Math.cos(radian)*points[1].x-Math.sin(radian)*points[1].y;
        currentY1 = Math.sin(radian)*points[1].x+Math.cos(radian)*points[1].y;
         
        currentX2 = Math.cos(radian)*points[2].x-Math.sin(radian)*points[2].y;
        currentY2 = Math.sin(radian)*points[2].x+Math.cos(radian)*points[2].y;
         
        points[0].x = currentX0;
        points[0].y = currentY0;
        points[0].radian = radianOffset;
        points[1].x = currentX1;
        points[1].y = currentY1;
        points[2].x = currentX2;
        points[2].y = currentY2;
     
        var dx=mouseX;
        var dy=mouseY;
        var radian = Math.atan2(dy,dx);
        var vx = Math.cos(radian)*speed;
        var vy = Math.sin(radian)*speed;
        centerX+= vx;
        centerY+= vy;
        radianOffset = radian;
         
         
         
         
        draw(points[0].x,points[0].y,points[1].x,points[1].y,points[2].x,points[2].y);
         
    }
    _animation = setInterval(animation,1000/60);
}
 
function draw(currentX0,currentY0,currentX1,currentY1,currentX2,currentY2)
{
    ctx.strokeStyle="#000000";
    ctx.lineWidth=1;
    ctx.beginPath();
    ctx.moveTo(centerX+currentX0, centerY+currentY0);
    ctx.lineTo(centerX+currentX1, centerY+currentY1);
    ctx.lineTo(centerX+currentX2, centerY+currentY2);
    ctx.lineTo(centerX+currentX0, centerY+currentY0);
    ctx.stroke();
}

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/%e3%83%9e%e3%82%a6%e3%82%b9%e3%82%92%e8%bf%bd%e3%81%84%e3%81%8b%e3%81%91%e3%82%8b%e3%82%aa%e3%83%96%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%ef%bd%9chtml5/trackback/