HN 日本語サマリー

← 一覧へ戻る
Web開発

スリーストローク問題

The Three-Stroke Problem (penpot.app)

26 pointsby elenathor0 コメント

要約

物理学における「三体問題」は、3つの天体が互いに軌道を描く際の予測不可能性とカオスな性質を説明する有名な問題です。この記事では、この概念をPenpotのデザインシステムにおける「スリーストローク問題」としてアナロジーで説明しています。これは、3種類のストローク(内側、中央、外側)が複雑に絡み合うと、デザインシステムがカオスになるという問題です。

全文翻訳

3つのストローク問題

3つの物体が互いに軌道を描いて公転する場合、初期条件のほとんどにおいて、その力学系はカオス的になる

これは、3つの天体が互いにどのように相互作用するかは予測不可能であり、不確実性の急速な増大を引き起こすことを示す、有名な物理学の問題です。ニュートン力学によれば、この3体問題は、初期条件のほとんどにおいて、その力学系がカオス的になることを示しています。

[画像: 同じタイミングで異なるストロークタイプが適用された正方形と円が、天体のようなオブジェクトを形成している様子]

これは、PenpotがWebGLレンダリングへの移行中に直面した課題、特に私が「3つのストローク問題」と名付けたものに対する素晴らしいアナロジーだと感じました。元の声明を言い換えると、この問題は次のように提起できます。

3種類のストロークが互いに絡み合った場合、初期条件のほとんどにおいて、そのデザインシステムはカオス的になる。

基本から始めましょう。ストロークとは何でしょうか?

ストローク(境界とも呼ばれる)は、レイヤーの周りを囲む線です。デザインソフトウェアでは、ストロークのプロパティを更新して、そのスタイル、サイズ、色をカスタマイズできます。([1])

この定義は正確で的確です。それは形状を囲み、境界を定める線です。この線の特徴は、必ずしも実線である必要はなく、さまざまなスタイル(破線、点線)を持つことができ、定義にはサイズや色などの他のプロパティも含まれています。
Penpotでさまざまなストロークスタイルを使用した形状の例を以下に示します。([6])

[画像: Penpotにおける、シンプルで異なるストロークの組み合わせ]

ストロークとフォーマット

CSSでは、これに相当するのは border プロパティです。その主なプロパティは次のように使用できます。border: <幅> <スタイル> <色>。border-radius プロパティは角丸の半径を定義し、形状に適用されます。各境界プロパティには、個別のCSSプロパティもあります。border-width、border-style、border-color([3])

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="./style.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Border Styles</title>
  </head>
  <body>
  <div class="box solid"></div>
  <div class="box dotted"></div>
  <div class="box dashed"></div>
  </body>
</html>
.box {
    width: 200px;
    height: 200px;
    padding: 16px;
    margin: 12px 0;
    font-family: sans-serif;
    background: #c7ff4fFF;
}

.solid {
    border: 10px solid #f76e28FF;
}

.dotted {
    border: 10px dotted #f76e28FF;
    border-radius: 50px;
}

.dashed {
    border: 10px dashed #f76e28FF;
    border-radius: 100%;
}

Penpotの元のレンダラーで使用され、WebGLレンダラーに置き換えられつつあるフォーマットであるSVGでは、これらのスタイルに相当するものは次のようになります([2])。

<svg width="240" height="700" viewBox="0 0 240 700" xmlns="http://www.w3.org/2000/svg">
  <!-- 1) Solid -->
  <rect
    x="20" y="20"
    width="200" height="200"
    fill="#c7ff4f"
    stroke="#f76e28"
    stroke-width="10"
  />

  <!-- 2) Dotted + border-radius: 50px -->
  <rect
    x="20" y="244"
    width="200" height="200"
    rx="50" ry="50"
    fill="#c7ff4f"
    stroke="#f76e28"
    stroke-width="10"
    stroke-linecap="round"
    stroke-dasharray="1 18"
  />

  <!-- 3) Dashed circle -->
  <circle
    cx="120" cy="568"
    r="95"
    fill="#c7ff4f"
    stroke="#f76e28"
    stroke-width="10"
    stroke-dasharray="20 12"
  />
</svg>
3つのストロークの登場

