作者:i_dovelemon
来源:CSDN
日期:2015 / 7 / 11
主题:Interpolate,Bezier Curve
<span style="font-family:Microsoft YaHei;"><html>
<script>
var _2dContext = 0;
/*Begin Solve Bezier Curve method*/
function LinearBezierCurve(p0,p1,t)
{
return p0 + (p1 - p0) * t;
}
function QuadBezierCurve(p0,p1,p2,t)
{
var current = 0;
var InvT = 1 - t;
var InvT_2 = InvT * InvT;
var T2 = t * t;
current = InvT_2 * p0;
current += 2 * InvT * t * p1;
current += T2* p2;
return current;
}
function CubicBezierCurve(p0,p1,p2,p3,t)
{
var current = 0;
var InvT = 1 - t;
var InvT_2 = InvT * InvT;
var InvT_3 = InvT_2 * InvT;
var T2 = t * t;
var T3 = T2 * t;
current += InvT_3 * p0;
current += 3 * InvT_2 *t * p1;
current += 3 * InvT * T2 * p2;
current += T3 * p3;
return current;
}
/*End Solve Bezier Curve*/
//Draw a point in the cavans
function DrawPoint(context,x,y,color)
{
context.beginPath();
context.moveTo(x,y);
context.lineTo(x + 1,y);
context.closePath();
context.strokeStyle = color;
context.stroke();
}
/*Begin Draw Bezier Curve*/
function DrawLinearBezierCurve(context,p0_x,p0_y,p1_x,p1_y,step,color)
{
var t = 0;
for(;t<=1;t+=step)
{
//Calculate the new position
var current_x = LinearBezierCurve(p0_x,p1_x,t);
var current_y = LinearBezierCurve(p0_y,p1_y,t);
//Draw the point
DrawPoint(context,current_x,current_y,color);
}
}
function DrawQuadBezierCurve(context, p0_x,p0_y,p1_x,p1_y,p2_x,p2_y,step,color)
{
var t = 0;
for(;t<=1;t+=step)
{
//Calculate the new position
var current_x = QuadBezierCurve(p0_x,p1_x,p2_x,t);
var current_y = QuadBezierCurve(p0_y,p1_y,p2_y,t);
//Draw the point
DrawPoint(context,current_x,current_y,color);
}
}
function DrawCubicBezierCurve(context,p0_x,p0_y,p1_x,p1_y,p2_x,p2_y,p3_x,p3_y,step,color)
{
var t = 0;
for(;t<=1;t+=step)
{
//Calculate the new position
var current_x = CubicBezierCurve(p0_x,p1_x,p2_x,p3_x,t);
var current_y = CubicBezierCurve(p0_y,p1_y,p2_y,p3_y,t);
//Draw the point
DrawPoint(context,current_x,current_y,color);
}
}
/*End Draw Bezier Curve*/
function gameInit()
{
//Get the canvas context
_2dContext = document.getElementById("canvas").getContext("2d");
//enable game loop
setInterval(gameLoop,100.0);
}
function gameLoop()
{
//Clear the canvas
_2dContext.clearRect(0,0,1024,768);
//Draw the linear bezier curve
DrawLinearBezierCurve(_2dContext,100,100,1000,100,0.001,"rgb(255,0,0)");
//Draw the Quadratic bezier curve
var qp1_x = Math.random() * 1024;
var qp1_y = Math.random() * 768;
DrawQuadBezierCurve(_2dContext,100,400,qp1_x,qp1_y,1000,400,0.001,"rgb(0,255,0)");
//Draw the Cubic bezier curve
var cp1_x = Math.random() * 1024;
var cp1_y = Math.random() * 768;
var cp2_x = Math.random() * 1024;
var cp2_y = Math.random() * 768;
DrawCubicBezierCurve(_2dContext,100,600,cp1_x,cp1_y,cp2_x,cp2_y,1000,600,0.001,"rgb(0,0,255)");
}
</script>
<body>
<body onLoad="gameInit();">
<canvas id="canvas" width="1024" height="768">
您的浏览器不支持Canvas特性!!!请使用Chrome,Firefox!!
</canvas>
</body>
</html></span>版权声明:本文为博主原创文章,未经博主允许不得转载。
插值技术之Bezier插值(1) -- Bezier Curve
原文:http://blog.csdn.net/i_dovelemon/article/details/46844851