6

having a SVG inline an HTML document , with a viewbox "x y w h". How can I check if an element is inside the view box?

by saying element it could be a nested child path inside a group and so on ...

so I think I'm looking for a build in function of SVG ,or a quick fast why to check it.

I've tried saving the viewbox cords ,and the element I want to check if it's inside the viewbox I did something like :

while (parent ! the svg element ...)
{
   parent = get elem parent 
} 

// now I have the most top parent of this element ,inside the svg doc now check if parent inside viewbox , if true then child also , otherwise false

Haddar Macdasi
  • 3,477
  • 8
  • 37
  • 59

1 Answers1

0

maybe you can try the checkEnclosure method of SVGSVGElement.

cuixiping
  • 24,167
  • 8
  • 82
  • 93
  • Could you give us an example on how to do it? – Norfeldt Jan 04 '20 at 23:13
  • @Norfeldt Do you mean [contains](https://developer.mozilla.org/en-US/docs/Web/API/Node/contains) ? – cuixiping Jan 12 '20 at 02:53
  • I meant a code example on how to use checkEnclosure - I can't figure out how to do it. I'm animating some text in and out of the viewport and would like to test if it is inside of it. – Norfeldt Jan 12 '20 at 10:55