-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
317aa78
commit 39201c5
Showing
6 changed files
with
225 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#annotationLayer { | ||
display: none; | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
} | ||
body:has(#debug:checked) { | ||
#annotationLayer { | ||
display: block; | ||
} | ||
|
||
#result { | ||
--s: 100px; /* control the size */ | ||
|
||
--_g: #0000 90deg, #d8dee910 0; | ||
background: conic-gradient(from 90deg at 2px 2px, var(--_g)) 0 0 / var(--s) | ||
var(--s), | ||
conic-gradient(from 90deg at 1px 1px, var(--_g)) 0 0 / calc(var(--s) / 5) | ||
calc(var(--s) / 5); | ||
/* compensate for viewport adjustment to see the labels*/ | ||
background-position-x: 20px; | ||
background-position-y: 20px; | ||
} | ||
} | ||
.annotation { | ||
fill: none; | ||
stroke: #5e81ac; | ||
} | ||
.point { | ||
fill: #d8dee9; | ||
stroke: #bf616a; | ||
cursor: pointer; | ||
} | ||
.control-point { | ||
fill: #d8dee9; | ||
stroke: #a3be8c; | ||
cursor: pointer; | ||
} | ||
|
||
.coordinate-text { | ||
font-size: 0.8em; | ||
fill: #ebcb8b; | ||
} | ||
|
||
#originalSVG { | ||
path { | ||
stroke-width: 1px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
class Point { | ||
constructor(x, y) { | ||
this.x = Math.round(x); | ||
this.y = Math.round(y); | ||
} | ||
|
||
toString() { | ||
return `(${this.x},${this.y})`; | ||
} | ||
} | ||
|
||
class Curve { | ||
constructor(p1, c1, c2, p2,) { | ||
this.p1 = p1; | ||
this.c1 = c1; | ||
this.c2 = c2; | ||
this.p2 = p2; | ||
} | ||
annotate(index) { | ||
annotateControlLine(annotationLayer, this.p1, this.c1); | ||
annotateControlLine(annotationLayer, this.c2, this.p2); | ||
annotatePoint(annotationLayer, this.p1, index + 0, false); | ||
annotatePoint(annotationLayer, this.c1, index + 0, true); | ||
annotatePoint(annotationLayer, this.c2, index + 1, true); | ||
annotatePoint(annotationLayer, this.p2, index + 1, false); | ||
} | ||
} | ||
|
||
function annotate(originalSVG) { | ||
|
||
const annotationLayer = document.createElementNS("http://www.w3.org/2000/svg", "svg"); | ||
annotationLayer.setAttribute("id", "annotationLayer"); | ||
originalSVG.parentNode.appendChild(annotationLayer); | ||
annotationLayer.setAttribute("width", originalSVG.getAttribute("width")); | ||
annotationLayer.setAttribute("height", originalSVG.getAttribute("height")); | ||
annotationLayer.setAttribute("viewBox", originalSVG.getAttribute("viewBox")); | ||
|
||
const paths = originalSVG.getElementsByTagName("path"); | ||
|
||
for (let path of paths) { | ||
const d = path.getAttribute("d"); | ||
const commands = d.match(/[MmLlHhVvCcSsQqTtAaZz][^MmLlHhVvCcSsQqTtAaZz]*/g); | ||
|
||
let pointIndex = 0; | ||
let lastPoint = null; | ||
commands.forEach((cmd) => { | ||
const type = cmd[0]; | ||
const points = parsePoints(cmd.slice(1).trim()); | ||
|
||
if (type === "M") { | ||
// annotatePoint(annotationLayer, points[0], pointIndex++); | ||
} | ||
if (type === "L") { | ||
annotatePoint(annotationLayer, points[0], pointIndex++); | ||
} | ||
if (type === "C") { | ||
const p1 = lastPoint; | ||
const c1 = points[0]; | ||
const c2 = points[1]; | ||
const p2 = points[2]; | ||
const c = new Curve(p1, c1, c2, p2); | ||
c.annotate(pointIndex++); | ||
} | ||
lastPoint = points[points.length - 1]; | ||
}); | ||
} | ||
} | ||
|
||
function parsePoints(pointString) { | ||
const coordinates = pointString.split(/[\s,]+/).map(Number); | ||
const points = []; | ||
for (let i = 0; i < coordinates.length; i += 2) { | ||
points.push(new Point(coordinates[i], coordinates[i + 1])); | ||
} | ||
return points; | ||
} | ||
|
||
function annotatePoint(svg, point, index, isControlPoint = false) { | ||
const circle = document.createElementNS( | ||
"http://www.w3.org/2000/svg", | ||
"circle" | ||
); | ||
circle.setAttribute("cx", point.x); | ||
circle.setAttribute("cy", point.y); | ||
circle.setAttribute("r", "3"); | ||
|
||
circle.classList.add(isControlPoint ? "control-point" : "point"); | ||
const titleElement = document.createElementNS("http://www.w3.org/2000/svg", "title"); | ||
titleElement.textContent = point.toString(); | ||
circle.appendChild(titleElement); | ||
svg.appendChild(circle); | ||
if (!isControlPoint) { | ||
const text = document.createElementNS("http://www.w3.org/2000/svg", "text"); | ||
text.setAttribute("x", point.x + 5); | ||
text.setAttribute("y", point.y - 5); | ||
text.textContent = `${index}`; | ||
text.classList.add("coordinate-text"); | ||
svg.appendChild(text); | ||
} | ||
} | ||
|
||
function annotateControlLine(svg, start, end) { | ||
const line = document.createElementNS("http://www.w3.org/2000/svg", "line"); | ||
line.setAttribute("x1", start.x); | ||
line.setAttribute("y1", start.y); | ||
line.setAttribute("x2", end.x); | ||
line.setAttribute("y2", end.y); | ||
line.classList.add("annotation"); | ||
svg.appendChild(line); | ||
} | ||
|
||
|
||
export { annotate }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters