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

SVGGeometryElement.prototype.{isPointInFill,isPointInStroke} #35746

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
34 changes: 20 additions & 14 deletions files/en-us/web/api/svggeometryelement/ispointinfill/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ isPointInFill(point)
### Parameters

- `point`
- : A DOMPointInit object interpreted as a point in the local coordinate system
- : A {{domxref("DOMPointInit")}} object interpreted as a point in the local coordinate system
of the element.

### Return value
Expand All @@ -45,8 +45,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,21 +57,21 @@ 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) {
// Fallback for browsers that don't support DOMPoint as an argument
// Fallback for browsers that don't support DOMPointInit as an argument
const pointObj = svg.createSVGPoint();
pointObj.x = point[0];
pointObj.y = point[1];
Expand All @@ -84,10 +84,16 @@ 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%)";
pointEl.cx.baseVal.value = point[0];
pointEl.cy.baseVal.value = point[1];
pointEl.r.baseVal.value = 5;
if (isPointInFill) {
pointEl.setAttribute("fill", "rgb(0 170 0 / 50%)");
pointEl.setAttribute("stroke", "rgb(0 170 0)");
} else {
pointEl.setAttribute("fill", "rgb(170 0 0 / 50%)");
pointEl.setAttribute("stroke", "rgb(170 0 0)");
}
svg.appendChild(pointEl);
}
```
Expand Down
36 changes: 21 additions & 15 deletions files/en-us/web/api/svggeometryelement/ispointinstroke/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ isPointInStroke(point)
### Parameters

- `point`
- : An object interpreted as a point in the local coordinate system
- : A {{domxref("DOMPointInit")}} object interpreted as a point in the local coordinate system
of the element.

### Return value
Expand All @@ -46,8 +46,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,21 +58,21 @@ 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);
const pointObj = { x: point[0], y: point[1] };
isPointInStroke = circle.isPointInStroke(pointObj);
} catch (e) {
// Fallback for browsers that don't support DOMPoint as an argument
// Fallback for browsers that don't support DOMPointInit as an argument
const pointObj = svg.createSVGPoint();
pointObj.x = point[0];
pointObj.y = point[1];
Expand All @@ -85,10 +85,16 @@ 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%)";
pointEl.cx.baseVal.value = point[0];
pointEl.cy.baseVal.value = point[1];
pointEl.r.baseVal.value = 5;
if (isPointInStroke) {
pointEl.setAttribute("fill", "rgb(0 170 0 / 50%)");
pointEl.setAttribute("stroke", "rgb(0 170 0)");
} else {
pointEl.setAttribute("fill", "rgb(170 0 0 / 50%)");
pointEl.setAttribute("stroke", "rgb(170 0 0)");
}
svg.appendChild(pointEl);
}
```
Expand Down
Loading