ペジェ曲線を操作する|HTML5

ペジェ曲線を操作する|HTML5

ペジェ曲線を操作する|HTML5(HTML5, JavaScript, CSS3) : デモ

ペジェ曲線を操作する|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(8kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>ペジェ曲線を操作する</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="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 mouseX = 0;
var mouseY = 0;
var offsetX = 0;
var offsetY = 0;
var ctx = null;
 
var buttonDown = null;
var buttonMove = null;
var buttonUp = null;
var theCanvas = null;
 
var ball1 = new Object();
ball1.f=null;
ball1.x = 0;
ball1.y = 0;
ball1.r = 10;
ball1.d = 0;
 
var ball2 = new Object();
ball2.f=null;
ball2.x = 0;
ball2.y = 0;
ball2.r = 10;
ball2.d = 0;
 
var ball3 = new Object();
ball3.f=null;
ball3.x = 0;
ball3.y = 0;
ball3.r = 10;
ball3.d = 0;
 
var ball4 = new Object();
ball4.f=null;
ball4.x = 0;
ball4.y = 0;
ball4.r = 10;
ball4.d = 0;
 
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();
    });
     
    var theContents = document.getElementById("contents");
    offsetX = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).width;
    offsetX = Number(offsetX.replace('px',''));
    offsetX =  offsetX / 2;
    offsetY = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).height;
    offsetY = Number(offsetY.replace('px',''));
    offsetY =  offsetY / 2;
     
    //ボタンタイプ
    if(g_mobiledevice==true)
    {
        buttonDown = 'touchstart';
        buttonMove = 'touchmove';
        buttonUp = 'touchend';
    }
    else
    {
        buttonDown = 'mousedown';
        buttonMove = 'mousemove';
        buttonUp = 'mouseup';
    }
     
    ball1.x = 15;
    ball1.y = 150;
     
    ball2.x = 100;
    ball2.y = 20;
     
    ball3.x = 190;
    ball3.y = 280;
     
    ball4.x = 285;
    ball4.y = 150;
     
    document.getElementById('canvas').addEventListener(buttonDown, mouseDownHandler, false);
     
    document.getElementById('canvas').addEventListener(buttonUp, mouseUpHandler, false);
     
    document.getElementById('canvas').addEventListener(buttonDown, mouseDownHandler, false);
     
    document.getElementById('canvas').addEventListener(buttonUp, mouseUpHandler, false);
     
    document.getElementById('canvas').addEventListener(buttonDown, mouseDownHandler, false);
     
    document.getElementById('canvas').addEventListener(buttonUp, mouseUpHandler, false);
     
    document.getElementById('canvas').addEventListener(buttonDown, mouseDownHandler, false);
     
    document.getElementById('canvas').addEventListener(buttonUp, mouseUpHandler, false);
    
    setBallAnimation();
}
 
function setBallAnimation()
{
    ctx = document.getElementById('canvas').getContext("2d");
    setInterval(animation, 1000/60);
}
 
function animation()
{
    ctx.clearRect(0,0,300,300);
    ctx.strokeStyle = "#000000";
    ctx.lineWidth = 1;
    ctx.beginPath();
    ctx.arc(ball1.x, ball1.y, ball1.r,0,Math.PI*2,true);
    ctx.fill();
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(ball2.x, ball2.y, ball2.r,0,Math.PI*2,true);
    ctx.fill();
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(ball3.x, ball3.y, ball3.r,0,Math.PI*2,true);
    ctx.fill();
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(ball4.x, ball4.y, ball4.r,0,Math.PI*2,true);
    ctx.fill();
    ctx.stroke();
    bezier();
}
 
function setMouseXY(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 mouseUpHandler()
{
    ball1.f = false;
    ball2.f = false;
    ball3.f = false;
    ball4.f = false;
}
 
function mouseDownHandler(e)
{
    setMouseXY(e);
    var dx1 = mouseX - ball1.x;
    var dy1 = mouseY - ball1.y;
    ball1.d = Math.sqrt(dx1 * dx1 + dy1 * dy1);
     
    if(ball1.d <= ball1.r)
    {
        ball1.f = true;
    }
     
    var dx2 = mouseX - ball2.x;
    var dy2 = mouseY - ball2.y;
    ball2.d = Math.sqrt(dx2 * dx2 + dy2 * dy2);
     
    if(ball2.d <= ball2.r)
    {
        ball2.f = true;
    }
     
    var dx3 = mouseX - ball3.x;
    var dy3 = mouseY - ball3.y;
    ball3.d = Math.sqrt(dx3 * dx3 + dy3 * dy3);
     
    if(ball3.d <= ball3.r)
    {
        ball3.f = true;
    }
     
    var dx4 = mouseX - ball4.x;
    var dy4 = mouseY - ball4.y;
    ball4.d = Math.sqrt(dx4 * dx4 + dy4 * dy4);
     
    if(ball4.d <= ball4.r)
    {
        ball4.f = true;
    }
     
    document.getElementById('canvas').addEventListener(buttonMove, mouseMoveHandler, false);
}
 
function mouseMoveHandler(e)
{
    if(ball1.f)
    {
        setMouseXY(e);
        ball1.x = mouseX;
        ball1.y = mouseY;
    }
    else
    if(ball2.f)
    {
        setMouseXY(e);
        ball2.x = mouseX;
        ball2.y = mouseY;
    }
    else
    if(ball3.f)
    {
        setMouseXY(e);
        ball3.x = mouseX;
        ball3.y = mouseY;
    }
    else
    if(ball4.f)
    {
        setMouseXY(e);
        ball4.x = mouseX;
        ball4.y = mouseY;
    }
}
 
function bezier()
{
    ctx.strokeStyle = "#000000";
    ctx.lineWidth = 1;
    ctx.beginPath();
     
    var x1 = ball1.x;
    var y1 = ball1.y;
    var x2 = ball2.x;
    var y2 = ball2.y;
    var x3 = ball3.x;
    var y3 = ball3.y;
    var x4 = ball4.x;
    var y4 = ball4.y;
    var t = 0;
    var seg = 50;
     
    for(var i = 0; i <= seg; i++)
    {
        //Bezier Curves Equation (0<=t<=1)
        var x = (1-t)*(1-t)*(1-t)*x1+3*(1-t)*(1-t)*t*x2+3*(1-t)*t*t*x3+t*t*t*x4;
        var y = (1-t)*(1-t)*(1-t)*y1+3*(1-t)*(1-t)*t*y2+3*(1-t)*t*t*y3+t*t*t*y4;
        if(i==0)
        {
            ctx.moveTo(x, y);
        }
        else
        {
            ctx.lineTo(x, y);
        }
        t += 1 / seg;
    }
    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;
}

コメントを残す

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