The transform attribute defines a list of transform definitions that are applied to an element and the element's children. Note: As of SVG2, transform is a presentation attribute, meaning it can be used as a CSS property.
Like HTML, SVGs are represented using the Document Object Model (DOM) and so can be manipulated with Javascript relatively easily, especially if you are familiar with using JS with HTML.
GetScreenCTM method. Returns the transformation matrix from current user units (i.e., after application of the 'transform' attribute, if any) to the parent user agent's notice of a “pixel”.
Just use the element type selector and add the transform: rotate(180deg) property to it like in the below snippet.
There are two ways to get/set transform values for SVG elements in Chrome, Firefox, IE, or any standards-fearing SVG user agent:
// Getting
var xforms = myElement.getAttribute('transform');
var parts = /translate\(\s*([^\s,)]+)[ ,]([^\s,)]+)/.exec(xforms);
var firstX = parts[1],
firstY = parts[2];
// Setting
myElement.setAttribute('transform','translate(30,100)');
Pros: Simple to set and understand (same as the markup).
Cons: Have to parse the string value to find what you want; for animated values, can't ask for the base value when the animation is active; feels dirty.
// Getting
var xforms = myElement.transform.baseVal; // An SVGTransformList
var firstXForm = xforms.getItem(0); // An SVGTransform
if (firstXForm.type == SVGTransform.SVG_TRANSFORM_TRANSLATE){
var firstX = firstXForm.matrix.e,
firstY = firstXForm.matrix.f;
}
// Setting
myElement.transform.baseVal.getItem(0).setTranslate(30,100);
Pros: No need to try to parse strings on your own; preserves existing transform lists (you can query or tweak just a single transformation in the list); you get real values, not strings.
Cons: Can be confusing to understand at first; more verbose for setting simple values; no convenient method for extracting rotation, scale, or skew values from the SVGTransform.matrix
, lack of browser support.
You may find a tool I wrote for exploring the DOM helpful in this.
transform
property that is an SVGAnimatedTransformList
.SVGAnimatedTransformList
to see the properties and methods that object supports.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With