衝突で消えていく複数の円|HTML5

衝突で消えていく複数の円|HTML5

衝突で消えていく複数の円(HTML5, JavaScript, CSS3) : デモ

衝突で消えていく複数の円(HTML5, JavaScript, CSS3) : ZIPファイル(4kb)

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="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,
   	theCanvas,
   	buttonDown,
    buttonMove,
    buttonUp,
    num = 10,
    circleArr = [],
   	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', ''));
    
    //ボタンタイプ
    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(buttonDown, buttonDownHandler, false);
    
   	ctx = document.getElementById('canvas').getContext("2d");
    
    draw_circles();
	animation();
    
    function animation()
	{
		ctx.clearRect(0, 0, 300, 300);
		for(var i = 0; i < circleArr.length; i++)
    	{
    		var this_circle = circleArr[i];
    		this_circle.update_me();
    		this_circle.draw_me();
    	}
		requestAnimationFrame(animation);
	}
    
	function buttonDownHandler(e)
    {
    	draw_circles();
    }
    
    function draw_circles()
    {
    	for(var i = 0; i < num; i++)
    	{
    		circleArr[i] = circle();
    	}
    }
    
    function circle()
    {
    	var x = 0,
    	y = 0,
    	x_move = 0,
    	y_move = 0,
    	radius = 0,
    	line_rgba,
    	fill_rgba,
    	alpha = 0;
    	
    	x = Math.random() * offsetX;
    	y = Math.random() * offsetY;
    	x_move = Math.random() * 10 - 5;
    	y_move = Math.random() * 10 - 5;
    	radius = Math.random() * 30 + 10;
    	alpha = Math.random();
    	line_rgba = Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255);
    	fill_rgba = Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255);

    	var draw_me = function()
    	{
    		ctx.beginPath();
	        ctx.arc(x, y, radius, 0, Math.PI * 2, true);
	        ctx.fillStyle = "rgba(" + fill_rgba + "," + alpha + ")";
	        ctx.fill();
	        
	        /*ctx.strokeStyle ="rgba(" + line_rgba + "," + alpha + ")";
    		ctx.lineWidth = 1;
	        ctx.beginPath();
	        ctx.arc(x, y, 5, 0, Math.PI * 2, true);
	        ctx.stroke();*/
    	}
    	
    	var update_me = function()
    	{
    		x += x_move;
    		y += y_move;
    		if(x > offsetX + radius)
    		{
    			x = 0 - radius;
    		}
    		else
    		if(x < 0 - radius)
    		{
    			x = offsetX + radius;
    		}
    		
    		if(y > offsetY + radius)
    		{
    			y = 0 - radius;
    		}
    		else
    		if(y < 0 - radius)
    		{
    			y = offsetY + radius;
    		}
    		
    		//衝突判定
    		var touching = false,
    		length = circleArr.length
    		for(var i = 0; i < length; i++)
    		{
    			var other_cicle = circleArr[i];
    			
    			if(other_cicle != this)
    			{
    				var dx = x - other_cicle.x;
    				var dy = y - other_cicle.y;
    				var dis = Math.sqrt(dx * dx + dy * dy);
    				if(dis - radius - other_cicle.radius < 0)
    				{
    					touching = true;
    					break;
    				}
    			}
    		}
    		
    		if(touching === true)
    		{
    			alpha -= 0.01;
    			if(alpha < 0)
    			{
    				alpha = 0;
    			}
    		}
    		else
    		{
    			alpha += 0.01;
    			if(alpha > 1)
    			{
    				alpha = 1;
    			}
    		}
    	}
    	
    	return{
    		update_me:update_me,
    		draw_me:draw_me,
    		x:x,
    		y:y,
    		radius:radius
    	}
    }
}

// 各ブラウザ対応
window.requestAnimationFrame = (function()
{
	return window.requestAnimationFrame ||
	window.webkitRequestAnimationFrame ||
	window.mozRequestAnimationFrame ||
	window.oRequestAnimationFrame ||
	window.msRequestAnimationFrame ||
	function(callback)
	{
		window.setTimeout(callback, 1000/60);
	};
}());

コメントを残す

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