regex - JavaScript: Custom Function For Path's "d" attribute -
I am trying to create a function so that I can edit the "D" attribute of SVG Javascript can be reused in the path.
At this time, I am capable of attaining the attribute and changing it, although it is on an ad-hoc basis. I want to make a reusable function. For example, if I have a SVG:
& Lt; / Svg & gt; I do this to edit the "D" attribute with javascript:
var element = document.query selector ("MyPath" ); Element.setAttribute ("D", "M475.385 ..."); How do I change this kind of workflow into a reusable function? The function should do the following:
- Perform a SVG query by class name.
- Query the path element of SVG by class name.
- "D" is the attribute of that path.
- Change the "d" attribute.
I want to be able to set it so that I can call only one function and this is what I need to do, finally sets the "D" attribute I believe That the best way to do this would be through some kind of custom regex.
Maybe I'm not you understand, but what I think is what you are asking Easy: function set path (svgClass, pathClass, newPath) {var element = document.query Selector ('.' + SvgClass + '.' + Path Class); Element.setAttribute ("d", newpath); } / Suppose you have a class "mySvg" & lt; Svg & gt; Add to Element Setpeth ('mySvg', 'myPath', 'M475.385,13.143 C25.477,8.58,21.678,5,17,5'); Or if you want to reuse a single class without the need of supplying them every time, you can:
Function getPathSetter (svgClass, pathClass) {return function (newpath) {var element = document.query selector ('.' + SvgClass + '.' + + + Path class); Element.setAttribute ("d", newpath); }; } / Suppose you have a class "mySvg" & lt; Svg & gt; Add to Element var ps = getPathSetter ('mySvg', 'mypath'); Ps ('m375.385,13.143c25.477,8.58,21.678,5,17,5'); SetTimeout (function (('m 75.385, 13.143c, 25.477,8.58, 21.678,5, 17,5');}, 3000); ... or, especially If you can do with classes, you can:
function path (svgClass, path class) {this.svgClass = svgClass; .pathClass = Path class;} Path.prototype.getPathElement = function () {return Document.query selector ('.' This.svgClass + '.' + This.pathClass);}; Path.prototype.set = function ( NewPath) {var element = this.getPathElement (); Element.setAttribute ("d", newpath);} Path.prototype.get = function () {var element = this.getPathElement (); return element.getAttribute (" D ");}; // Assume that you add a class" mySvg "to & lt; svg & gt; Element var p = new path ('mySvg', 'mypath'); p.set ('M375.385,13.143C25.477,8.58,21.678,5,17,5'); set timeout (function () (p .set ('M75.385, 13.143 c25.477,8.58, 21.678,5, 17.5'); Alert (PGT) ());}, 3000); < / Div>
Comments
Post a Comment