rect-hittest-001.html (1299B)
1 <!DOCTYPE html> 2 <title>elementFromPoint(...) on <rect>s</title> 3 <link rel="help" href="https://svgwg.org/svg2-draft/interact.html#hit-testing"> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <svg id="svg" width="420" height="300"> 7 <rect id="border" x="0.5" y="0.5" width="419" height="299" stroke="#000" stroke-width="1" fill="none"/> 8 9 <rect id="rect1" x="40" y="30" width="20" height="20"/> 10 </svg> 11 <script> 12 // Points are relative to the client rect of the <svg> root. 13 const tests = [ 14 // Points on the edge. 15 { x: 50, y: 30, expectedElemId: "rect1" }, 16 { x: 60, y: 40, expectedElemId: "rect1" }, 17 { x: 50, y: 50, expectedElemId: "rect1" }, 18 { x: 40, y: 40, expectedElemId: "rect1" }, 19 ]; 20 21 setup(() => { 22 const svg = document.getElementById("svg"); 23 const svgBounds = svg.getBoundingClientRect(); 24 window.svgOrigin = { 25 x: svgBounds.left << 0, 26 y: svgBounds.top << 0, 27 }; 28 }); 29 30 tests.forEach(testcase => { 31 test(t => { 32 const expectedElem = document.getElementById(testcase.expectedElemId); 33 const hitElem = document.elementFromPoint(svgOrigin.x + testcase.x, svgOrigin.y + testcase.y); 34 assert_equals(hitElem, expectedElem); 35 }, `${document.title}, element at (${testcase.x}, ${testcase.y})`); 36 }); 37 </script>