Ran*_*man 5 javascript svg html5-canvas path-2d
我想检查 Path2D 对象的各个路径段。例如,假设
path = new Path2D();
path.ellipse(70,90,2,2,0,0,2*Math.PI);
Run Code Online (Sandbox Code Playgroud)
我想传递path给其他函数并提取 的元素,Path2D以便它们可以转换为另一种格式。总体目标是允许用户创建路径并将其转换为 TikZ 以包含在 LaTeX 文档中。
是否有像 Java 那样的东西Shape.getPathIterator(),它允许人们检查路径上的每个段。JavaScript 可以做到这一点吗?作为模板,这里是 Java 代码的概要,它可以完成我希望在 Javascript 中完成的任务。
PathIterator p = path.getPathIterator(new AffineTransform());
while (p.isDone() == false)
{
double c = new double[6];
int t = p.currentSegment(c);
if (t == PathIterator.SEG_MOVETO)
// (c[0],c[1]) is the "move-to" point.
else if (t == PathIterator.SEG_LINETO)
// (c[0],c[1]) is the "line-to" point.
else if (t == == PathIterator.SEG_CUBICTO)
// c[0] through c[5] specify the cubic curve
else
// etc., etc.
}
Run Code Online (Sandbox Code Playgroud)
看到@Kaiido 的回答后编辑此内容。
我最终所做的就是扩展Path2D到一个功能相同的新类,除了它在调用时将每次调用 、 等存储arc()到lineTo()一个数组中。这使我能够检查过去的通话记录。对于每个人来说,这可能不是一个足够通用的解决方案,但它可以满足我的需求。
不,目前 API 中没有任何内容允许我们这样做。
有一些关于扩展 Path2D API 的讨论,以使其不那么“不透明”,但还没有任何具体内容,我认为没有人在积极致力于它,而且我们无法确定它将包括什么。
目前的提案如下:
Path2D 检测。允许检查当前不透明的 Path2D 对象。
不管怎样,我自己几个月前就开始研究这个想法,希望我能帮助这个讨论以某种可能的功能设计开始。
我非常雄心勃勃的想法是引入像 SVG 这样的 APISVGPathData并向 Path2D 添加方法,例如getBBox、getPathData()、setPathData()、toSVGString()、getTotalLength()或getPointAtLength()。
您可以检查我的项目所在的存储库,下面是使用您的输入的演示。
const path = new Path2D();
path.ellipse(70,90,50,20,0,0,2*Math.PI);
const svgString = path.toSVGString();
document.querySelector("pre").textContent += svgString;
document.querySelector("path").setAttribute("d", svgString);
document.querySelector("canvas").getContext("2d").fill(path);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/gh/Kaiido/path2D-inspection@master/build/path2D-inspection.min.js"></script>
<pre>
const path = new Path2D();
path.ellipse(70,90,50,20,0,0,2*Math.PI);
path.toSVGString();
</pre>
SVG:<br>
<svg><path fill="green"></path></svg><br>
Canvas:<br>
<canvas></canvas>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
793 次 |
| 最近记录: |