10.1 KiB · text History 11accb3
defmodule VigilWeb.ShaderLive.Index do
  @moduledoc """
  /shaders: the neiam moire wallpaper's patterns, live, each settable as
  the page background (neiam-org's /shaders, in Vigil's frame). The
  drawing and the settings live in the browser
  (assets/js/shader_field.js, shader_background.js); this page only lays
  out the previews and controls. Public, like the theme picker it hangs
  off: the choice is the browser's, not the account's.
  """
  use VigilWeb, :live_view

  # `id` is the pattern name the shader settings use.
  @patterns [
    %{
      id: "both",
      name: "Moire + Halftone",
      about: "The wallpaper's default: rings and dots together, fading out from a drifting hub."
    },
    %{
      id: "moire",
      name: "Moire",
      about:
        "Two ring families slightly apart; where they interleave and coincide, the moire beats."
    },
    %{
      id: "halftone",
      name: "Halftone",
      about: "Dots on a rotated grid, sized as in print by the fade and a travelling wave."
    },
    %{
      id: "bokeh",
      name: "Bokeh",
      about:
        "Soft out-of-focus discs drifting upward, fading in and out as they rise, each its own shade."
    },
    %{
      id: "stars",
      name: "Starfield",
      about: "Sparse stars, mostly faint and a few bright, each twinkling at its own rate."
    },
    %{
      id: "prism",
      name: "Prism",
      about:
        "A glowing beam strikes a turning shape and splits into rays, each its own width, slowly swelling and thinning."
    },
    %{
      id: "oil",
      name: "Oil on glass",
      about:
        "Slicks of oil on dark glass, their colours the bands of a thin film, swirling slowly."
    },
    %{
      id: "bubbles",
      name: "Bubbles",
      about:
        "Soap bubbles drifting about and bouncing off the edges and each other, their films swirling."
    },
    %{
      id: "spheres",
      name: "Absorbing spheres",
      about:
        "Spheres launched every which way; where two meet, the larger absorbs the smaller. At three, a new round."
    },
    %{
      id: "eyes",
      name: "Eyes",
      about:
        "Eyes that open where they please, look about, and now and then at you; then close and go elsewhere."
    },
    %{
      id: "alternate",
      name: "Alternate",
      about: "Fades back and forth between moire and halftone every few minutes."
    }
  ]

  @effects [
    %{
      id: "glints",
      name: "Glint",
      about: "a band of light sweeping across every minute and a half"
    },
    %{id: "breathing", name: "Breathing", about: "the pattern slowly swelling and easing"},
    %{
      id: "foreground",
      name: "Foreground discs",
      about: "large defocused discs drifting past now and then"
    },
    %{id: "grain", name: "Film grain", about: "a fine moving noise"},
    %{id: "dither", name: "Dither", about: "keeps slow gradients from banding"},
    %{
      id: "alwaysMove",
      name: "Always move",
      about: "animate even when your system asks for reduced motion (otherwise one still frame)"
    }
  ]

  # The prism pattern's shape: its number of sides, 0 for a circle.
  @prism_shapes [
    {0, "Circle"},
    {3, "Triangle"},
    {4, "Square"},
    {5, "Pentagon"},
    {6, "Hexagon"},
    {7, "Heptagon"},
    {8, "Octagon"}
  ]

  @impl true
  def mount(_params, _session, socket) do
    {:ok,
     socket
     |> assign(:page_title, "Shaders")
     |> assign(:patterns, @patterns)
     |> assign(:effects, @effects)
     |> assign(:prism_shapes, @prism_shapes)}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <Layouts.app flash={@flash} current_scope={@current_scope} active={:shaders}>
      <section class="space-y-1">
        <p class="text-xs font-semibold uppercase tracking-[0.2em] text-primary">Background</p>
        <h1 class="text-2xl font-extrabold tracking-tight sm:text-3xl">Shaders</h1>
        <p class="text-sm text-base-content/70">
          Everything drifts slowly and never repeats. Pick one for the page background, or turn it off.
        </p>
      </section>

      <%!-- Drawn and driven entirely in the browser; LiveView leaves it be. --%>
      <div id="shader-controls" phx-hook="ShaderControls" phx-update="ignore" class="space-y-6">
        <%!-- Current background and shared settings --%>
        <div class="space-y-4 rounded-box border border-base-300 bg-base-100 p-5">
          <div class="flex flex-wrap items-center justify-between gap-4">
            <p>
              Background: <span class="font-semibold text-primary" data-shader-current>Off</span>
            </p>
            <button type="button" class="btn btn-sm btn-ghost border-base-300" data-use="off">
              <.icon name="hero-no-symbol-micro" class="size-4" /> Turn off
            </button>
          </div>
          <div class="grid gap-x-8 gap-y-3 md:grid-cols-2 xl:grid-cols-3">
            <label :for={effect <- @effects} class="flex cursor-pointer items-start gap-3">
              <input
                type="checkbox"
                class="toggle toggle-primary toggle-sm mt-0.5"
                data-setting={effect.id}
              />
              <span class="text-sm">
                <span class="font-semibold">{effect.name}</span>
                <span class="text-base-content/70">— {effect.about}</span>
              </span>
            </label>
            <.range label="Bokeh lights" setting="lights" min="4" max="64" step="1" />
            <.range label="Speed" setting="speed" min="0.25" max="4" step="0.25" unit="×" />
          </div>

          <%!-- Shown only while that pattern is the background --%>
          <.pattern_settings pattern="prism" title="Prism">
            <.range label="Beam angle" setting="prismAngle" min="0" max="355" step="5" unit="°" />
            <.range label="Rays" setting="prismRays" min="2" max="24" step="1" />
            <label class="flex flex-col gap-1">
              <span class="text-sm font-semibold">Shape</span>
              <select class="select select-sm" data-setting="prismSides">
                <option :for={{sides, name} <- @prism_shapes} value={sides}>{name}</option>
              </select>
            </label>
            <label class="flex cursor-pointer items-start gap-3">
              <input
                type="checkbox"
                class="toggle toggle-primary toggle-sm mt-0.5"
                data-setting="prismSlow"
              />
              <span class="text-sm">
                <span class="font-semibold">Slow spin</span>
                <span class="text-base-content/70">
                  — a steady live rotation, instead of a turn every 15 s
                </span>
              </span>
            </label>
          </.pattern_settings>
          <.pattern_settings pattern="oil" title="Oil on glass">
            <.range label="Coverage" setting="oilCoverage" min="0" max="100" step="5" unit="%" />
          </.pattern_settings>
          <.pattern_settings pattern="bubbles" title="Bubbles">
            <.range label="Bubbles" setting="bubbleCount" min="1" max="32" step="1" />
          </.pattern_settings>
          <.pattern_settings pattern="spheres" title="Absorbing spheres">
            <.range label="Spheres per round" setting="sphereCount" min="4" max="48" step="1" />
          </.pattern_settings>
          <.pattern_settings pattern="eyes" title="Eyes">
            <.range label="Eyes" setting="eyeCount" min="1" max="24" step="1" />
            <.range
              label="Largest to smallest"
              setting="eyeRatio"
              min="1"
              max="6"
              step="0.5"
              unit="×"
            />
          </.pattern_settings>

          <p class="text-xs text-base-content/60">
            Colours follow the theme. Settings are kept in this browser, and apply to the previews and the background alike.
          </p>
        </div>

        <%!-- One live preview per pattern --%>
        <div class="grid gap-4 md:grid-cols-2 xl:grid-cols-3">
          <div
            :for={pattern <- @patterns}
            data-shader-card={pattern.id}
            data-name={pattern.name}
            class="flex flex-col gap-3 rounded-box border border-base-300 bg-base-100 p-4"
          >
            <div class="flex items-center justify-between gap-4">
              <h2 class="font-semibold">{pattern.name}</h2>
              <button type="button" class="btn btn-primary btn-sm" data-use={pattern.id}>Use</button>
            </div>
            <canvas
              id={"shader-#{pattern.id}"}
              phx-hook="ShaderPreview"
              data-pattern={pattern.id}
              class="aspect-video w-full rounded-field border border-base-300 bg-base-300"
            ></canvas>
            <p class="text-sm text-base-content/70">{pattern.about}</p>
          </div>
        </div>
      </div>

      <p class="text-center text-xs text-base-content/60">
        The neiam moire wallpaper, from neiam-device: the same shader as the Plasma and Android versions.
      </p>
    </Layouts.app>
    """
  end

  attr :pattern, :string, required: true
  attr :title, :string, required: true
  slot :inner_block, required: true

  defp pattern_settings(assigns) do
    ~H"""
    <div data-shader-when={@pattern} hidden>
      <div class="space-y-3 border-t border-base-300 pt-4">
        <h3 class="text-xs font-semibold uppercase tracking-[0.2em] text-primary">{@title}</h3>
        <div class="grid gap-x-8 gap-y-3 md:grid-cols-2 xl:grid-cols-3">
          {render_slot(@inner_block)}
        </div>
      </div>
    </div>
    """
  end

  attr :label, :string, required: true
  attr :setting, :string, required: true
  attr :min, :string, required: true
  attr :max, :string, required: true
  attr :step, :string, required: true
  attr :unit, :string, default: ""

  defp range(assigns) do
    ~H"""
    <label class="flex flex-col gap-1">
      <span class="text-sm">
        <span class="font-semibold">{@label}</span>: <span data-shader-value={@setting}></span>{@unit}
      </span>
      <input
        type="range"
        min={@min}
        max={@max}
        step={@step}
        class="range range-primary range-xs"
        data-setting={@setting}
      />
    </label>
    """
  end
end