I
I
intriga932020-12-22 11:41:01
JavaScript
intriga93, 2020-12-22 11:41:01

How to set viewBox programmatically?

let svgElem = document.createElement('svg');
svgElem.setAttribute('width', 500);
svgElem.setAttribute('height', 500);
svgElem.setAttribute('viewBox', '0 0 50 50');

does not give the desired result

instead of viewBox="0 0 50 50"
it will be viewbox="0 0 50 50"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-12-22
@intriga93

svgElem.setAttributeNS('http://www.w3.org/2000/svg', 'viewBox', '0 0 50 50')

Or let the element creation look like this:
document.createElementNS('http://www.w3.org/2000/svg', 'svg')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question