C曲線|HTML5

C曲線|HTML5

C曲線(HTML5, JavaScript, CSS3) : デモ

C曲線(HTML5, JavaScript, CSS3) : ZIPファイル(4kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
	<head>
		<meta charset="UTF-8" />
		<title>C Curve</title>
		<link href="css/base.css" rel="stylesheet" type="text/css">
		<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:25px;
	color:#fff;
}

#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);

function init()
{
	var theCanvas = document.getElementById("canvas");
	var centerX = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).width;
	centerX = Number(centerX.replace('px',''));
	centerX =  centerX / 3;
	
	var centerY = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).height;
	centerY = Number(centerY.replace('px',''));
	centerY =  centerY / 2.7;
	var ctx = theCanvas.getContext("2d");
	
	var X = 0;
	var Y = 0;
	var nowX = 0;
	var nowY = 0;
	var maxLength = 4;
	
	maxLength = 4;
	c_curve(100, 0);
	
	function directionChange(degree)
	{
		
		nowD = nowD + degree;
	}
	
	function relativeDraw(X,Y)
	{
		X = nowX + X;
		Y = nowY + Y;
		
		ctx.strokeStyle="#000000";
		ctx.lineWidth=1;
		ctx.beginPath();
        ctx.moveTo(centerX + nowX, centerY + nowY);
        ctx.lineTo(centerX + X, centerY + Y);
       ctx.stroke();
        ctx.fill();
	    //ctx.stroke();
	    ctx.closePath();
		
		nowX = X;
		nowY = Y;
	}
	
	function c_curve(X, Y)
	{
		if (X * X + Y * Y <= maxLength * maxLength)
		{
			relativeDraw(X, Y);
		}
		else
		{
			c_curve((X - Y) / 2, (Y + X) / 2);
			c_curve((X + Y) / 2, (Y - X) / 2);
		}
	}
}

コメントを残す

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