Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
查看路径
What's in a name? That which we call a rose By any other word would smell as sweet...
css
body{background: #be0e2e;font-family: consolas; color:white; } svg { width: 110vmin; display:block; position:absolute; margin:auto; top:0;bottom:0;left:0;right:0; z-index:1; } text { fill: white; font-family: consolas; font-size: 9px; } p{position:absolute;z-index:2} label{-webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;opacity:.8}
JavaScript
let rid = null; // request animation id const SVG_NS = "http://www.w3.org/2000/svg"; const pathlength = shape.getTotalLength(); let t = 0; // at the begining of the path let lengthAtT = pathlength * t; let d = shape.getAttribute("d"); // 1. build the d array let n = d.match(/C/gi).length; // how many times let pos = 0; // the position, used to find the indexOf the nth C class subPath { constructor(d) { this.d = d; this.get_PointsRy(); this.previous = subpaths.length > 0 ? subpaths[subpaths.length - 1] : null; this.measurePath(); this.get_M_Point(); //lastPoint this.lastCubicBezier; this.get_lastCubicBezier(); } get_PointsRy() { this.pointsRy = []; let temp = this.d.split(/[A-Z,a-z\s,]/).filter(v => v); // remove empty elements temp.map(item => { this.pointsRy.push(parseFloat(item)); }); //this.pointsRy numbers not strings } measurePath() { let path = document.createElementNS(SVG_NS, "path"); path.setAttributeNS(null, "d", this.d); // no need to append it to the SVG // the lengths of every path in dry this.pathLength = path.getTotalLength(); } get_M_Point() { if (this.previous) { let p = this.previous.pointsRy; let l = p.length; this.M_point = [p[l - 2], p[l - 1]]; } else { let p = this.pointsRy; this.M_point = [p[0], p[1]]; } } get_lastCubicBezier() { let lastIndexOfC = this.d.lastIndexOf("C"); let temp = this.d .substring(lastIndexOfC + 1) .split(/[\s,]/) .filter(v => v); let _temp = []; temp.map(item => { _temp.push(parseFloat(item)); }); this.lastCubicBezier = [this.M_point]; for (let i = 0; i < _temp.length; i += 2) { this.lastCubicBezier.push(_temp.slice(i, i + 2)); } } } let subpaths = []; // create new subPaths for (let i = 0; i < n; i++) { // finds the of nth C in d let newpos = d.indexOf("C", pos + 1); if (i > 0) { // if it's not the first C let sPath = new subPath(d.substring(0, newpos)); subpaths.push(sPath); } //change the value of the position pos pos = newpos; } // at the end add d to the subpaths array subpaths.push(new subPath(d)); // 2. get the index of the bezierLengths where the point at t is let index; for (index = 0; index < subpaths.length; index++) { if (subpaths[index].pathLength >= lengthAtT) { break; } } function get_T(t, index) { let T; lengthAtT = pathlength * t; if (index > 0) { T = (lengthAtT - subpaths[index].previous.pathLength) / (subpaths[index].pathLength - subpaths[index].previous.pathLength); } else { T = lengthAtT / subpaths[index].pathLength; } //console.log(T) return T; } let T = get_T(t, index); let newPoints = getBezierPoints(T, subpaths[index].lastCubicBezier); drawCBezier(newPoints, partialPath, index); function getBezierPoints(t, points) { let helperPoints = []; // helper points 0,1,2 for (let i = 1; i < 4; i++) { //points.length must be 4 !!! let p = lerp(points[i - 1], points[i], t); helperPoints.push(p); } // helper points 3,4 helperPoints.push(lerp(helperPoints[0], helperPoints[1], t)); helperPoints.push(lerp(helperPoints[1], helperPoints[2], t)); // helper point 5 is where the first Bézier ends and where the second Bézier begins helperPoints.push(lerp(helperPoints[3], helperPoints[4], t)); // points for the dynamic bézier let firstBezier = [ points[0], helperPoints[0], helperPoints[3], helperPoints[5] ]; //console.log(firstBezier) return firstBezier; } function lerp(A, B, t) { // a virtual line from A to B // get the position of a point on this line // if(t == .5) the point in in the center of the line // 0 <= t <= 1 let ry = [ (B[0] - A[0]) * t + A[0], //x (B[1] - A[1]) * t + A[1] //y ]; return ry; } function drawCBezier(points, path, index) { let d; if (index > 0) { d = subpaths[index].previous.d; } else { d = `M${points[0][0]},${points[0][1]} C`; } // points.length == 4 for (let i = 1; i < 4; i++) { d += ` ${points[i][0]},${points[i][1]} `; } //console.log(d) partialPath.setAttributeNS(null, "d", d); } /* _t.addEventListener("input", ()=>{ t = _t.value; lengthAtT = pathlength*t; for(index = 0; index < subpaths.length; index++){ if(subpaths[index].pathLength >= lengthAtT){break; } } T = get_T(t, index); newPoints = getBezierPoints(T,subpaths[index].lastCubicBezier); drawCBezier(newPoints, partialPath, index); })*/ t = 0.025; function Typing() { rid = window.requestAnimationFrame(Typing); if (t >= 1) { window.cancelAnimationFrame(rid); rid = null; } else { t += 0.0025; } lengthAtT = pathlength * t; for (index = 0; index < subpaths.length; index++) { if (subpaths[index].pathLength >= lengthAtT) { break; } } T = get_T(t, index); newPoints = getBezierPoints(T, subpaths[index].lastCubicBezier); drawCBezier(newPoints, partialPath, index); } Typing(); theSvg.addEventListener("click", () => { if (rid) { window.cancelAnimationFrame(rid); rid = null; } else { if (t >= 1) { t = 0.025; } rid = window.requestAnimationFrame(Typing); } }); cb.addEventListener("input", () => { if (cb.checked) { useThePath.style.display = "block"; } else { useThePath.style.display = "none"; } });
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>情人节文字动画-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号