d3实现文字蛇形动画效果
代码语言:html
所属分类:布局界面
代码描述:d3实现文字蛇形动画效果
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> html { line-height: 1.15; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; font-family: "Lato", sans-serif; } body { margin: 0; overflow-x: hidden; } section { display: block; } svg { overflow: visible; } section { background-color: white; } h1, h2, h3, h4, h5, h6 { margin-top: 0; margin-bottom: 0.5rem; font-weight: 300; } #viz-word-snake { font-size: 14px; white-space: pre; cursor: default; width: 700px; margin: 0 auto; } .circle-path { fill: none; pointer-events: none; } .node .circle-background { fill: none; pointer-events: all; cursor: pointer; } .circle-center-translation { font-family: "Lato", sans-serif; font-size: 0.8em; font-weight: 400; text-anchor: middle; pointer-events: none; } .circle-center-original { font-family: "Dancing Script", cursive; font-size: 1.7em; text-anchor: middle; pointer-events: none; } .circle-center-language { font-family: "Lato", sans-serif; font-size: 0.8em; font-weight: 400; font-style: italic; text-anchor: middle; pointer-events: none; } .circle-path-legend { font-family: "Lato", sans-serif; font-size: 0.8em; pointer-events: none; } .circle-path-text { font-family: "Dancing Script", cursive; font-size: 1.1em; pointer-events: none; } </style> </head> <body translate="no"> <div id="viz-word-snake"></div> <script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/d3.v5.js"></script> <script > var darkgrey = "#161616", middlegrey = "#a7a7a7", lightgrey = "#afafaf"; var languageMap = []; languageMap["de"] = "German"; languageMap["es"] = "Spanish"; languageMap["fr"] = "French"; languageMap["it"] = "Italian"; languageMap["ja"] = "Japanese"; languageMap["nl"] = "Dutch"; languageMap["pl"] = "Polish"; languageMap["pt"] = "Portuguese"; languageMap["ru"] = "Russian"; languageMap["tr"] = "Turkish"; languageMap["all"] = "All languages"; var divWidth = parseInt(d3.select("#viz-word-snake").style("width")); var margin = { top: 10, right: 10, bottom: 40, left: 10 }; var width = divWidth - margin.left - margin.right; var height = width; console.log(divWidth,width,height) //SVG container var svg = d3.select("#viz-word-snake").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + (margin.left) + "," + (margin.top) + ")"); var parseTime = d3.timeParse("%Y-%m-%d"); // timeParse is added in version v4 var angle = 35 * Math.PI / 180; var radius = 75; var newXmargin = margin.left; var n; //Round number to 2 behind the decimal function round2(num) { return (Math.round((num + 0.00001) * 100) / 100); }//round2 function calculateGrid() { //How many circles fir in one "row" var s = width / Math.cos(angle); var numCircle = Math.min(4, Math.floor(s / (2 * radius))); //I don't want 1 circle if (numCircle === 1) numCircle = 2; //If it's not an exact fit, make it so radius = Math.min(radius, round2((s / numCircle) / 2)); //Save the x-locations if each circle var xLoc = new Array(numCircle); for (var i = 0; i < numCircle; i++) { xLoc[i] = round2((1 + 2 * i) * radius * Math.cos(angle)); }//for i //Locations for the textPath var xLocArc = new Array(numCircle + 1); for (var i = 0; i <= numCircle; i++) { xLocArc[i] = round2(2 * i * radius * Math.cos(angle)); }//for i //New width & divide margins so it will sit in the center width = xLocArc[numCircle]; newXmargin = round2((divWidth - width) / 2); svg.attr("transform", "translate(" + newXmargin + "," + (margin.top) + ")"); return { xLoc: xLoc, xLocArc: xLocArc, numCircle: numCircle }; }//function calculateGrid var grid = calculateGrid(); var top1 =[ {"language":"nl","original":"collega","translation":"colleague","frequency":"38418"}, {"language":"fr","original":"bien | bon","translation":"good","frequency":"204516"}, {"language":"de","original":"schön","translation":"beautiful","frequency":"169808"}, {"language":"it","original":"bene | buon | buono","translation":"good","frequency":"66672"}, {"language":"ja","original":"仕事 | 作業","translation":"work","frequency":"33468"}, {"language":"pl","original":"czwartek","translation":"thursday","frequency":"66756"}, {"language":"pt","original":"bom | bem | boa","translation":"good","frequency":"140778"}, {"language":"ru","original":"мама","translation":"mama","frequency":"95900"}, {"language":"es","original":"hermosa | hermoso | bonito","translation":"beautiful","frequency":"180996"}, {"language":"tr","original":"güzel","translation":"beautiful","frequency":"63927"} ] var top100Overall = [{"translation":"beautiful","rank":"1"},{"translation":"thursday","rank":"2"},{"translation":"tuesday","rank":"3"},{"translation":"wednesday","rank":"4"},{"translation":"good","rank":"5"},{"translation":"address","rank":"6"},{"translation":"place","rank":"7"},{"translation":"little","rank":"8"},{"translation":"saturday","rank":"9"},{"translation":"white","rank":"10"},{"translation":"love","rank":"11"},{"translation":"request","rank":"12"},{"translation":"english","rank":"13"},{"translation":"great","rank":"14"},{"translation":"happy","rank":"15"},{"translation":"friday","rank":"16"},{"translation":"answer","rank":"17"},{"translation":"business","rank":"18"},{"translation":"city","rank":"19"},{"translation":"sunday","rank":"20"},{"translation":"teacher","rank":"21"},{"translation":"school","rank":"22"},{"translation":"brother","rank":"23"},{"translation":"monday","rank":"24"},{"translation":"language","rank":"25"},{"translation":"autumn","rank":"26"},{"translation":"summary","rank":"27"},{"translation":"job","rank":"28"},{"translation":"man","rank":"29"},{"translation":"available","rank":"30"},{"translation":"quantity","rank":"31"},{"translation":"street","rank":"32"},{"translation":"description","rank":"33"},{"translation":"time","rank":"34"},{"translation":"big","rank":"35"},{"translation":"dog","rank":"36"},{"translation":"work","rank":"37"},{"translation":"daughter","rank":"38"},{"translation":"fast","rank":"39"},{"translation":"company","rank":"40"},{"translation":"name","rank":"41"},{"translation":"chair","rank":"42"},{"translation":"house","rank":"43"},{"translation":"proposal","rank":"44"},{"translation":"well","rank":"45"},{"translation":"confirmation","rank":"46"},{"translation":"kitchen","rank":"47"},{"translation":"children","rank":"48"},{"translation":"order","rank":"49"},{"translation":"friend","rank":"50"},{"translation":"development","rank":"51"},{"translation":"table","rank":"52"},{"translation":"country","rank":"53"},{"translation":"home","rank":"54"},{"translation":"width","rank":"55"},{"translation":"first name","rank":"56"},{"translation":"holiday","rank":"57"},{"translation":"regards","rank":"58"},{"translation":"reply","rank":"59"},{"translation":"decision","rank":"60"},{"translation":"important","rank":"61"},{"translation":"girl","rank":"62"},{"translation":"life","rank":"63"},{"translation":"bad","rank":"64"},{"translation":"right","rank":"65"},{"translation":"aunt","rank":"66"},{"translation":"bed","rank":"67"},{"translation":"father","rank":"68"},{"translation":"offer","rank":"69"},{"translation":"sorry","rank":"70"},{"translation":"nothing","rank":"71"},{"translation":"summer","rank":"72"},{"translation":"target","rank":"73"},{"translation":"power","rank":"74"},{"translation":"colleague","rank":"75"},{"translation":"height","rank":"76"},{"translation":"price","rank":"77"},{"translation":"small","rank":"78"},{"translation":"christmas","rank":"79"},{"translation":"car","rank":"80"},{"translation":"search","rank":"81"},{"translation":"invoice","rank":"82"},{"translation":"shirt","rank":"83"},{"translation":"heart","rank":"84"},{"translation":"amount","rank":"85"},{"translation":"people","rank":"86"},{"translation":"sad","rank":"87"},{"translation":"day","rank":"88"},{"translation":"kind","rank":"89"},{"translation":"family","rank":"90"},{"translation":"responsible","rank":"91"},{"translation":"water","rank":"92"},{"translation":"translator","rank":"93"},{"translation":"son","rank":"94"},{"translation":"month","rank":"95"},{"translation":"tree","rank":"96"},{"translation.........完整代码请登录后点击上方下载按钮下载查看
网友评论0