前言:canvas动画入门系列之简单连线动画。虽然简单,但连线动画应用场景还挺多,因此做了个小demo,一通百通。
step1:绘制点
首先创建个标签<canvas id="canvas"></canvas>
设置几个点的坐标
const points = [ [200, 100], //上 [300, 200], //右 [100, 200], //左 [200, 100], //上 [200, 300], //下 [100, 200], //左 [300, 200], //右 [200, 300] ]; const canvas = document.querySelector("canvas"); const ctx = canvas.getContext("2d");
然后把点给画出来
points.forEach(([x, y]) => { drawDot(x, y); }); function drawDot(x1, y1, r) { ctx.save(); ctx.beginPath(); //不写会和线连起来 ctx.fillStyle = "red"; //绘制成矩形 ctx.arc(x1, y1, r ? r : 2, 0, 2 * Math.PI); ctx.fill(); ctx.restore(); }
step2:绘制线条
我们封装一个方法,传入起点终点,绘制一根线条
function drawLine(x1, y1, x2, y2) { ctx.save(); ctx.beginPath(); //不写每次都会重绘上次的线 ctx.lineCap = "round"; ctx.lineJoin = "round"; var grd = ctx.createLinearGradient(x1, y1, x2, y2); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.closePath(); ctx.strokeStyle = "rgba(255,255,255,1)"; ctx.stroke(); ctx.restore(); }
step3:线条动画
这里面需要计算两点之间的斜率,然后x坐标每次挪动±1单位,已知斜率和x偏移,即可计算出y的偏移。值得注意的是,这个坐标系和数学中的xy坐标系有点不一样,y轴是反的。然后可以引入额外的参数speed控制速度
function lineMove(points) { if (points.length < 2) { return; } const [[x1, y1], [x2, y2]] = points; let dx = x2 - x1; let dy = y2 - y1; if (Math.abs(dx) < 1 && Math.abs(dy) < 1) { points = points.slice(1); lineMove(points); return; } let x = x1, y = y1; //线条绘制过程中的终点 if (dx === 0) { (x = x2), (y += (speed * dy) / Math.abs(dy)); } else if (dy === 0) { x += (speed * dx) / Math.abs(dx); y = y2; } else if (Math.abs(dx) >= 1) { let rate = dy / dx; x += (speed * dx) / Math.abs(dx); y += (speed * rate * dx) / Math.abs(dx); } drawLine(x1, y1, x, y); points[0] = [x, y]; window.requestAnimationFrame(function() { lineMove(points); }); }
主要代码就这么多,先看效果
完整代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>canvas-连线动画</title> </head> <body> <canvas id="canvas" width="400" height="400"></canvas> <script> //起点:10,20 终点:150,200 const points = [ [200, 100], //上 [300, 200], //右 [100, 200], //左 [200, 100], //上 [200, 300], //下 [100, 200], //左 [300, 200], //右 [200, 300] ]; const canvas = document.querySelector("canvas"); const ctx = canvas.getContext("2d"); // const img = new Image(); const speed = 10; //速度 // img.onload = function() { // canvas.width = img.width; // canvas.height = img.height; animate(ctx); // }; // img.src = "./imgs/demo.png"; function animate(ctx) { // ctx.drawImage(img, 0, 0); ctx.fillRect(0, 0, canvas.width, canvas.height); points.forEach(([x, y]) => { drawDot(x, y); }); lineMove(points); } function lineMove(points) { if (points.length < 2) { return; } const [[x1, y1], [x2, y2]] = points; let dx = x2 - x1; let dy = y2 - y1; if (Math.abs(dx) < 1 && Math.abs(dy) < 1) { points = points.slice(1); lineMove(points); return; } let x = x1, y = y1; //线条绘制过程中的终点 if (dx === 0) { (x = x2), (y += (speed * dy) / Math.abs(dy)); } else if (dy === 0) { x += (speed * dx) / Math.abs(dx); y = y2; } else if (Math.abs(dx) >= 1) { let rate = dy / dx; x += (speed * dx) / Math.abs(dx); y += (speed * rate * dx) / Math.abs(dx); } drawLine(x1, y1, x, y); points[0] = [x, y]; window.requestAnimationFrame(function () { lineMove(points); }); } function drawLine(x1, y1, x2, y2) { ctx.save(); ctx.beginPath(); //不写每次都会重绘上次的线 ctx.lineCap = "round"; ctx.lineJoin = "round"; var grd = ctx.createLinearGradient(x1, y1, x2, y2); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.closePath(); ctx.strokeStyle = "rgba(255,255,255,1)"; ctx.stroke(); ctx.restore(); } function drawDot(x1, y1, r) { ctx.save(); ctx.beginPath(); //不写会和线连起来 ctx.fillStyle = "red"; //绘制成矩形 ctx.arc(x1, y1, r ? r : 2, 0, 2 * Math.PI); ctx.fill(); ctx.restore(); } </script> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 明达年度发烧碟MasterSuperiorAudiophile2017[DSF]
- 翁立友.2008-恨孤单【大旗】【WAV+CUE】
- 群星.2009-Magic.Cover日粤精华3CD【环球】【WAV+CUE】
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]