drawfillsvg.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Draw Fill SVG
  3. *
  4. * A plugin that simulates a "draw" effect on the stroke of an SVG, fades out
  5. * the stroke, and fades in a fill colour.
  6. *
  7. * Licensed under the MIT license.
  8. * http://www.opensource.org/licenses/mit-license.php
  9. *
  10. * Copyright 2014, Call Me Nick
  11. * http://callmenick.com
  12. */
  13. (function( window ){
  14. 'use strict';
  15. /**
  16. * Cross browser transition end events
  17. *
  18. * Use modernizr to detect cross browser transition end events. Make sure
  19. * to include Modernizr in your doc and have "Modernizr.prefixed()" checked
  20. * off in the extensibility section.
  21. */
  22. var transEndEventNames = {
  23. "WebkitTransition" : "webkitTransitionEnd",
  24. "MozTransition" : "transitionend",
  25. "OTransition" : "oTransitionEnd",
  26. "msTransition" : "MSTransitionEnd",
  27. "transition" : "transitionend"
  28. },
  29. transEndEventName = transEndEventNames[ Modernizr.prefixed('transition') ];
  30. /**
  31. * Extend obj function
  32. *
  33. */
  34. function extend( a, b ) {
  35. for( var key in b ) {
  36. if( b.hasOwnProperty( key ) ) {
  37. a[key] = b[key];
  38. }
  39. }
  40. return a;
  41. }
  42. /**
  43. * DrawFillSVG constructor
  44. *
  45. */
  46. function DrawFillSVG( options ) {
  47. this.options = extend( {}, this.options );
  48. extend( this.options, options );
  49. this._init();
  50. }
  51. /**
  52. * DrawFillSVG options
  53. *
  54. * Available options:
  55. * elementId - the ID of the element to draw
  56. */
  57. DrawFillSVG.prototype.options = {
  58. elementId : "svg"
  59. }
  60. /**
  61. * DrawFillSVG _init
  62. *
  63. * Initialise DrawFillSVG
  64. */
  65. DrawFillSVG.prototype._init = function() {
  66. this.svg = document.getElementById(this.options.elementId);
  67. this.paths = this.svg.querySelectorAll("path");
  68. this._initAnimation();
  69. }
  70. /**
  71. * DrawFillSVG _initAnimation()
  72. *
  73. * Reset some style properties on our paths, add some transitions, set the
  74. * stroke-dasharray to the length of the path, and the stroke-dashoffset to
  75. * the length of the path pushing it out of view initially. Then, set the
  76. * stroke-dashoffset to 0, animating the strokes in a drawing manner. Then,
  77. * run the path filler sequence.
  78. */
  79. DrawFillSVG.prototype._initAnimation = function() {
  80. for ( var i = 0; i < this.paths.length; i++ ) {
  81. var path = this.paths[i];
  82. var length = path.getTotalLength();
  83. // reset opacities
  84. path.style.fillOpacity = 0;
  85. path.style.strokeOpacity = 1;
  86. // reset transitions
  87. path.style.transition = path.style.WebkitTransition = "none";
  88. // reset stroke dash array and stroke dash offset
  89. path.style.strokeDasharray = length + " " + length;
  90. path.style.strokeDashoffset = length;
  91. path.getBoundingClientRect();
  92. // apply new transitions
  93. path.style.transition = path.style.WebkitTransition = "stroke-dashoffset 2s ease-in-out";
  94. // go baby go
  95. path.style.strokeDashoffset = 0;
  96. // fill the path
  97. this._fillPath( path );
  98. }
  99. }
  100. /**
  101. * DrawFillSVG _fillPath()
  102. *
  103. * Resets the transition props, then fills the path and fades out the stroke
  104. * by updating the styles.
  105. */
  106. DrawFillSVG.prototype._fillPath = function( path ) {
  107. path.addEventListener( transEndEventName, function() {
  108. // reset transitions
  109. path.style.transition = path.style.WebkitTransition = "none";
  110. path.style.transition = path.style.WebkitTransition = "fill-opacity 1s ease-in-out, stroke-opacity 1s ease-in-out";
  111. // edit props
  112. path.style.fillOpacity = 1;
  113. path.style.strokeOpacity = 0;
  114. } );
  115. }
  116. /**
  117. * DrawFillSVG replay
  118. *
  119. * A public function that allows you to replay the animation if you want. For
  120. * example, click a button, and replay the animation.
  121. */
  122. DrawFillSVG.prototype.replay = function() {
  123. this._initAnimation();
  124. }
  125. /**
  126. * Add to global namespace
  127. */
  128. window.DrawFillSVG = DrawFillSVG;
  129. })( window );