Skip to content
Advertisement

Is there a way to translate a nested element with transform=”translate(mouseX,mouseY)” in JS?

Edit: Code:

JavaScript
JavaScript
JavaScript

note: similar but not a duplicate of Creating a new SVGTransform object to append to SVGTransformList because I can’t comment xD

and I want to move an SVG in an SVG to my mouse position. The problem is, the #snap-icon-x-container element appears to have no viewportElement. In Creating a new SVGTransform object to append to SVGTransformList, it has a comment from RBarryYoung saying that I should use the svg.viewportElement. What does svg refer to (I have tried the element) and (not that relevant) why does viewportElement not show up in VSCode ctrlspace menu?

Advertisement

Answer

Since after editing your question changed I’m adding a new answer. I hope this is what you need:

To the svg I’m adding 9 rectangles, thecells you intend to click. For every rectangle I am adding an event listener: when clicked the cross appear in the clicked rectangle. In order to know the position I’m using the value ofthe x and y attributes of the rectangle + 25

Please observe that the nested svg element (#cross) has a viewBox a x a y a width and a height attributes. Alsoinstead of translating the svg element I’m changing the x and y attributes.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement