javascript - SVG and TweenLite - Select class within parent -
i have script modified tutorial animates svg strokes when containing div comes view (using inview.js). have added tweenlite animation changes svg fill-opacity 0 1. problem addition not care whether containing div in view (i'm using getelementsbyclassname target svg elements).
how can target elements class name if containing div in view? guess need use "parentelement" in tweenlite lines i'm not sure how.
i have modified function within svglines.js (everything after drawsvgpaths):
function replacerectswithpaths(parentelement) { var rects = $(parentelement).find('rect'); $.each(rects, function() { var rectx = $(this).attr('x'); var recty = $(this).attr('y'); var rectx2 = parsefloat(rectx) + parsefloat($(this).attr('width')); var recty2 = parsefloat(recty) + parsefloat($(this).attr('height')); var convertedpath = 'm' + rectx + ',' + recty + ' ' + rectx2 + ',' + recty + ' ' + rectx2 + ',' + recty2 + ' ' + rectx + ',' + recty2 + ' ' + rectx + ',' + recty; $(svg('path')) .attr('d', convertedpath) .attr('fill', $(this).attr('fill')) .attr('stroke', $(this).attr('stroke')) .attr('stroke-width', $(this).attr('stroke-width')) .insertafter(this); }); $(rects).remove(); } function replacelineswithpaths(parentelement) { var lines = $(parentelement).find('line'); $.each(lines, function() { var linex1 = $(this).attr('x1'); var liney1 = $(this).attr('y1'); var linex2 = $(this).attr('x2'); var liney2 = $(this).attr('y2'); var convertedpath = 'm' + linex1 + ',' + liney1 + ' ' + linex2 + ',' + liney2; $(svg('path')) .attr('d', convertedpath) .attr('fill', $(this).attr('fill')) .attr('stroke', $(this).attr('stroke')) .attr('stroke-width', $(this).attr('stroke-width')) .insertafter(this); }); $(lines).remove(); } function replacecircleswithpaths(parentelement) { var circles = $(parentelement).find('circle'); $.each(circles, function() { var cx = $(this).attr('cx'); var cy = $(this).attr('cy'); var r = $(this).attr('r'); var r2 = parsefloat(r * 2); var convertedpath = 'm' + cx + ', ' + cy + ' m' + (-r) + ', 0 ' + 'a ' + r + ', ' + r + ' 0 1,0 ' + r2 + ',0 ' + 'a ' + r + ', ' + r + ' 0 1,0 ' + (-r2) + ',0 '; $(svg('path')) .attr('d', convertedpath) .attr('fill', $(this).attr('fill')) .attr('stroke', $(this).attr('stroke')) .attr('stroke-width', $(this).attr('stroke-width')) .insertafter(this); }); $(circles).remove(); } function replaceellipseswithpaths(parentelement) { var ellipses = $(parentelement).find('ellipse'); $.each(ellipses, function() { var cx = $(this).attr('cx'); var cy = $(this).attr('cy'); var rx = $(this).attr('rx'); var ry = $(this).attr('ry'); var convertedpath = 'm' + cx + ', ' + cy + ' m' + (-rx) + ', 0 ' + 'a ' + rx + ', ' + ry + ' 0 1,0 ' + rx*2 + ',0 ' + 'a ' + rx + ', ' + ry + ' 0 1,0 ' + (-rx*2) + ',0 '; $(svg('path')) .attr('d', convertedpath) .attr('fill', $(this).attr('fill')) .attr('stroke', $(this).attr('stroke')) .attr('stroke-width', $(this).attr('stroke-width')) .insertafter(this); }); $(ellipses).remove(); } function replacepolygonswithpaths(parentelement) { var polygons = $(parentelement).find('polygon'); $.each(polygons, function() { var points = $(this).attr('points'); var polypoints = points.split(/[ ,]+/); var endpoint = polypoints[0] + ', ' + polypoints[1]; $(svg('path')) .attr('d', 'm' + points + ' ' + endpoint) .attr('fill', $(this).attr('fill')) .attr('stroke', $(this).attr('stroke')) .attr('stroke-width', $(this).attr('stroke-width')) .insertafter(this); }); $(polygons).remove(); } function replacepolylineswithpaths(parentelement) { var polylines = $(parentelement).find('polyline'); $.each(polylines, function() { var points = $(this).attr('points'); $(svg('path')) .attr('d', 'm' + points) .attr('fill', $(this).attr('fill')) .attr('stroke', $(this).attr('stroke')) .attr('stroke-width', $(this).attr('stroke-width')) .insertafter(this); }); $(polylines).remove(); } function hidesvgpaths(parentelement) { var paths = $(parentelement).find('path'); //for each path.. $.each( paths, function() { //get total length var totallength = this.gettotallength(); //set paths invisible $(this).css({ 'stroke-dashoffset': totallength, 'stroke-dasharray': totallength + ' ' + totallength }); }); } function drawsvgpaths(_parentelement, _timemin, _timemax, _timedelay) { var paths = $(_parentelement).find('path'); //for each path.. $.each( paths, function(i) { //get total length var totallength = this.gettotallength(); //set paths invisible $(this).css({ 'stroke-dashoffset': totallength, 'stroke-dasharray': totallength + ' ' + totallength }); //animate $(this).delay(_timedelay*i).animate({ 'stroke-dashoffset': 0 }, { duration: math.floor(math.random() * _timemax) + _timemin ,easing: 'easeinoutquad' }); }); } function replacewithpaths(parentelement) { replacerectswithpaths(parentelement); replacelineswithpaths(parentelement); replaceellipseswithpaths(parentelement); replacecircleswithpaths(parentelement); replacepolygonswithpaths(parentelement); replacepolylineswithpaths(parentelement); } function startsvganimation(parentelement) { drawsvgpaths(parentelement, 500, 500, 300); var svgwork = document.getelementsbyclassname('svgwork'); tweenlite.to(svgwork, 1, {attr:{"fill-opacity":1}}).delay(1); var svgservices = document.getelementsbyclassname('svgservices'); tweenlite.to(svgservices, 1, {attr:{"fill-opacity":1}}).delay(2.5); var svgcalc = document.getelementsbyclassname('svgcalc'); tweenlite.to(svgcalc, 1, {attr:{"fill-opacity":1}}).delay(3); var svgteam = document.getelementsbyclassname('svgteam'); tweenlite.to(svgteam, 1, {attr:{"fill-opacity":1}}).delay(1.5); var svgworkforus = document.getelementsbyclassname('svgworkforus'); tweenlite.to(svgworkforus, 1, {attr:{"fill-opacity":1}}).delay(2); } $(function() { //if (!modernizr.touch) { var animated = $('.js-animate'); replacewithpaths(animated); hidesvgpaths(animated); $(document).scroll(function() { $(animated).each(function(i) { if( $(this).visible() ) { startsvganimation(this); animated.splice(i,1); }; }); }); $(document).scroll(); //}; });
i think quick-fix replacing instances of document
parentelement
inside startsvganimation
function.
so startsvganimation
like:
function startsvganimation(parentelement){ drawsvgpaths(parentelement, 500, 500, 300); var svgwork = parentelement.getelementsbyclassname('svgwork'); tweenlite.to(svgwork, 1, {attr:{"fill-opacity":1}}).delay(1); var svgservices = parentelement.getelementsbyclassname('svgservices'); tweenlite.to(svgservices, 1, {attr:{"fill-opacity":1}}).delay(2.5); var svgcalc = parentelement.getelementsbyclassname('svgcalc'); tweenlite.to(svgcalc, 1, {attr:{"fill-opacity":1}}).delay(3); var svgteam = parentelement.getelementsbyclassname('svgteam'); tweenlite.to(svgteam, 1, {attr:{"fill-opacity":1}}).delay(1.5); var svgworkforus = parentelement.getelementsbyclassname('svgworkforus'); tweenlite.to(svgworkforus, 1, {attr:{"fill-opacity":1}}).delay(2); }
hope helps.
Comments
Post a Comment