I am creating a rect element based on input values from textbox(getting Height,Width,X and Y from textbox).
When rect is created and appended in parent g element. I am creating a text element showing the newly created rect number.I positioned this text to top left corner of rect.
This is my piece of code(not entire only relevant to question).It is called on onclick event of button.
//Creating <rect>
Rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
Rect.setAttributeNS(null, "id", "rectId");
Rect.setAttributeNS(null, "x", x); //getting x from textbox
Rect.setAttributeNS(null, "width", width); //getting width from
textbox
Rect.setAttributeNS(null, "y", y); //getting y from textbox
Rect.setAttributeNS(null, "height", height); //getting height from
textbox
Rect.setAttributeNS(null, "fill", "none");
grp.appendChild(Rect);
//Creating <text> for Rect Num
RectNo = document.createElementNS('http://www.w3.org/2000/svg', 'text');
RectNo.setAttributeNS(null, "id", "textNo");
numTxt = document.createTextNode("RectNum_"+(any number));
RectNo.appendChild(numTxt);
grp.appendChild(RectNo);
scale = (width - (width-13)) / width; //(width of rect)
posX = x+1; //(x of rect)
posY = y+3; //(y of rect)
RectNo.setAttribute("transform", "translate(" + posX + "," + posY + ")
scale(" + scale + ")");
Created rect can be of any size,so for scaling the text element, I tried above code of transform. But somehow it is not working.What I need is that no matter how big or small the rect size is, the RectNo text should appear in the top left-corner of rect and should get adjusted(resize height and width) according to the rect size.