Guest User

Untitled

a guest
Oct 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. var tree_x = tributary.sw/2 + 410;
  2. var tree_y = 55;
  3. var tree_scale = .24;
  4. var ncurves = 20;
  5.  
  6. var head = [565, 266];
  7. var root = [0, 250];
  8.  
  9. var left_heads = [
  10. [358, 284],
  11. [363, 170],
  12. [330, 199],
  13. [380, 186],
  14. [330, 150],
  15. [405, 113]
  16. ];
  17. var right_heads = [
  18. [558, 153],
  19. [515, 183],
  20. [595, 210],
  21. [580, 250],
  22. [567, 260]
  23. ];
  24.  
  25. var font_size = 15;
  26. var font_face = "Arial";
  27.  
  28. tributary.duration = 3000;
  29. tributary.ease = d3.ease("quad");
  30. tributary.loop = "pingpong"
  31.  
  32. var sentences = [
  33. "Better to be thought a fool then open your mouth and remove all doubt",
  34. "I don't give a damn for a man that can only spell a word one way.",
  35. "I don't like to commit myself about heaven and hell - you see, I have friends in both places."
  36. ]
  37.  
  38.  
  39. tributary.init = function(g) {
  40.  
  41. var make_curve = function(i) {
  42. var start, end, curve, amp;
  43. if(i % 2 === 0) { //even curves go left
  44. start = left_heads[(i/2) % left_heads.length];
  45. curve = -100 * Math.random() - 20;
  46. end = [
  47. root[0] + Math.random() * 20 -10,
  48. root[1] + Math.random() * 100 -30
  49. ]
  50. } else {
  51. start = right_heads[parseInt(i/2) % right_heads.length];
  52. curve = 100 * Math.random() + 20;
  53. end = [
  54. root[0] + Math.random() * 20 -10,
  55. root[1] + Math.random() * 100 -30
  56. ]
  57. }
  58. var xoff = -30 + Math.random() * 100;
  59. var yoff = -15 + Math.random() * 30;
  60.  
  61. amp = 40 * Math.random() + 35;
  62. var cdata = {
  63. i: i,
  64. start: start,
  65. end: end,
  66. curve: curve,
  67. amp: amp,
  68. xoff: xoff,
  69. yoff: yoff
  70. }
  71.  
  72.  
  73. var c = g.selectAll("path#curve" + i)
  74. .data([cdata])
  75. .enter()
  76. .append("path").attr("id", "curve" + i)
  77. .classed("curve", true)
  78.  
  79. c
  80. //.attr('stroke','blue')
  81. //.attr('stroke-width',3)
  82. .attr('fill','none')
  83. .attr('d', function(d) {
  84. var path = 'M '+d.start[0]+' '+d.start[1]+' q '+d.curve+' '+d.amp+' '+d.end[0]+' '+d.end[1];
  85. return path
  86. })
  87. var sentence = sentences[i%sentences.length];
  88. var text = g.append('text')
  89. .attr('font-size',font_size)
  90. .attr('font-family',font_face)
  91.  
  92.  
  93. var string = text.selectAll("textPath.txt")
  94. .data([sentence])
  95. .enter()
  96. .append('textPath')
  97. .attr('xlink:href','#curve' + i)
  98. .classed("txt", true)
  99. .text(sentence)
  100. }
  101. //i happen to host this on the same domain, but you
  102. //can load a remote file as long as Access-Control-Allow-Origin:* header is set
  103. d3.html("/static/svgs/tree.svg", function(data) {
  104. g.node().appendChild(data);
  105.  
  106. var tree_trunk = g.select("#tree_trunk");
  107. var bbox = tree_trunk.node().getBBox();
  108. tree_trunk
  109. .attr("transform", "translate(" + [tree_x - bbox.width/2, tree_y] + ")scale(" + tree_scale + ")")
  110.  
  111. g.append("circle")
  112. .attr("cx", head[0])
  113. .attr("cy", head[1])
  114. .attr("r", 13)
  115. .attr("fill", "none")
  116. //.attr("stroke", "#ff0000")
  117.  
  118. var curves = d3.range(ncurves);
  119. for(i in curves) {
  120. make_curve(i);
  121. }
  122. });
  123. }
  124.  
  125. tributary.run = function(g,t) {
  126. g.selectAll(".curve")
  127. .attr('d', function(d) {
  128.  
  129. var end = [
  130. d3.interpolate(d.end[0], d.end[0] + d.xoff)(t),
  131. d3.interpolate(d.end[1], d.end[1] + d.yoff)(t)
  132. ]
  133. var path = 'M '+d.start[0]+' '+d.start[1]+' q '+d.curve+' '+d.amp+' '+end[0]+' '+end[1];
  134. return path
  135. })
  136. g.selectAll(".txt")
  137. .text(function(d) {
  138. return d;
  139. })
  140. }
Add Comment
Please, Sign In to add comment