书籍名称:HTML5-Animation-with-JavaScript
书籍源码:https://github.com/lamberta/html5-animation
1.使用quadraticCurveTo,
- 开始点:moveTo(20,20)
- 控制点:quadraticCurveTo(20,100,200,20)的前两个参数,
- 结束点:quadraticCurveTo(20,100,200,20)的后两个参数
图示如下:
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.多重曲线
时间: 2024-10-10 00:18:48