书籍名称:HTML5-Animation-with-JavaScript
书籍源码:https://github.com/lamberta/html5-animation
1.使用quadraticCurveTo,
图示如下:
2.曲线经过的点
如果你想让曲线经过一个点,可以利用下面这个公式来计算。其中xt,yt是我们希望曲线经过的目标点,而x0,y0,x2,y2分别为两个端点。通过这个公式可以计算出控制点x1,y1
x1 = xt * 2 - (x0 + x2) / 2;
y1 = yt * 2 - (y0 + y2) / 2;
03-curve-through-point.html代码:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Curve Through Point</title> <link rel="stylesheet" href="../include/style.css"> </head> <body> <header> Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a> </header> <canvas id="canvas" width="400" height="400"></canvas> <aside>Move mouse on canvas element.</aside> <script src="../include/utils.js"></script> <script> window.onload = function () { var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'), mouse = utils.captureMouse(canvas), x0 = 100, y0 = 200, x2 = 300, y2 = 200; canvas.addEventListener('mousemove', function () { context.clearRect(0, 0, canvas.width, canvas.height); var x1 = mouse.x * 2 - (x0 + x2) / 2, y1 = mouse.y * 2 - (y0 + y2) / 2; //curve through mouse context.beginPath(); context.moveTo(x0, y0); context.quadraticCurveTo(x1, y1, x2, y2); context.stroke(); }, false); }; </script> </body> </html>
3.多重平滑曲线
一,作为首次尝试,先讲一个容易写的一个错的方法,从每个点依次画曲线。
根据图示可以看出,红色圈出的地方是尖角,而并非平滑曲线。
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Multi Curve 1</title> <link rel="stylesheet" href="../include/style.css"> </head> <body> <header> Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a> </header> <canvas id="canvas" width="400" height="400"></canvas> <aside>Refresh page for random curves.</aside> <script> window.onload = function () { var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'), points = [], numPoints = 9; //array of random point objects for (var i = 0; i < numPoints; i++) { points.push({ x: Math.random() * canvas.width, y: Math.random() * canvas.height }); } //move to the first point context.beginPath(); context.moveTo(points[0].x, points[0].y); //and loop through each successive pair for (i = 1; i < numPoints; i += 2) { context.quadraticCurveTo(points[i].x, points[i].y, points[i+1].x, points[i+1].y); } context.stroke(); }; </script> </body> </html>
二,平滑多重曲线
先来分析下图,白色的为控制点,黑色为中点,除首位两个中点不在曲线上,其余都在曲线上。鉴于此可以绘画平滑的曲线。
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Multi Curve 3</title> <link rel="stylesheet" href="../include/style.css"> </head> <body> <header> Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a> </header> <canvas id="canvas" width="400" height="400"></canvas> <aside>Refresh page for random curves.</aside> <script> window.onload = function () { var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'), points = [], numPoints = 9, ctrlPoint = {}, ctrlPoint1 = {}; for (var i = 0; i < numPoints; i++) { points.push({ x: Math.random() * canvas.width, y: Math.random() * canvas.height }); } //find the first midpoint and move to it ctrlPoint1.x = (points[0].x + points[numPoints-1].x) / 2; ctrlPoint1.y = (points[0].y + points[numPoints-1].y) / 2; context.beginPath(); context.moveTo(ctrlPoint1.x, ctrlPoint1.y); //curve through the rest, stopping at each midpoint for (i = 0; i < numPoints - 1; i++) { ctrlPoint.x = (points[i].x + points[i+1].x) / 2; ctrlPoint.y = (points[i].y + points[i+1].y) / 2; context.quadraticCurveTo(points[i].x, points[i].y, ctrlPoint.x, ctrlPoint.y); } //curve through the last point, back to the first midpoint context.quadraticCurveTo(points[i].x, points[i].y, ctrlPoint1.x, ctrlPoint1.y); context.stroke(); }; </script> </body> </html>
三,首尾相连的平滑曲线
和之间的方法一样,只是连端点也要作为两个控制点的中点。
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Multi Curve 3</title> <link rel="stylesheet" href="../include/style.css"> </head> <body> <header> Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a> </header> <canvas id="canvas" width="400" height="400"></canvas> <aside>Refresh page for random curves.</aside> <script> window.onload = function () { var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'), points = [], numPoints = 9, ctrlPoint = {}, ctrlPoint1 = {}; for (var i = 0; i < numPoints; i++) { points.push({ x: Math.random() * canvas.width, y: Math.random() * canvas.height }); } //find the first midpoint and move to it ctrlPoint1.x = (points[0].x + points[numPoints-1].x) / 2; ctrlPoint1.y = (points[0].y + points[numPoints-1].y) / 2; context.beginPath(); context.moveTo(ctrlPoint1.x, ctrlPoint1.y); //curve through the rest, stopping at each midpoint for (i = 0; i < numPoints - 1; i++) { ctrlPoint.x = (points[i].x + points[i+1].x) / 2; ctrlPoint.y = (points[i].y + points[i+1].y) / 2; context.quadraticCurveTo(points[i].x, points[i].y, ctrlPoint.x, ctrlPoint.y); } //curve through the last point, back to the first midpoint context.quadraticCurveTo(points[i].x, points[i].y, ctrlPoint1.x, ctrlPoint1.y); context.stroke(); }; </script> </body> </html>