3

I've used an html code to make a text glitch on a website, however I need to only make the animation glitch happen every 10 seconds as it's a little crazy at the moment.

Where I'm stuck is how to re-execute the html code using a JS setInterval(html, 10000). Is this even possible? Thanks!

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="svg">
<script>
    var html = '<defs>';
    html += '<filter id="glitch" x="0" y="0">';
    html += '<feColorMatrix in="SourceGraphic" mode="matrix" values="1 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 1 0" result="r" />';
    html += '<feOffset in="r" result="r" dx="-5">';
    html += '<animate attributeName="dx" attributeType="XML" values="0; -5; 0; -18; -2; -4; 0 ;-3; 0" dur="0.2s" repeatCount="indefinite" />';
    html += '</feOffset>';
    html += '<feColorMatrix in="SourceGraphic" mode="matrix" values="0 0 0 0 0  0 1 0 0 0  0 0 0 0 0  0 0 0 1 0" result="g"/>';
    html += '<feOffset in="g" result="g" dx="-5" dy="1">';
    html += '<animate attributeName="dx" attributeType="XML" values="0; 0; 0; -3; 0; 8; 0 ;-1; 0" dur="0.15s" repeatCount="indefinite" />';
    html += '</feOffset>';
    html += '<feColorMatrix in="SourceGraphic" mode="matrix" values="0 0 0 0 0  0 0 0 0 0  0 0 1 0 0  0 0 0 1 0" result="b"/>';
    html += '<feOffset in="b" result="b" dx="5" dy="2">'
    html += '<animate attributeName="dx" attributeType="XML" values="0; 3; -1; 4; 0; 2; 0 ;18; 0" dur="0.35s" repeatCount="indefinite"/>';
    html += '</feOffset>';
    html += '<feBlend in="r" in2="g" mode="screen" result="blend" />';
    html += '<feBlend in="blend" in2="b" mode="screen" result="blend" />';
    html += '</filter>';
    html += '</defs>';
</script></svg>
mikegyi
  • 45
  • 5

1 Answers1

3

You don't need setInterval() for this - it can all be done in the SVG animation. Following the strategy outlined here, make a "dummy" animation element that does nothing, and tie the start/end of the other animations to it:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">

  <defs>
    <animateTransform begin="myGlitch.end" id="pause" dur="10s" type="translate" attributeType="XML" attributeName="transform" />
    <filter id="glitch" x="0" y="0">
      <feColorMatrix in="SourceGraphic" mode="matrix" values="1 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 1 0" result="r" />
      <feOffset in="r" result="r" dx="-5">
        <animate attributeName="dx" attributeType="XML" values="0; -5; 0; -18; -2; -4; 0 ;-3; 0" dur="0.2s" begin="0; pause.end" />
      </feOffset>
      <feColorMatrix in="SourceGraphic" mode="matrix" values="0 0 0 0 0  0 1 0 0 0  0 0 0 0 0  0 0 0 1 0" result="g" />
      <feOffset in="g" result="g" dx="-5" dy="1">
        <animate attributeName="dx" attributeType="XML" values="0; 0; 0; -3; 0; 8; 0 ;-1; 0" dur="0.15s" begin="0; pause.end" />
      </feOffset>
      <feColorMatrix in="SourceGraphic" mode="matrix" values="0 0 0 0 0  0 0 0 0 0  0 0 1 0 0  0 0 0 1 0" result="b" />
      <feOffset in="b" result="b" dx="5" dy="2">
        <animate id="myGlitch" attributeName="dx" attributeType="XML" values="0; 3; -1; 4; 0; 2; 0 ;18; 0" dur="0.35s" begin="0; pause.end" />
      </feOffset>
      <feBlend in="r" in2="g" mode="screen" result="blend" />
      <feBlend in="blend" in2="b" mode="screen" result="blend" />
    </filter>
  </defs>
</svg>

Notice that the pause animation starts after the end of the last glitch animation (begin=myGlitch.end), and each of the glitch animations start at the end of the pause element (begin="0; pause.end").

Community
  • 1
  • 1
rphv
  • 5,409
  • 3
  • 29
  • 47