この定義に、もう一つ関連性の高いプロパティを追加する必要があります。それは、形状を境界取る位置に対するストロークのタイプです。そして、ここで「3つのストローク問題」に遭遇します。内側、中央、外側のストロークです([7])。

  • 内側ストローク: 形状の完全に内側に配置されます。その幅は、形状の端から内側に向かって伸びます。
  • 中央ストローク: ストロークの半分が形状の内側に、もう半分が外側に配置されます。定義された幅の半分が形状の内側に、もう半分が外側に位置します。
  • 外側ストローク: 形状の完全に外側に配置されます。その幅は、形状の端から外側に向かって伸びます。

[画像: 内側、外側、中央のストロークの異なる組み合わせと、ソリッドな塗りつぶしカラーを適用したPenpotの形状]

ここでパーティーが始まります。なぜなら、私たちを一つにするパスが分岐し始めるからです。

まず、CSSでは、デフォルトの境界線は内側タイプです。このため、外側または中央の境界線が必要な場合は、outline および outline-offset プロパティを使用して、境界線を外側または内側に「押し出す」(負の値を使用する)必要があります。outline は border に相当しますが、外側タイプのストロークです。

「中央」の境界線の場合、使用した幅の半分だけ境界線を内側に移動させる必要があります。

.outer {
  outline: 10px solid #f76e28FF;
}

.center {
  outline: 10px solid #f76e28FF;
  outline-offset: -5px;
}

一方、CSSでは中央揃えのストロークを取得するために工夫が必要ですが、SVGでは中央揃えのストロークがデフォルトです。内側と外側のストロークを取得するには、クリップとマスクを適用する必要があります。

<svg width="520" height="170" viewBox="0 0 520 170" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <clipPath id="clipRect">
      <rect x="20" y="20" width="120" height="120" rx="20"/>
    </clipPath>

    <mask id="outerOnly">
      <rect width="100%" height="100%" fill="white"/>
      <!-- 元の形状のオン/インサイドのすべてを削除 -->
      <rect x="380" y="20" width="120" height="120" rx="20" fill="black"/>
    </mask>
  </defs>

  <!-- CENTER (default) -->
  <rect x="200" y="20" width="120" height="120" rx="20"
        fill="#c7ff4f" stroke="#f76e28" stroke-width="12"/>

  <!-- INNER (clipped centered stroke) -->
  <g clip-path="url(#clipRect)">
    <rect x="20" y="20" width="120" height="120" rx="20"
          fill="#c7ff4f" stroke="#f76e28" stroke-width="12"/>
  </g>

  <!-- OUTER (masked centered stroke) -->
  <rect x="380" y="20" width="120" height="120" rx="20"
        fill="#c7ff4f"/>
  <rect x="380" y="20" width="120" height="120" rx="20"
        fill="none" stroke="#f76e28" stroke-width="12" mask="url(#outerOnly)"/>
</svg>
WebGLレンダラーでの実装方法

では、WebGLレンダラーではどうしているのでしょうか?この場合、Penpotでは広く知られているSkia 2Dレンダリングライブラリを使用することにしました。高レベルでは、SkiaはSkPaintクラス([4])を提供しており、形状の描画方法を定義できます。PaintのタイプはFill、Stroke、またはStrokeFillにすることができます。Penpotで行っていることについては、最終的にさまざまなPaintとそのストロークメソッドの組み合わせを使用します([5])。Skiaでの実装方法は次のとおりです。

[画像: 内側、外側、中央のストロークを組み合わせ、さまざまな塗りつぶしタイプを使用したPenpotでのさまざまな種類のストローク]

// Inner stroke: Skia strokes are always centered on the geometry,
// so we inset the rect by width/2. The stroke then spans from the
// shape edge inward -> fully inside the shape.
SkPaint stroke;
stroke.setAntiAlias(true);
stroke.setStyle(SkPaint::kStroke_Style);
stroke.setStrokeWidth(strokeWidth);
stroke.setColor(0xFFF76E28);

const SkRect innerRect = rect.makeInset(strokeWidth / 2, strokeWidth / 2);
canvas->drawRect(innerRect, stroke);

[画像: Skiaでの内側ストローク]

// Center stroke: this is Skia's native behavior. The stroke
// straddles the boundary, half inside, half outside. We draw
// directly on the shape rect, no offset needed.
SkPaint stroke;
stroke.setAntiAlias(true);
stroke.setStyle(SkPaint::kStroke_Style);
stroke.setStrokeWidth(strokeWidth);
stroke.setColor(0xFF14CECA);

