Squircles

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Squircles

Squircles A squircle is a shape intermediate between a square and a circle. There is no single mathematical definition, rather there are a number of curves that fit this shape. All general squircles are parametrized, and can vary between circle and square. Loading the interactive tester…

“Squircles”(方圆)是一种介于正方形和圆形之间的形状。它没有单一的数学定义,而是有多种符合该形状的曲线。所有通用的方圆都是参数化的,可以在圆形和正方形之间变化。正在加载交互式测试器……

Superellipse The best known squircle is the superellipse, popularized by Piet Hein. Martin Gardner wrote a detailed narrative of its origin in a Scientific American column in 1977. The text is replicated in https://piethein.com/superellipse/ but that’s missing equations and figures. The superellipse has the formula $|x|^n + |y|^n = 1$ (we’ll use a unit radius for convenience). When $n=2$ it is a circle, and it approaches a square asymptotically as $n \rightarrow \infty$.

超椭圆(Superellipse)最著名的方圆是超椭圆,由皮特·海恩(Piet Hein)推广。马丁·加德纳(Martin Gardner)在 1977 年的《科学美国人》专栏中详细叙述了它的起源。该文本在 https://piethein.com/superellipse/ 有所转载,但缺少公式和图表。超椭圆的公式为 $|x|^n + |y|^n = 1$(为方便起见,我们使用单位半径)。当 $n=2$ 时,它是一个圆;随着 $n \rightarrow \infty$,它渐近趋向于一个正方形。

Measurement There is no standard measurement for squircles. For superellipses, the exponent is the most common parameter. A good choice is the coordinates of the midpoint of the quadrant, assuming a unit radius; here $x$ and $y$ are equal. The conversion formula is simple and intuitive: $x = 0.5^{1/n}$. This measurement is called “gauge” in the interactive demo above, and is the primary parameter; with the exception of some unreachable regions of parameter space, it should be possible to compare different squircle variants with similar gauge.

测量:方圆没有标准的测量方法。对于超椭圆,指数是最常见的参数。一个好的选择是假设单位半径时象限中点的坐标;此时 $x$ 和 $y$ 相等。转换公式简单直观:$x = 0.5^{1/n}$。这种测量方法在上面的交互式演示中被称为“量规”(gauge),是主要参数;除了参数空间中一些无法到达的区域外,应该可以比较具有相似量规的不同方圆变体。

Flat-sided squircles There are two basic approaches to squircle creation. One is a single analytic curve, where curvature reaches zero at the poles, but is otherwise nonzero. The other is mixing straight line segments with corners. A significant advantage of the latter approach is that it can adapt to rectangles of arbitrary aspect ratio without distorting the corner shape.

平边方圆:创建方圆有两种基本方法。一种是单一的解析曲线,其曲率在极点处达到零,但在其他地方不为零。另一种是将直线段与圆角混合。后一种方法的一个显著优点是,它可以适应任意长宽比的矩形,而不会使圆角形状变形。

The Apple squircle shape Squircles received renewed attention when Apple changed the icon shape from rounded rectangle to their own squircle in iOS 7 in 2013. There were several blog posts to analyze and recreate the shape. An early analysis suggested that it was a superellipse of exponent 5, but when people extracted the Béziers and looked more closely, that was found to be inaccurate. Rather, it’s a flat-sided squircle. For the raw Bézier path data of the Apple shape, the best source is the PaintCode blog. This blog correctly points out some bugs in that logic, and also has some details on behavior for oval rather than square aspect ratios. The Apple shape has a number of flaws, including one extraneous straight line segment, and a lack of symmetry. However, those flaws are minor, and really only reveal themselves under close analysis. The Apple shape has no additional parameter, so its variation is controlled entirely by the length of the flat side. With no flat side, it has a gauge fixed at 0.809. An extremely detailed analysis of the Apple shape is in The Art of Continuous Corners.

苹果方圆形状:2013 年,当苹果在 iOS 7 中将图标形状从圆角矩形更改为他们自己的方圆时,方圆再次受到关注。有几篇博客文章对其形状进行了分析和重现。早期的分析认为它是一个指数为 5 的超椭圆,但当人们提取贝塞尔曲线并仔细观察时,发现这种说法是不准确的。相反,它是一个平边方圆。对于苹果形状的原始贝塞尔路径数据,最好的来源是 PaintCode 博客。该博客正确地指出了该逻辑中的一些错误,并提供了一些关于非正方形长宽比(即椭圆形)下表现的细节。苹果形状有一些缺陷,包括一个多余的直线段和缺乏对称性。然而,这些缺陷很小,只有在仔细分析时才会显现出来。苹果形状没有额外的参数,因此其变化完全由平边的长度控制。在没有平边的情况下,其量规固定为 0.809。关于苹果形状的极其详细的分析可以在《连续圆角的艺术》(The Art of Continuous Corners)中找到。

