多彩な放射線|HTML5

多彩な放射線|HTML5

多彩な放射線|HTML5(HTML5, JavaScript, CSS3) : デモ

多彩な放射線|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(15kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>多彩な放射線|Radiation Colors</title>
        <link href="css/base.css" rel="stylesheet" type="text/css">
        <script src="js/devicecheck.js"></script>
        <!--script src="js/Perlin.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;
}

#txt
{
	position: absolute;
    top: 5px;
    left: 5px;
    width: 120px;
    height:12px;
    font-size: 12px;
}

3.JavaScript

base.js

window.addEventListener("load", init, false);
 
function init()
{
    var offsetX = 0,
    offsetY = 0,
   	ctx,
   	theCanvas,
   	theContents,
    mouseX = 0,
    mouseY = 0,
    buttonDown,
    buttonMove,
    step = 15,
    radius = 130,
    angnoise = Math.random() * 10,
    radiusnoise = Math.random() * 10,
    xnoise = Math.random() * 10,
    ynoise = Math.random() * 10,
    angle = - Math.PI / 2,
    strokeCol = 254,
    strokeChange = -1,
    buttonUp;
    
    //ボタンタイプ
    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();
    });
     
    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',''));
    
    //document.getElementById('canvas').addEventListener(buttonMove, mouseMoveHandler, false);
    document.getElementById('canvas').addEventListener(buttonDown, mouseDownHandler, false);
     
    ctx = document.getElementById('canvas').getContext("2d");
    draw();
    function draw()
    {
    	ctx.clearRect(0, 0, 300, 300);
    	ctx.lineWidth = 1;
	    for(var ang = 0; ang <= 180; ang += 0.5)
	    {
	    	ctx.beginPath();
	    	var r = Math.floor(Math.random() * 256);
	    	var g = Math.floor(Math.random() * 256);
	    	var b = Math.floor(Math.random() * 256);
	    	//var a = Math.random() * 2;
	    	var a = 1;
	    	ctx.strokeStyle = "rgba(" + r + "," + g + "," + b + "," + a + ")";
	    	var rad = ang * Math.PI / 180;
	    	var x = offsetX / 2 + (radius * Math.cos(rad));
	    	var y = offsetY / 2 + (radius * Math.sin(rad));
	    	var opprad = rad + Math.PI;
	    	var x2 = offsetX / 2 + (radius * Math.cos(opprad));
	    	var y2 = offsetY / 2 + (radius * Math.sin(opprad));
	    	ctx.moveTo(x, y);
	    	ctx.lineTo(x2, y2);
	    	ctx.stroke();
	    }
    }
    
    
    //setBallAnimation();
    
    function setBallAnimation()
	{
		animation();
		requestAnimationFrame(setBallAnimation);
	}
     
    function animation()
    {
        ctx.clearRect(0, 0, 300, 300);
        
        ctx.strokeStyle = "#000000";
        ctx.lineWidth = 1;
        ctx.beginPath();
        for(var ang = 0; ang <= 360; ang += 5)
        {
        	var rad = ang * Math.PI / 180;
        	var x = offsetX / 2 + (radius * Math.cos(rad));
        	var y = offsetY / 2 + (radius * Math.sin(rad));
        	if(ang === 0)
        	{
        		ctx.moveTo(x, y);
        	}
        	else
        	{
        		ctx.lineTo(x, y);
        	}
        }
        ctx.stroke();
    }
    
    function mouseDownHandler()
    {
    	draw();
    }
    
    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;
	    }
	}
}

// 各ブラウザ対応
(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;
}

コメントを残す

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