canvas->drawRect(rect, stroke);

[画像: Skiaでの中央ストローク]

// Outer stroke: outset the rect by width/2 so the centered stroke
// spans from the shape edge outward -> fully outside the shape.
SkPaint stroke;
stroke.setAntiAlias(true);
stroke.setStyle(SkPaint::kStroke_Style);
stroke.setStrokeWidth(strokeWidth);
stroke.setColor(0xFFFC8CFF);

const SkRect outerRect = rect.makeOutset(strokeWidth / 2, strokeWidth / 2);
canvas->drawRect(outerRect, stroke);

[画像: Skiaでの外側ストローク]

パス:すべてが正方形ではない

ここまでは単純、あるいは少なくとも直感的であるように思えます。しかし、デザインの世界では、すべてが正方形ではありません。私たちはMinecraftの世界に住んでいるわけではありません。そのため、パスのようなより複雑な形状を導入し、Paintを追加して各形状を2回描画するだけでは不十分になります。内側と外側のストロークには、他のSkia関数を使用する必要があります。デフォルトでは、Skiaのストロークは中央揃え(SVGと同様)であるため、その場合は特別なことは何もする必要がありません。

この三角形のパスを想定します。

  const SkPath path = SkPathBuilder()
                          .moveTo(128, 37)
                          .lineTo(226, 219)
                          .lineTo(30, 219)
                          .close()
                          .detach();

  const float strokeWidth = 10;

  SkPaint fill;
  fill.setAntiAlias(true);
  fill.setStyle(SkPaint::kFill_Style);
  fill.setColor(0xFFA5F1EE);
  canvas->drawPath(path, fill);
// Inner stroke: centered stroke at double width, clipped to the path.
SkPaint stroke;
stroke.setAntiAlias(true);
stroke.setStyle(SkPaint::kStroke_Style);
stroke.setStrokeWidth(strokeWidth * 2);
stroke.setColor(0xFF9042FF);

canvas->save();
canvas->clipPath(path, SkClipOp::kIntersect, /*antialias=*/true);
canvas->drawPath(path, stroke);
canvas->restore();

[画像: Skiaを使用したパス上の内側ストローク]

// Center stroke: Skia's native behavior, nothing to correct.
SkPaint stroke;
stroke.setAntiAlias(true);
stroke.setStyle(SkPaint::kStroke_Style);
stroke.setStrokeWidth(strokeWidth);
stroke.setColor(0xFF14CECA);

canvas->drawPath(path, stroke);

[画像: Skiaを使用したパス上の中央ストローク]

// Outer stroke: draw a centered stroke at DOUBLE width into an
// isolated layer, then erase the shape's interior with kClear.
// Only the outside half of the stroke survives. The layer is
// essential: kClear must erase the layer, not the fill below.
SkPaint stroke;
stroke.setAntiAlias(true);
stroke.setStyle(SkPaint::kStroke_Style);
stroke.setStrokeWidth(strokeWidth * 2);
stroke.setColor(0xFFFC8CFF);

canvas->saveLayer(nullptr, nullptr);
canvas->drawPath(path, stroke);

SkPaint clear;
clear.setAntiAlias(true);
clear.setBlendMode(SkBlendMode::kClear);
canvas->drawPath(path, clear);
canvas->restore();

[画像: Skiaを使用したパス上の外側ストローク]

点線および破線スタイル

ストロークのスタイルによっては、点線または破線のストロークを扱う際に、適切なSkPathEffectを使用して創造性を発揮する必要があります。

SkPaint stroke;
stroke.setAntiAlias(true);
stroke.setStyle(SkPaint::kStroke_Style);
stroke.setStrokeWidth(strokeWidth);
stroke.setColor(0xFFFC8CFF);

const SkScalar intervals[] = {strokeWidth + 10, strokeWidth + 10};
stroke.setPathEffect(SkDashPathEffect::Make(intervals, 0.0f));

canvas->drawRect(rect, stroke);

[画像: Skiaを使用した破線ストローク]

SkPaint stroke;
stroke.setAntiAlias(true);
stroke.setStyle(SkPaint::kStroke_Style);
stroke.setStrokeWidth(strokeWidth);
stroke.setColor(0xFFFC8CFF);