The Figma squircle Figma published a blog post, Desperately Seeking Squircles, with an analysis of the Apple squircle and their own approximation. It is influential because Figma is an important design tool, because their writeup was compelling, and because there are any number of open source implementations of it, mostly TypeScript/JavaScript. squircle-path-kit from msurguy figma-squircle from phamfoo corner-smoothing from sanalabs (uses figma-squircle) squircle-js from bring-shrubbery (uses figma-squircle) figma_squircle from aloisdeniel (Dart/Flutter) Lisse from JaceThings The Figma blog contains a plot of the Béziers comprising a cleaned up version of iOS 7 rounded rectangle, revealing three Bézier segments per quadrant. It fixes the straight-line segment and the asymmetry, so is not an exact match. The middle segment is very close to an arc. The other segments are more problematic. They have zero curvature at the endpoints, so are G2 continuous with flat sides, but there is a curvature discontinuity with the middle (arc) section, and their curvature profile is not especially smooth. A reasonable guess is that it was drawn by hand to be approximately smooth. Without a flat side, the Figma squircle is only capable of a gauge between $\sqrt{0.5}$ (0.707) and 0.854. Gauges up to 1 are of course attainable by attainable by adding the flat side.

Figma 方圆:Figma 发表了一篇名为《拼命寻找方圆》(Desperately Seeking Squircles)的博客文章,分析了苹果方圆和他们自己的近似方案。它之所以具有影响力,是因为 Figma 是一个重要的设计工具,因为他们的文章很有说服力,而且有许多开源实现,主要是 TypeScript/JavaScript。包括 msurguy 的 squircle-path-kit、phamfoo 的 figma-squircle、sanalabs 的 corner-smoothing(使用 figma-squircle)、bring-shrubbery 的 squircle-js(使用 figma-squircle)、aloisdeniel 的 figma_squircle(Dart/Flutter)以及 JaceThings 的 Lisse。Figma 博客包含了一张由 iOS 7 圆角矩形的清理版本组成的贝塞尔曲线图,显示每个象限有三个贝塞尔段。它修复了直线段和不对称性,因此不是完全匹配。中间段非常接近圆弧。其他段则更有问题。它们在端点处的曲率为零,因此与平边保持 G2 连续,但与中间(圆弧)部分存在曲率不连续,且其曲率轮廓不是特别平滑。一个合理的猜测是,它是手工绘制以达到近似平滑的。在没有平边的情况下,Figma 方圆的量规只能在 $\sqrt{0.5}$ (0.707) 和 0.854 之间。当然,通过增加平边可以达到高达 1 的量规。

Clothoid squircles The Figma blog suggests “smoothed curvature profiles” which have a piecewise linear relationship between arc length and curvature. It then goes on to approximate them with cubic Bézier segments, but their approximation has fairly significant curvature discontinuities when joining to the circular arc. The clothoid squircle is worth describing explicitly, as it has G2 continuity (as opposed to G1 for the Figma approximation). The behavior is generally similar to the Figma variant. Without a flat side, it is only capable of a gauge between $\sqrt{0.5}$ (0.707) and 0.790.

回旋曲线方圆:Figma 博客建议使用“平滑曲率轮廓”,其弧长和曲率之间具有分段线性关系。随后,它继续用三次贝塞尔段对其进行近似,但他们的近似在连接到圆弧时存在相当显著的曲率不连续性。回旋曲线方圆值得明确描述,因为它具有 G2 连续性(相对于 Figma 近似方案的 G1)。其表现通常与 Figma 变体相似。在没有平边的情况下,其量规只能在 $\sqrt{0.5}$ (0.707) 和 0.790 之间。

The box decorations corner-shape spec Squircles got a big boost as they’re now standardized in CSS, as the corner-shape property of the box decorations spec. These specify real superellipse corners, with additional tweaks and support for animation.

盒模型装饰 corner-shape 规范:方圆得到了极大的推动,因为它们现在已在 CSS 中标准化,作为盒模型装饰规范的 corner-shape 属性。这些规范指定了真正的超椭圆圆角,并带有额外的调整和对动画的支持。

The Chromium superellipse approximation While the CSS spec mandates the actual superellipse shape, practical implementations will generally use a Bézier approximation. A blog post, The corner cases of implementing CSS corner-shape in Blink, gives an efficient closed-form approximation, with two Bézier segments per quadrant. This formula, determined using symbolic regression, is parametrized, and handles exponents 2 and above well. It is exact for placing the midpoint (this is part of the formula), so is well calibrated in that regard.

Chromium 超椭圆近似:虽然 CSS 规范强制要求使用实际的超椭圆形状,但实际实现通常会使用贝塞尔近似。一篇名为《在 Blink 中实现 CSS corner-shape 的边界情况》的博客文章给出了一个高效的闭式近似,每个象限有两个贝塞尔段。这个通过符号回归确定的公式是参数化的,并且能很好地处理 2 及以上的指数。它在放置中点方面是精确的(这是公式的一部分),因此在这方面校准得很好。

Apple-like behavior with the corner-shape spec The early analysis of the Apple squircle shape as being approximately an exponent 5 superellipse was based on coarse shape only and didn’t take into account its flat side. A much better match to the Apple shape is attainable using a superellipse with exponent 3 and a corresponding flat side length to match the gauge. This has G2 continuity and a similar curvature profile. This illustration was made using the Chromium

使用 corner-shape 规范实现类似苹果的行为:早期将苹果方圆形状分析为近似指数为 5 的超椭圆,仅基于粗略形状,并未考虑其平边。使用指数为 3 的超椭圆并配合相应的平边长度来匹配量规,可以获得与苹果形状更好的匹配。这具有 G2 连续性和相似的曲率轮廓。此插图是使用 Chromium 制作的。