0

I want to get "transform" data below using Javascript.

<svg id="svgHarita" width="5025px" height="2159px" viewBox="0 0 5025 2159" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="transform: scale(3) translate(45.5px, 122px);">
  • var x // output: scale(3)
  • var y // output: 45.5px
  • var z // output: 122px

Thanks, Regards

1
  • 1
    Please visit the help center, take the tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output. Commented Oct 10, 2018 at 5:57

3 Answers 3

2

getAttribute and Regex

var regex = /scale\((\d)\).*?translate\((.*?), (.*?)\)/
console.log(
  document.getElementById("svgHarita").getAttribute("style").match(regex)
);  
<svg id="svgHarita" width="5025px" height="2159px" viewBox="0 0 5025 2159" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="transform: scale(3) translate(45.5px, 122px);">

Sign up to request clarification or add additional context in comments.

Comments

0

You can add simply using style.transform

document.getElementById("svgHarita").style.WebkitTransform = "scale(3) translate(45.5px, 122px)"; 
document.getElementById("svgHarita").style.msTransform = "scale(3) translate(45.5px, 122px)"; 
document.getElementById("svgHarita").style.transform = "scale(3) translate(45.5px, 122px)";

4 Comments

I do not think that is what was asked
I think, he/she want to transfrom svg using css
I think he wants to get the values of scale and translates
Okey it will be
0

It's using js, will be better using some better regExp, but it does its job

let el = document.querySelector("#svgHarita")
let arr = el.style.transform.replace(/[\(\)translatescale,px ]/gi, " ").replace(/ +/gi, " ").trim().split(" ")
let x = arr[0]
let y = arr[1]
let z = arr[2]
console.log(x, y, z)
<svg id="svgHarita" width="5025px" height="2159px" viewBox="0 0 5025 2159" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="transform: scale(3) translate(45.5px, 122px);">

2 Comments

it removes all text then leaves numbers and period, its replaced by " " , then we replace that " " " " s p a c e d " text to a "spaced" one, finally we split and get the array using the " " as splitter
Yes it is indeed simpler to use a well crafted RegEx :) - Also you lost the px

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.