SkPath circle = SkPath::Circle(0, 0, strokeWidth / 2);
const float advance = strokeWidth + 5;  // spacing between dot centers
stroke.setPathEffect(SkPath1DPathEffect::Make(
    circle, advance, /*phase=*/0.0f, SkPath1DPathEffect::kTranslate_Style));

canvas->drawRect(rect, stroke);

[画像: Skiaを使用した点線ストローク]

冒頭で述べたことが現実になっていることがわかります。追跡すべき変数が多く、システムのカオスを抑制する必要があります。

もう一つの変数:複数のストローク

これらすべては順調に進んでいるように見えますが、新しい変数が現れるまでです。それはストロークのプロパティではなく、形状のプロパティであるストロークの数です。すべてが順調すぎるように見えましたが、デザインは私たちにさらに一歩進むことを求めています。

Penpotの場合、複数の境界線は常にサポートされていました。結局のところ、SVGがあれば、より多くの要素を追加するだけです。WebGLレンダラーでは、同様のことを行います。指定された順序で、形状の数だけ境界線を追加します。

[画像: Skiaでの外側、中央、内側のストローク]

void draw(SkCanvas* canvas) {
      canvas->clear(SK_ColorWHITE);

      const SkRect rect = SkRect::MakeXYWH(48, 48, 160, 160);
      const float w = 16;
  
      SkPaint p;
      p.setAntiAlias(true);

      // Fill
      p.setStyle(SkPaint::kFill_Style);
      p.setColor(0xFFA5F1EE);
      canvas->drawRect(rect, p);

      p.setStyle(SkPaint::kStroke_Style);
      p.setStrokeWidth(w);

      // Outer stroke: outset by w/2 -> spans from shape edge outward.
      p.setColor(0xFFFC8CFF);                    // pink
      canvas->drawRect(rect.makeOutset(w / 2, w / 2), p);

      // Center stroke: on the rect itself -> half in, half out.
      // Overlaps the inner half of the outer stroke; drawn later, so it wins.
      p.setColor(0xFF14CECA);
      canvas->drawRect(rect, p);

      // Inner stroke: inset by w/2 -> spans from shape edge inward.
      // Drawn last, covers the inner half of the center stroke.
      p.setColor(0xFF9042FF);
      canvas->drawRect(rect.makeInset(w / 2, w / 2), p);
  }

各ストローク(および塗りつぶし)を個別に描画する必要があることがわかります。

[画像: Penpotで複数のストロークスタイルを使用したさまざまな形状]

しかし、CSSでは、同じ形状で複数のストロークをサポートすることは、border-タイプのプロパティでは不可能でした(ただし、これはすぐに変更される可能性があります!)。複数の影をサポートするbox-shadowプロパティが、最も古典的な回避策です。影は(ぼかしなしで)ソリッドに描画され、ストローク効果が得られます。

.multi-border {
    border-radius: 20px;
    background: #A5F1EE;
    box-shadow:
      inset 0 0 0 6px #9042FF,
      0 0 0 6px #A5F1EE,
      0 0 0 12px #14CECA,
      0 0 0 18px #FC8CFFAA;
}

[画像: box-shadowプロパティを使用した、さまざまなタイプのストロークを持つCSS正方形(角丸)]

これは外側ストロークと内側ストロークにも機能することが観察できます。後者の場合、inset設定を使用するとうまくいきます。とはいえ、CSSで複数の境界線を宣言的に追加できるようになるようです!(Lea Verou氏のBluesky投稿)

[画像: border-colorおよびoutline-colorプロパティを求めるLea Verou氏のBluesky投稿]

まとめ

3つの軌道を描く物体のようにカオスに陥る運命にあるように見えたシステムから始めました。3種類のストローク、それぞれが「デフォルト」のストロークについて意見が一致しない3つのフォーマット、そしてその上に積み重なる変数です。

結論:WebGLレンダラーを使用することで、SVGで可能だったことよりも、境界線に関連する視覚要素の実装において、はるかに柔軟性が高まりました。

そして、まだ終わりではありません。Penpotでストロークができることの限界を押し広げる余地はまだたくさんあります。「ストロークからパスへ」(ストロークを独自の編集可能なパス形状に変換する)や「辺ごとの個別のストローク」(形状の各辺に異なるストロークを設定する)といった機能です。

3つのストロークのパーティーは続きます 🎉