简体   繁体   English

使用SVGMatrix记录HTML画布2D转换

[英]Documentation of HTML canvas 2D transformations using SVGMatrix

I'm looking at using an HTML canvas element for a simple game, but have had trouble finding complete documentation of certain features regarding the 2D drawing context. 我正在考虑使用HTML canvas元素进行简单的游戏,但是很难找到有关2D绘图上下文的某些功能的完整文档。 Usually I would look at MDN , but there are still things missing from there. 通常我会看MDN ,但仍然缺少一些东西。

In particular, even when I looked at the HTML Canvas 2D Context W3C Candidate Recommendation (which I understand to be the official word on the subject), I can't seem to find out more about using the SVGMatrix object for transformations. 特别是,即使我查看HTML Canvas 2D Context W3C Candidate Recommendation (我理解为该主题的官方词),我似乎也无法找到更多关于使用SVGMatrix对象进行转换的信息。

Both links I've given describe transformation functions on the CanvasRenderingContext2D object that take the individual elements of a matrix as arguments. 我给出的两个链接都描述了CanvasRenderingContext2D对象上的转换函数, CanvasRenderingContext2D矩阵的各个元素作为参数。 For example: 例如:

context.transform(a, b, c, d, e, f)

However, the links also have these notes: 但是,链接也有这些注释:

The methods listed below remain for historical and compatibility reasons as SVGMatrix objects are used in most parts of the API nowadays and will be used in the future instead. 下面列出的方法仍然是出于历史和兼容性原因,因为SVGMatrix对象现在在API的大多数部分中使用,并将在未来使用。

Most of the API uses SVGMatrix objects rather than this API. 大多数API使用SVGMatrix对象而不是此API。 This API remains mostly for historical reasons 此API主要是出于历史原因

Despite the pages saying that most of the API now uses SVGMatrix objects instead of specifying individual matrix elements, I can't seem to find documentation for this. 尽管页面上说大多数API现在都使用SVGMatrix对象而不是指定单个矩阵元素,但我似乎无法找到相关文档。

Can someone point me to some official documentation, or otherwise describe the API that makes use of SVGMatrix objects instead of specifying individual matrix elements? 有人可以指向我一些官方文档,或以其他方式描述使用SVGMatrix对象而不是指定单个矩阵元素的API吗?

This answer may have more the form of a blurb than actual solid information, but given the circumstances I hope it still can give some input. 这个答案可能比实际的固体信息更具有模糊的形式,但考虑到情况,我希望它仍然可以提供一些输入。

There is not much documentation of its usage - there are still (I believe) discussion about exposing as well as how to implement it (with canvas/paths) which is why we don´t see much of it yet. 关于它的用法没有太多文档 - 仍然(我相信)关于公开以及如何实现它(使用canvas / paths)的讨论,这就是我们还没有看到它的原因。 Canvas has (or had?) a method currentTransform which would return a SVGMatrix object, but it is not widely supported, or is hidden behind experimental flags (ie. in Chrome. disclaimer: may have changed recently). Canvas有(或者有?)一个方法currentTransform ,它会返回一个SVGMatrix对象,但它没有被广泛支持,或被隐藏在实验标志后面(即在Chrome中。免责声明:最近可能已经改变)。

You can read one thread of the discussion here . 你可以在这里阅读讨论的一个主题。

Its use is also mentioned here (MDN) but not documented further. 它的用途也在这里提到(MDN),但没有进一步记录。

I would assume in the case of canvas, that in the future you could pass in a retrieved and modified object to f.ex. 我会假设在canvas的情况下,将来你可以将检索和修改的对象传递给f.ex. setTransform() as an "overloaded" option to the existing signature (much like the Path2D object can be to stroke and fill), although there is no hint of this in the recommendation (see how it's used with SVG below for a likely usage pattern). setTransform()作为现有签名的“重载”选项(很像Path2D对象可以是笔划和填充),尽管建议中没有这一点(请参阅下面的SVG如何使用它的可能的使用模式) )。

The object does the same as setTransform()/transform() though, you would initialize it with values for ae through the use the same methods (as the properties themselves are read-only) as with the context, so there is no difference in principle (looking at the source code for the open source browser will show exactly how it's being used). 该对象与setTransform()/transform()的作用相同,但是,您可以使用相同的方法(因为属性本​​身是只读的)与上下文一样使用ae的值初始化它,因此没有区别原则(查看开源浏览器的源代码将准确显示它是如何使用的)。

See also this article (more for SVG and how to use the object). 另请参阅本文 (有关SVG的更多信息以及如何使用该对象)。

To play around with the current implementation of the object itself in f.ex. 在f.ex中讨论对象本身的当前实现。 console, you can do: 控制台,你可以这样做:

var matrix = document.createElementNS('http://www.w3.org/2000/svg','svg')
             .createSVGMatrix();

PS: Also feel free to check out my own matrix implementation (MIT) . PS:也可以随意查看我自己的矩阵实现(MIT)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM