Advertisement
itskrystalized

background-clip: text >> javascript

Jun 13th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.  /**
  3.   -webkit-background-clip: text Polyfill
  4.  
  5.   # What? #
  6.   A polyfill which replaces the specified element with a SVG
  7.   in browser where &quot;-webkit-background-clip: text&quot;
  8.   is not available.
  9.  
  10.   Fork it on GitHub
  11.   https://github.com/TimPietrusky/background-clip-text-polyfill
  12.  
  13.   # 2013 by Tim Pietrusky
  14.   # timpietrusky.com
  15. **/
  16.  
  17. Element.prototype.backgroundClipPolyfill = function () {
  18.   var a = arguments[0],
  19.       d = document,
  20.       b = d.body,
  21.       el = this;
  22.  
  23.   function hasBackgroundClip() {
  24.     return b.style.webkitBackgroundClip != undefined;
  25.   };
  26.  
  27.   function addAttributes(el, attributes) {
  28.     for (var key in attributes) {
  29.       el.setAttribute(key, attributes[key]);
  30.     }
  31.   }
  32.  
  33.   function createSvgElement(tagname) {
  34.     return d.createElementNS(&#x27;http://www.w3.org/2000/svg&#x27;, tagname);
  35.   }
  36.  
  37.   function createSVG() {
  38.     var a = arguments[0],
  39.         svg = createSvgElement(&#x27;svg&#x27;),
  40.         pattern = createSvgElement(&#x27;pattern&#x27;),
  41.         image = createSvgElement(&#x27;image&#x27;),
  42.         text = createSvgElement(&#x27;text&#x27;);
  43.    
  44.     // Add attributes to elements
  45.     addAttributes(pattern, {
  46.       &#x27;id&#x27; : a.id,
  47.       &#x27;patternUnits&#x27; : &#x27;userSpaceOnUse&#x27;,
  48.       &#x27;width&#x27; : a.width,
  49.       &#x27;height&#x27; : a.height
  50.     });
  51.    
  52.     addAttributes(image, {
  53.       &#x27;width&#x27; : a.width,
  54.       &#x27;height&#x27; : a.height
  55.     });
  56.     image.setAttributeNS(&#x27;http://www.w3.org/1999/xlink&#x27;, &#x27;xlink:href&#x27;, a.url);
  57.    
  58.     addAttributes(text, {
  59.       &#x27;x&#x27; : 0,
  60.       &#x27;y&#x27; : 80,
  61.       &#x27;class&#x27; : a[&#x27;class&#x27;],
  62.       &#x27;style&#x27; : &#x27;fill:url(#&#x27; + a.id + &#x27;);&#x27;
  63.     });
  64.    
  65.     // Set text
  66.     text.textContent = a.text;
  67.      
  68.     // Add elements to pattern
  69.     pattern.appendChild(image);
  70.      
  71.     // Add elements to SVG
  72.     svg.appendChild(pattern);
  73.     svg.appendChild(text);
  74.    
  75.     return svg;
  76.   };
  77.  
  78.   /*
  79.    * Replace the element if background-clip
  80.    * is not available.
  81.    */
  82.   if (!hasBackgroundClip()) {
  83.     var img = new Image();
  84.     img.onload = function() {
  85.       var svg = createSVG({
  86.         &#x27;id&#x27; : a.patternID,
  87.         &#x27;url&#x27; : a.patternURL,
  88.         &#x27;class&#x27; : a[&#x27;class&#x27;],
  89.         &#x27;width&#x27; : this.width,
  90.         &#x27;height&#x27; : this.height,
  91.         &#x27;text&#x27; : el.textContent
  92.       });
  93.      
  94.       el.parentNode.replaceChild(svg, el);
  95.     }
  96.     img.src = a.patternURL;
  97.   }
  98. };
  99.  
  100. var element = document.querySelector(&#x27;.headline&#x27;);
  101.  
  102. /*
  103.  * Call the polyfill
  104.  *
  105.  * patternID : the unique ID of the SVG pattern
  106.  * patternURL : the URL to the background-image
  107.  * class : the css-class applied to the SVG
  108.  */
  109. element.backgroundClipPolyfill({
  110.   &#x27;patternID&#x27; : &#x27;mypattern&#x27;,
  111.   &#x27;patternURL&#x27; : &#x27;http://timpietrusky.com/cdn/army.png&#x27;,
  112.   &#x27;class&#x27; : &#x27;headline&#x27;
  113. });
  114. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement