# 蚂蚁线选区

黑白相间的虚线沿着印刷样张上选中图形的边缘移动。

## 试一试

选择圆形、拱形或斜条。隐藏虚线后查看油墨边缘，再显示虚线，确认选中的仍是同一个图形。

## 交互细节

分开绘制图形和选区边界。在与图形轮廓一致的 SVG 路径上改变虚线偏移量。暂停后虚线停在当前位置；设备启用减弱动态效果时显示静止虚线。

## 各平台的操作方式

- iOS · 轻点样张上的图形或下方对应按钮。隐藏虚线后再次显示，确认当前选中的图形。
- Android · 点按图形或对应按钮。用“暂停动画”定住虚线，或用“隐藏虚线”查看油墨边缘。
- PC · 点击图形，或用 Tab 移至图形按钮后按回车键。依次点击“暂停动画”和“隐藏虚线”，比较两种状态。

## 适用场景

适合在绘图或印刷校样工具中查看被选区边界遮住的细节。

## 实现要求

> 请在选中的图形周围绘制移动的黑白虚线，允许用户单独控制虚线的显示和动画播放，并保留当前选择。

## 来源与致谢

[GIMP · Show Selection](<https://docs.gimp.org/3.0/en/gimp-view-show-selection.html>)

## 独立 HTML 示例

将以下代码保存为 HTML 文件，用浏览器打开即可离线体验。

```html
<!doctype html>
<html lang="zh-Hans" data-motion-locales="inline">
<head><script>if(new URLSearchParams(location.search).get('reduce')==='1')document.documentElement.classList.add('motion-reduced');</script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="color-scheme" content="light">
  <title>움직이는 선택 경계</title>
  <style>
    :root {
      color-scheme: light;
      font-family: "Segoe UI", "Malgun Gothic", system-ui, sans-serif;
      color: #282820;
      background: #f5f0e5;
    }
    * { box-sizing: border-box; }
    body { margin: 0; }
    main { width: min(100%, 920px); margin: auto; padding: clamp(16px, 3.6vw, 38px); }
    header { display: flex; align-items: flex-end; gap: 15px; margin-bottom: 17px; }
    .register { flex: 0 0 auto; width: 30px; height: 30px; margin-bottom: 3px; color: #09625e; }
    h1 { margin: 0; font-size: clamp(24px, 4.1vw, 42px); line-height: 1.12; letter-spacing: -.055em; }
    .intro { max-width: 66ch; margin: 0 0 16px; color: #514d40; font-size: 14px; line-height: 1.55; }
    .paper { border: 1px solid #beb7a4; background: #fdf9ee; overflow: hidden; }
    .paper svg { width: 100%; height: auto; display: block; }
    .print-shape { cursor: pointer; touch-action: manipulation; }
    .registration-mark { fill: none; stroke: #6d7262; stroke-width: 1; }
    #selection { pointer-events: none; fill: none; stroke-linejoin: round; }
    #selection use { vector-effect: non-scaling-stroke; }
    #base-edge { stroke: white; stroke-width: 6; }
    #ants-edge { stroke: #171c19; stroke-width: 5; stroke-dasharray: 8 7; animation: march .82s linear infinite; }
    @keyframes march { to { stroke-dashoffset: -15; } }
    [data-motion="paused"] #ants-edge, [data-motion="reduced"] #ants-edge, [data-outline="hidden"] #ants-edge { animation-play-state: paused; }
    [data-outline="hidden"] #selection { visibility: hidden; }
    @media (prefers-reduced-motion: reduce) { #ants-edge { animation: none; } }
    .tools { display: flex; flex-wrap: wrap; gap: 15px 24px; align-items: flex-end; margin: 17px 0 12px; }
    .control-label { display: block; font-size: 12px; font-weight: 650; margin-bottom: 6px; color: #525247; }
    .buttons { display: flex; flex-wrap: wrap; gap: 5px; }
    button {
      min-height: 42px; display: inline-flex; align-items: center; justify-content: center; gap: 7px;
      padding: 6px 12px; border: 1px solid #a9a58f; border-radius: 4px; background: #fffdf6; color: inherit;
      font-family: inherit; font-size: 13px; font-weight: 600; line-height: 1.25;
      cursor: pointer; touch-action: manipulation;
    }
    button:hover { border-color: #424d41; }
    button:focus-visible { outline: 3px solid #005f96; outline-offset: 2px; }
    button[aria-pressed="true"] { background: #283b31; border-color: #283b31; color: #fffef4; }
    button[disabled] { cursor: default; background: #e5e2d8; border-color: #c4c1b1; color: #5b5a52; }
    .swatch { width: 12px; height: 12px; display: inline-block; box-shadow: inset 0 0 0 1px rgba(0,0,0,.25); }
    [data-choose="disc"] .swatch { background: #f9ac2f; border-radius: 50%; }
    [data-choose="arch"] .swatch { background: #19978c; border-radius: 7px 7px 1px 1px; }
    [data-choose="strip"] .swatch { background: #ea6444; height: 7px; transform: rotate(-18deg); }
    #status { margin: 0; border-top: 1px solid #c6c3b1; padding-top: 9px; font-size: 13px; line-height: 1.5; }
    @media(max-width: 540px) {
      header { gap: 9px; margin-bottom: 10px; }
      .register { width: 23px; height: 23px; }
      .tools { gap: 11px; margin-top: 12px; }
      .group { width: 100%; }
      button { min-height: 44px; }
      .intro { font-size: 13px; margin-bottom: 12px; }
    }
  </style>
</head>
<body>
  <main id="scene" data-selected="disc" data-outline="visible" data-motion="running">
    <header>
      <svg class="register" viewBox="0 0 40 40" aria-hidden="true"><g stroke="currentColor" stroke-width="2" fill="none"><circle cx="20" cy="20" r="12"/><path d="M20 0v40M0 20h40"/></g><circle cx="20" cy="20" r="3" fill="currentColor"/></svg>
      <h1 data-label="title">움직이는 선택 경계</h1>
    </header>
    <p class="intro" data-label="intro">인쇄판에서 도형을 골라 보세요. 점선만 숨기면 선택한 도형의 가장자리를 가림 없이 확인할 수 있습니다.</p>
    <div class="paper">
      <svg id="print" viewBox="0 0 700 330" aria-hidden="true" focusable="false">
        <defs>
          <path id="disc" d="M290 149a89 89 0 1 1-178 0 89 89 0 1 1 178 0Z"/>
          <path id="arch" d="M320 242V151a75 75 0 0 1 150 0v91h-45v-91a30 30 0 0 0-60 0v91Z"/>
          <path id="strip" d="m472 159 93-63 33 48-93 63Z"/>
          <pattern id="grid" width="35" height="35" patternUnits="userSpaceOnUse"><path d="M35 0H0v35" fill="none" stroke="#d6cfbb" stroke-width="1"/></pattern>
          <pattern id="screen" width="7" height="7" patternUnits="userSpaceOnUse" patternTransform="rotate(-20)"><circle cx="2.5" cy="2.5" r="1.7" fill="#a85320"/></pattern>
          <pattern id="parallel" width="8" height="8" patternUnits="userSpaceOnUse" patternTransform="rotate(28)"><path d="M3 0v8" stroke="#0c584e" stroke-width="2.2"/></pattern>
          <pattern id="engrave" width="9" height="9" patternUnits="userSpaceOnUse" patternTransform="rotate(-34)"><path d="M3 0v9" stroke="#a03829" stroke-width="2.4"/></pattern>
          <clipPath id="clip-disc"><use href="#disc"/></clipPath>
          <clipPath id="clip-arch"><use href="#arch"/></clipPath>
          <clipPath id="clip-strip"><use href="#strip"/></clipPath>
          <g id="target" class="registration-mark"><circle r="5.5"/><path d="M-11 0h22M0-11v22"/></g>
        </defs>
        <rect width="700" height="330" fill="#f9f4e7"/>
        <rect x="30" y="27" width="640" height="276" fill="url(#grid)"/>
        <rect x="30" y="27" width="640" height="276" stroke="#bfbba7" fill="none"/>
        <g><use href="#target" x="30" y="27"/><use href="#target" x="670" y="27"/><use href="#target" x="30" y="303"/><use href="#target" x="670" y="303"/></g>
        <g fill="#a8a391" opacity=".7"><rect x="60" y="266" width="580" height="1"/><rect x="60" y="272" width="365" height="1"/></g>
        <g class="print-shape" data-art="disc">
          <use href="#disc" fill="#eeb74a"/>
          <g clip-path="url(#clip-disc)" pointer-events="none"><path d="M112 134q56 34 112-7t93-10v140H112Z" fill="#dc9638"/><circle cx="188" cy="235" r="91" fill="url(#screen)"/></g>
        </g>
        <g class="print-shape" data-art="arch">
          <use href="#arch" fill="#1b8d80"/>
          <g clip-path="url(#clip-arch)" pointer-events="none"><path d="m299 188 197-72v147H299Z" fill="#26786b"/><path d="m300 208 200-65v130H300Z" fill="url(#parallel)"/></g>
        </g>
        <g class="print-shape" data-art="strip">
          <use href="#strip" fill="#e36746"/>
          <g clip-path="url(#clip-strip)" pointer-events="none"><path d="m468 204 144-97v60l-102 64Z" fill="url(#engrave)"/></g>
        </g>
        <g id="selection"><use id="base-edge" href="#disc"/><use id="ants-edge" href="#disc"/></g>
        <g transform="translate(558 275)"><rect width="18" height="6" fill="#eeb74a"/><rect x="19" width="18" height="6" fill="#1b8d80"/><rect x="38" width="18" height="6" fill="#e36746"/><rect x="57" width="18" height="6" fill="#283b31"/></g>
      </svg>
    </div>
    <div class="tools">
      <div class="group" role="group" aria-labelledby="plate-label">
        <span class="control-label" id="plate-label" data-label="plate">도형 선택</span>
        <div class="buttons">
          <button type="button" data-choose="disc" aria-pressed="true"><i class="swatch" aria-hidden="true"></i><span data-label="disc">원</span></button>
          <button type="button" data-choose="arch" aria-pressed="false"><i class="swatch" aria-hidden="true"></i><span data-label="arch">아치</span></button>
          <button type="button" data-choose="strip" aria-pressed="false"><i class="swatch" aria-hidden="true"></i><span data-label="strip">사선 띠</span></button>
        </div>
      </div>
      <div class="group" role="group" aria-labelledby="outline-label">
        <span class="control-label" id="outline-label" data-label="edge">선택 경계</span>
        <div class="buttons"><button type="button" id="toggle-outline">점선 숨기기</button><button type="button" id="toggle-motion">움직임 멈추기</button></div>
      </div>
    </div>
    <p id="status" role="status" aria-live="polite" aria-atomic="true"></p>
  </main>
  <script>
    const strings = {
      ko: {
        title: '움직이는 선택 경계', intro: '인쇄판에서 도형을 골라 보세요. 점선만 숨기면 선택한 도형의 가장자리를 가림 없이 확인할 수 있습니다.',
        plate: '도형 선택', edge: '선택 경계', disc: '원', arch: '아치', strip: '사선 띠',
        hide: '점선 숨기기', show: '점선 보기', pause: '움직임 멈추기', resume: '움직임 재개', reduced: '동작 줄이기 적용 중',
        selected: name => name + ' 선택됨.', hidden: '점선을 숨겼습니다.', running: '점선이 가장자리를 따라 움직입니다.', paused: '점선이 멈춰 있습니다.', still: '기기 설정에 따라 점선이 멈춰 있습니다.'
      },
      en: {
        title: 'Marching ants', intro: 'Choose a shape on the print proof. Hide its outline to inspect the ink edge while keeping the shape selected.',
        plate: 'Select a shape', edge: 'Selection outline', disc: 'Disc', arch: 'Arch', strip: 'Stripe',
        hide: 'Hide outline', show: 'Show outline', pause: 'Pause motion', resume: 'Resume motion', reduced: 'Reduced motion on',
        selected: name => name + ' selected.', hidden: 'Outline hidden.', running: 'Dashes travel along the edge.', paused: 'Dashes paused.', still: 'Your device settings keep the dashes still.'
      },
      es: {
        title: 'Contorno de selección animado', intro: 'Elige una figura en la prueba de impresión. Oculta el contorno para ver el borde de la tinta sin perder la selección.',
        plate: 'Elegir figura', edge: 'Contorno de selección', disc: 'Disco', arch: 'Arco', strip: 'Franja',
        hide: 'Ocultar contorno', show: 'Mostrar contorno', pause: 'Pausar animación', resume: 'Reanudar animación', reduced: 'Movimiento reducido activo',
        selected: name => 'Selección actual: ' + name.toLowerCase() + '.', hidden: 'Contorno oculto.', running: 'Los trazos recorren el borde.', paused: 'Los trazos están en pausa.', still: 'Los trazos permanecen fijos por la configuración del dispositivo.'
      },
      'zh-Hans': {
        title: '蚂蚁线选区', intro: '在印刷样张上选一个图形。隐藏虚线后，图形仍保持选中，方便查看油墨边缘。',
        plate: '选择图形', edge: '选区边界', disc: '圆形', arch: '拱形', strip: '斜条',
        hide: '隐藏虚线', show: '显示虚线', pause: '暂停动画', resume: '继续动画', reduced: '已启用减弱动态效果',
        selected: name => '已选中' + name + '。', hidden: '虚线已隐藏。', running: '虚线沿边缘移动。', paused: '虚线已暂停。', still: '按设备设置，虚线保持静止。'
      }
    };
    const params = new URLSearchParams(location.search);
    const requested = params.get('lang') || document.documentElement.lang || 'ko';
    const locale = Object.hasOwn(strings, requested) ? requested : 'ko';
    const t = strings[locale];
    document.documentElement.lang = locale;
    document.title = t.title;
    document.querySelectorAll('[data-label]').forEach(node => { node.textContent = t[node.dataset.label]; });
    const scene = document.getElementById('scene');
    const outlineButton = document.getElementById('toggle-outline');
    const motionButton = document.getElementById('toggle-motion');
    const status = document.getElementById('status');
    const choiceButtons = [...document.querySelectorAll('[data-choose]')];
    const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
    const reducedQuery = params.get('reduce') === '1';
    let selected = 'disc';
    let visible = true;
    let paused = false;
    function render() {
      const reduced = reducedQuery || reducedMotion.matches || document.documentElement.classList.contains('motion-reduced');
      scene.dataset.selected = selected;
      scene.dataset.outline = visible ? 'visible' : 'hidden';
      scene.dataset.motion = reduced ? 'reduced' : paused ? 'paused' : 'running';
      for (const button of choiceButtons) button.setAttribute('aria-pressed', String(button.dataset.choose === selected));
      for (const line of document.querySelectorAll('#selection use')) line.setAttribute('href', '#' + selected);
      outlineButton.textContent = visible ? t.hide : t.show;
      motionButton.disabled = reduced || !visible;
      motionButton.textContent = reduced ? t.reduced : paused ? t.resume : t.pause;
      const state = !visible ? t.hidden : reduced ? t.still : paused ? t.paused : t.running;
      status.textContent = t.selected(t[selected]) + ' ' + state;
    }
    function choose(shape) {
      selected = shape;
      render();
    }
    for (const button of choiceButtons) button.addEventListener('click', () => choose(button.dataset.choose));
    document.querySelectorAll('[data-art]').forEach(shape => shape.addEventListener('click', () => choose(shape.dataset.art)));
    outlineButton.addEventListener('click', () => { visible = !visible; render(); });
    motionButton.addEventListener('click', () => { paused = !paused; render(); });
    reducedMotion.addEventListener('change', render);
    new MutationObserver(render).observe(document.documentElement, {attributes: true, attributeFilter: ['class']});
    render();
  </script>
</body>
</html>
```
