简体   繁体   English

网页上的3D透视文字

[英]3D perspective text on a webpage

How can I achieve 3D text transformations in perspective using Javascript/CSS. 如何使用Javascript / CSS在透视图中实现3D文本转换。

Solutions using external libraries of Javascript/CSS are also possible 也可以使用外部Javascript / CSS库进行解决

CSS transformations with perspective are only possible with Safari at this point. 目前,只有使用Safari才能进行带有透视图的CSS转换。 Alas Chrome, while based on Webkit, and seemingly supporting the proper CSS attributes, will not apply perspective transformations. 虽然基于Webkit的Alas Chrome似乎支持适当的CSS属性,但不会应用透视图转换。 They will be supported at some point on Firefox, no clue about IE. 在Firefox上的某些时候将支持它们,而对IE则毫无头绪。

Your only other option really is <canvas> . 您唯一的其他选择实际上是<canvas> However, just like with CSS transforms, the canvas API only provides functions for "2D" affine transformations (scaling, rotation, skewing). 但是,就像CSS转换一样,canvas API仅提供用于“ 2D”仿射转换(缩放,旋转,倾斜)的功能。 With this the best you can get is an isometric perspective, as that can be achieved with just skewing. 借助此功能,您可以获得的最佳效果就是等轴测透视图,因为只需倾斜即可实现。

However, since canvas gives you pixel level control over the image, you can fake perspective, though doing so is complex. 但是,由于canvas使您可以控制图像的像素级别,因此可以伪造透视图,尽管这样做很复杂。 The obvious way is to use the putImageData function and calculate each pixel using a 3D perspective transform matrix. 最明显的方法是使用putImageData函数并使用3D透视变换矩阵计算每个像素。 Obviously you'd need to know some things about linear algebra and trigonometry. 显然,您需要了解有关线性代数和三角学的一些知识。 Regardless of your math skills, doing 3d transforms at such a low level is extremely costly performance wise, and highly variable between different browsers (Chrome is the fastest by far, Firefox will chug along at fairly low framerates, and Safari is somewhere in the middle). 不管您的数学技能如何,在如此低的水平上进行3d转换都是非常昂贵的性能,并且在不同的浏览器之间变化很大(Chrome是迄今为止最快的,Firefox的帧速会很低,而Safari却处于中等水平) )。

A better solution performance wise, but similarly complex and math intensive, is to use drawImage to paint an image/text/whatever to canvas one line at a time, and in between each change the canvas's scaling transform values. 更好的解决方案性能明智的方法是使用drawImage来绘制图像/文本/任何内容,以一次绘制一条线,并且在每次更改之间都更改画布的缩放变换值,但这是复杂且数学密集的。 This is exactly the method that was used to get perspective on the SNES with mode 7 that natively only supported 2D transformations. 这正是在本机仅支持2D转换的模式7下获得SNES透视图的方法。

Another method is detailed here. 这里详细介绍了另一种方法。

As you can imagine none of this is trivial, and performance is spotty at best. 可以想象,这都不是琐碎的事,而性能充其量也是充其量。 So if you are not willing to delve into a mass of linear algebra, trigonometry and canvas API documentation, I would say you are pretty much out of luck. 因此,如果您不愿意研究大量线性代数,三角函数和canvas API文档,那么我会说您很不走运。 Any JS library that does as you ask is subject to all these limitations. 您要求的任何JS库都受所有这些限制。 I know of a handful of demos out there, but none that could really be called a library (though if someone knows of something I'd be happy to be corrected). 我知道那里有一些演示,但是没有一个真正可以称为库(尽管有人知道我很乐意予以纠正)。

If anyone is interested in the nutty details of any of the approaches I mentioned, I'd be happy to try to lay them out more comprehensively. 如果有人对我提到的任何方法的详细细节感兴趣,我很乐意尝试更全面地进行布局。 In the mean time, you can play around with my own demo that uses a combination of the first two techniques I specified. 同时,您可以玩我自己的演示,该演示结合了我指定的前两种技术。

http://bigmooworld.com/pwings/pilotwings/pilotwings.html http://bigmooworld.com/pwings/pilotwings/pilotwings.html

Some of you might recognize it... 你们中的有些人可能会意识到...

Use WASD to pan, up/down to zoom, right/left to rotate, and Q/E to change perspective. 使用WASD进行平移,使用上/下进行缩放,使用右/左进行旋转,并使用Q / E更改视角。 Feel free to peruse the code, but be forewarned that it is not well organized or commented, and most of it is discarded junk code. 可以随意阅读代码,但是请注意,它没有很好的组织或注释,并且大多数都是废弃的垃圾代码。

So anyway my answer is...Yes it is possible, easily in Safari, or with a great deal of effort and shoddy performance in other browsers (and there is probably a way in IE but I have no clue about how). 因此,无论如何,我的答案是……是的,可以在Safari中轻松实现,或者在其他浏览器中进行大量努力和劣质性能(IE中可能有一种方法,但我不知道如何实现)。

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

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