プログラミングとイラストレーション » 放射状-フェードイン|HTML5
プログラミングとイラストレーション > HTML5 > 放射状-フェードイン|HTML5

放射状-フェードイン|HTML5

放射状-フェードイン|HTML5

放射状-フェードイン|HTML5(HTML5, JavaScript, CSS3) : デモ

放射状-フェードイン|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(16kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>放射状 フェードイン|Radiation Fade in</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()
    {
    	var a = 0;
    	ctx.clearRect(0, 0, 300, 300);
    	ctx.lineWidth = 1;
	    for(var ang = 0; ang <= 180; ang += 1.5)
	    {
	    	ctx.beginPath();
	    	var r = 0;
	    	var g = 0;
	    	var b = 0;
	    	//var a = Math.random() * 2;
	    	a += 1 / 180;
	    	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();
	    }
    }
    
    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;
})();

コメントを残す

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

コメントフィード

トラックバック URL : http://www.htmlcode.jp/%e6%94%be%e5%b0%84%e7%8a%b6-%e3%83%95%e3%82%a7%e3%83%bc%e3%83%88%e3%82%99%e3%82%a4%e3%83%b3%ef%bd%9chtml5/trackback/