螺旋状の球体|HTML5

螺旋状の球体|HTML5

螺旋状の球体|HTML5(HTML5, JavaScript, CSS3) : デモ

螺旋状の球体|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(5kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>3d 螺旋状の球|3d Sphere by Spiral</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="275"></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: 275px;
}

3.JavaScript

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;
}

base.js

window.addEventListener("load", init, false);
  
function init()
{
    var offsetX = 0,
    offsetY = 0,
    ctx,
    points = [],
    numPoints = 0,
    fl = 250,
    vpX = 0,
    vpY = 0,
    mouseX = 0,
    mouseY = 0,
    buttonDown,
    buttonMove,
    buttonUp,
    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', ''));
     
    vpX = offsetX / 2;
    vpY = offsetY / 2;
    
    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();
    });
     
    document.getElementById('canvas').addEventListener(buttonMove, mouseMoveHandler, false);
    
    var s = 0,
    t = 0,
    last_x = 0,
    last_y = 0,
    last_z = 0,
    radius = 100,
    i = 0;
    
    while(t < 180)
    {
    	s += 9;
    	t += 0.5;
    	var radian_s = s * Math.PI / 180;
    	var radian_t = t * Math.PI / 180;
    	
    	var this_x = 0 + (radius * Math.cos(radian_s) * Math.sin(radian_t));
    	var this_y = 0 + (radius * Math.sin(radian_s) * Math.sin(radian_t));
    	var this_z = 0 + (radius * Math.cos(radian_t));
    	point3D(this_x, this_y, this_z, vpX, vpY, 0, 0, 0, i);
    	i++;
    }
    
    //x,y,z,vpX,vpY,cX,cY,cZ,index
    function point3D(x, y, z, vpX, vpY, cX, cY, cZ, i)
    {
    	var p = {};
        p.x = x;
		p.y = y;
		p.z = z;
		p.vpX = vpX;
		p.vpY = vpY;
		p.cX = cX;
		p.cY = cY;
		p.cZ = cZ;
		p.angleX = 0;
		p.angleY = 0;
		p.angleZ = 0;
		p.screenX = 0;
		p.screenY = 0;
		points[i] = p;
    }
    
    numPoints = points.length;
    setAnimation();
     
    function setAnimation()
    {
        animation();
        requestAnimationFrame(setAnimation);
    }
     
    function animation()
    {
        var angleX = (mouseY - vpY) * 0.001;
        var angleY = (mouseX - vpX) * 0.001;

        for(var i = 0; i < numPoints; i++)
        {
        	var point = points[i];
			setMovePoints(point, angleX, angleY);
        }
        
        ctx.clearRect(0, 0, 300, 300);
    	ctx.strokeStyle = "#000000";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.moveTo(points[0].screenX, points[0].screenY);
		for(i = 1; i < numPoints; i++)
		{
			ctx.lineTo(points[i].screenX, points[i].screenY);
		}
		//ctx.lineTo(points[0].screenX, points[0].screenY);
		ctx.stroke();
    }
    
    function setMovePoints(point, angleX, angleY)
    {
    	point.angleX = angleX;
		point.angleY = angleY;
		
		var scale = fl / (fl + point.z + point.cZ);
		point.screenX = point.vpX + (point.cX + point.x) * scale;
		
		scale = fl / (fl + point.z + point.cZ);
		point.screenY = point.vpY + (point.cY + point.y) * scale;
		
		
		var cosX = Math.cos(point.angleX);
		var sinX = Math.sin(point.angleX);
		
		var y1 = point.y * cosX - point.z * sinX;
		var z1 = point.z * cosX + point.y * sinX;
		
		point.y = y1;
		point.z = z1;
		
		
		var cosY = Math.cos(point.angleY);
		var sinY = Math.sin(point.angleY);
		
		var x1 = point.x * cosY - point.z * sinY;
		var z1 = point.z * cosY + point.x * sinY;
		
		point.x = x1;
		point.z = z1;
		
		/*
		var cosZ = Math.cos(point.angleZ);
		var sinZ = Math.sin(point.angleZ);
		
		var x1 = point.x * cosZ - point.y * sinZ;
		var y1 = point.y * cosZ + point.x * sinZ;
		
		point.x = x1;
		point.y = y1;
		*/
    }
    
    function mouseMoveHandler(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;
        }
    }
}
 
// 各ブラウザ対応
window.requestAnimationFrame = (function()
{
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function(callback)
    {
        window.setTimeout(callback, 1000/60);
    };
}());

コメントを残す

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