Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SVGGeometryElement#{isPointInFill,isPointInStroke} examples #35746

Merged
merged 2 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions files/en-us/web/api/svggeometryelement/ispointinfill/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ browser-compat: api.SVGGeometryElement.isPointInFill

{{APIRef("SVG")}}

The **`SVGGeometryElement.isPointInFill()`** method determines
whether a given point is within the fill shape of an element. Normal hit testing rules
apply; the value of the {{cssxref("pointer-events")}} property on the element determines
whether a point is considered to be within the fill. The `point` argument is
interpreted as a point in the local coordinate system of the element.
The **`isPointInFill()`** method of the {{domxref("SVGGeometryElement")}} interface determines whether a given point is within the fill shape of an element. The `point` argument is interpreted as a point in the local coordinate system of the element.

## Syntax

Expand All @@ -23,8 +19,7 @@ isPointInFill(point)
### Parameters

- `point`
- : A DOMPointInit object interpreted as a point in the local coordinate system
of the element.
- : An object representing a point interpreted in the local coordinate system of the element. It is converted to a {{domxref("DOMPoint")}} object using the same algorithm as [`DOMPoint.fromPoint()`](/en-US/docs/Web/API/DOMPoint/fromPoint_static).

### Return value

Expand All @@ -45,8 +40,8 @@ A boolean indicating whether the given point is within the fill or not.
cx="50"
cy="50"
r="45"
fill="white"
stroke="black"
fill="rgb(0 0 0 / 25%)"
stroke="rgb(0 0 0 / 50%)"
stroke-width="10" />
</svg>
```
Expand All @@ -57,20 +52,20 @@ A boolean indicating whether the given point is within the fill or not.
const svg = document.getElementsByTagName("svg")[0];
const circle = document.getElementById("circle");
const points = [
["10", "10"],
["40", "30"],
["70", "40"],
["15", "75"],
["83", "83"],
[10, 10],
[40, 30],
[70, 40],
[15, 75],
[83, 83],
];

for (const point of points) {
let isPointInFill;

try {
const pointObj = new DOMPoint(point[0], point[1]);
const pointObj = { x: point[0], y: point[1] };
isPointInFill = circle.isPointInFill(pointObj);
} catch (e) {
} catch {
// Fallback for browsers that don't support DOMPoint as an argument
const pointObj = svg.createSVGPoint();
pointObj.x = point[0];
Expand All @@ -84,17 +79,28 @@ for (const point of points) {
"http://www.w3.org/2000/svg",
"circle",
);
pointEl.style.cx = point[0];
pointEl.style.cy = point[1];
pointEl.style.r = 5;
pointEl.style.fill = isPointInFill ? "seagreen" : "rgb(255 0 0 / 50%)";
svg.appendChild(pointEl);
pointEl.cx.baseVal.value = point[0];
pointEl.cy.baseVal.value = point[1];
pointEl.r.baseVal.value = 5;
const pathEl = document.createElementNS("http://www.w3.org/2000/svg", "path");
if (isPointInFill) {
pointEl.setAttribute("fill", "rgb(0 170 0 / 50%)");
pointEl.setAttribute("stroke", "rgb(0 170 0)");
pathEl.setAttribute("stroke", "rgb(0 170 0)");
pathEl.setAttribute("d", `M ${point[0] - 5} ${point[1]} h 10 m -5 -5 v 10`);
} else {
pointEl.setAttribute("fill", "rgb(170 0 0 / 50%)");
pointEl.setAttribute("stroke", "rgb(170 0 0)");
pathEl.setAttribute("stroke", "rgb(170 0 0)");
pathEl.setAttribute("d", `M ${point[0] - 5} ${point[1]} h 10`);
}
svg.append(pointEl, pathEl);
}
```

### Result

{{EmbedLiveSample("Examples", "150", "155")}}
{{EmbedLiveSample("Examples", "150", "150")}}

## Specifications

Expand Down
51 changes: 28 additions & 23 deletions files/en-us/web/api/svggeometryelement/ispointinstroke/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ browser-compat: api.SVGGeometryElement.isPointInStroke

{{APIRef("SVG")}}

The **`SVGGeometryElement.isPointInStroke()`** method
determines whether a given point is within the stroke shape of an element. Normal hit
testing rules apply; the value of the {{cssxref("pointer-events")}} property on the
element determines whether a point is considered to be within the stroke. The
`point` argument is interpreted as a point in the local coordinate system of
the element.
The **`isPointInStroke()`** method of the {{domxref("SVGGeometryElement")}} interface determines whether a given point is within the stroke shape of an element. The `point` argument is interpreted as a point in the local coordinate system of the element.

## Syntax

Expand All @@ -24,8 +19,7 @@ isPointInStroke(point)
### Parameters

- `point`
- : An object interpreted as a point in the local coordinate system
of the element.
- : An object representing a point interpreted in the local coordinate system of the element. It is converted to a {{domxref("DOMPoint")}} object using the same algorithm as [`DOMPoint.fromPoint()`](/en-US/docs/Web/API/DOMPoint/fromPoint_static).

### Return value

Expand All @@ -46,8 +40,8 @@ A boolean indicating whether the given point is within the stroke or not.
cx="50"
cy="50"
r="45"
fill="white"
stroke="black"
fill="rgb(0 0 0 / 25%)"
stroke="rgb(0 0 0 / 50%)"
stroke-width="10" />
</svg>
```
Expand All @@ -58,20 +52,20 @@ A boolean indicating whether the given point is within the stroke or not.
const svg = document.getElementsByTagName("svg")[0];
const circle = document.getElementById("circle");
const points = [
["10", "10"],
["40", "30"],
["70", "40"],
["15", "75"],
["83", "83"],
[10, 10],
[40, 30],
[70, 40],
[15, 75],
[83, 83],
];

for (const point of points) {
let isPointInStroke;

try {
const pointObj = new DOMPoint(point[0], point[1]);
isPointInFill = circle.isPointInStroke(pointObj);
} catch (e) {
const pointObj = { x: point[0], y: point[1] };
isPointInStroke = circle.isPointInStroke(pointObj);
} catch {
// Fallback for browsers that don't support DOMPoint as an argument
const pointObj = svg.createSVGPoint();
pointObj.x = point[0];
Expand All @@ -85,11 +79,22 @@ for (const point of points) {
"http://www.w3.org/2000/svg",
"circle",
);
pointEl.style.cx = point[0];
pointEl.style.cy = point[1];
pointEl.style.r = 5;
pointEl.style.fill = isPointInStroke ? "seagreen" : "rgb(255 0 0 / 50%)";
svg.appendChild(pointEl);
pointEl.cx.baseVal.value = point[0];
pointEl.cy.baseVal.value = point[1];
pointEl.r.baseVal.value = 5;
const pathEl = document.createElementNS("http://www.w3.org/2000/svg", "path");
if (isPointInStroke) {
pointEl.setAttribute("fill", "rgb(0 170 0 / 50%)");
pointEl.setAttribute("stroke", "rgb(0 170 0)");
pathEl.setAttribute("stroke", "rgb(0 170 0)");
pathEl.setAttribute("d", `M ${point[0] - 5} ${point[1]} h 10 m -5 -5 v 10`);
} else {
pointEl.setAttribute("fill", "rgb(170 0 0 / 50%)");
pointEl.setAttribute("stroke", "rgb(170 0 0)");
pathEl.setAttribute("stroke", "rgb(170 0 0)");
pathEl.setAttribute("d", `M ${point[0] - 5} ${point[1]} h 10`);
}
svg.append(pointEl, pathEl);
}
```

Expand Down