網站導覽

v2.1.0
實作

圖片調整

在瀏覽器中以裁切器裁切、旋轉並調整圖片,再將原始檔案和調整內容以資料形式交出。

本頁內容

讀者選擇一張圖片、裁切它、每次旋轉四分之一圈,並設定亮度、對比和飽和度,同時即時看到結果。瀏覽器中沒有任何東西會修改圖片本身:頁面交出的是原始檔案和以資料表示的調整內容,再由應用程式的影像處理流程裁切並編碼要儲存的圖片。本頁使用裁切器、原生輸入元件和 CSS 濾鏡實作這個流程。

圖片調整

<section class="sheet p-lg container-inline" data-example="image-adjustments" aria-labelledby="adjust-title">  <div class="flex flex-wrap items-center justify-between gap-sm mb-md">    <h2 class="m(0) text-md font-medium fg-text-strong" id="adjust-title">Adjust a picture</h2>    <div class="flex flex-wrap items-center gap-xs">      <label class="btn btn-sm">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M7.5892 2.215A0.4996 0.4996 0 0 0 7.5 2.5L7.5006 10.0245A0.5 0.5 0 0 0 8.4946 10.0734L8.5 10L8.4986 2.4621A0.5007 0.5007 0 0 0 8.4108 2.215A0.7044 0.7044 0 0 0 7.5892 2.215Z" fill="currentColor" stroke="none" fill-rule="evenodd" />          <path d="M5 5.5L7.8586 2.6414A0.2 0.2 0 0 1 8.1414 2.6414L11 5.5" />          <path d="M2.5 10.5L2.5 12.5A1 1 0 0 0 3.5 13.5L12.5 13.5A1 1 0 0 0 13.5 12.5L13.5 10.5" />        </svg>        <span data-choose>Choose a picture</span>        <input type="file" class="sr-only" accept="image/*" aria-describedby="adjust-hint">      </label>      <button type="button" class="btn btn-sm btn-quiet" data-clear disabled>Clear</button>    </div>  </div>  <p class="m(0) mb-md text-sm fg-text-muted" id="adjust-hint">The picture stays in this browser. Nothing is uploaded.</p>  <div data-error hidden>    <div class="alert alert-danger mb-md" role="alert">      <div class="alert-body">        <p class="alert-description">That file could not be read as a picture. Choose a PNG, JPG, WebP or AVIF.</p>      </div>    </div>  </div>  <div class="grid gap-lg grid-cols(1) grid-cols(2)@container(--2xl)">    <div class="flex flex-col gap-sm min-w(0)">      <div class="cropper" style="--cropper-x: 0; --cropper-y: 0; --cropper-width: 1; --cropper-height: 1">        <img class="cropper-image" src="/demo/dawn.svg" alt="A pier at dawn" width="640" height="400">        <div class="cropper-box" role="application" aria-roledescription="crop box" tabindex="0" aria-label="Crop the picture">          <span class="cropper-handle cropper-handle-top cropper-handle-left"></span>          <span class="cropper-handle cropper-handle-top"></span>          <span class="cropper-handle cropper-handle-top cropper-handle-right"></span>          <span class="cropper-handle cropper-handle-right"></span>          <span class="cropper-handle cropper-handle-bottom cropper-handle-right"></span>          <span class="cropper-handle cropper-handle-bottom"></span>          <span class="cropper-handle cropper-handle-bottom cropper-handle-left"></span>          <span class="cropper-handle cropper-handle-left"></span>        </div>        <p class="cropper-status" role="status"></p>      </div>      <div class="flex flex-wrap items-center gap-xs">        <button type="button" class="btn btn-sm" data-rotate="-90">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M3.1626 8.6029A4.8333 4.8333 0 0 0 12.5278 9.5552A4.8333 4.8333 0 0 0 4.5522 4.5549L2.653 6.3845A0.5001 0.5001 0 0 1 2.4222 6.492A0.7044 0.7044 0 0 1 1.8413 5.9111A0.4998 0.4998 0 0 1 1.9862 5.6401L3.8478 3.8451Q4.7383 2.9615 5.9133 2.5222Q7.2234 2.0323 8.6132 2.1898Q10.003 2.3473 11.1703 3.1179A5.8276 5.8276 0 0 1 13.4737 9.8799Q13.0195 11.2028 12.0149 12.176A5.8276 5.8276 0 0 1 4.9149 12.9636Q3.7213 12.2343 2.9883 11.0431Q2.3309 9.9747 2.1708 8.7305A0.5 0.5 0 0 1 2.5032 8.1942Q2.6066 8.1862 2.7088 8.1685A0.4998 0.4998 0 0 1 3.1626 8.6029Z" fill="currentColor" stroke="none" fill-rule="evenodd" />            <path d="M2.3333 2.3333L2.3333 5.8A0.2 0.2 0 0 0 2.5333 6L6 6" />          </svg>Rotate left</button>        <button type="button" class="btn btn-sm" data-rotate="90">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M3.1626 8.6029A4.8333 4.8333 0 0 0 12.5278 9.5552A4.8333 4.8333 0 0 0 4.5522 4.5549L2.653 6.3845A0.5001 0.5001 0 0 1 2.4222 6.492A0.7044 0.7044 0 0 1 1.8413 5.9111A0.4998 0.4998 0 0 1 1.9862 5.6401L3.8478 3.8451Q4.7383 2.9615 5.9133 2.5222Q7.2234 2.0323 8.6132 2.1898Q10.003 2.3473 11.1703 3.1179A5.8276 5.8276 0 0 1 13.4737 9.8799Q13.0195 11.2028 12.0149 12.176A5.8276 5.8276 0 0 1 4.9149 12.9636Q3.7213 12.2343 2.9883 11.0431Q2.3309 9.9747 2.1708 8.7305A0.5 0.5 0 0 1 2.5032 8.1942Q2.6066 8.1862 2.7088 8.1685A0.4998 0.4998 0 0 1 3.1626 8.6029Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="translate(16 0) scale(-1 1)" />            <path d="M2.3333 2.3333L2.3333 5.8A0.2 0.2 0 0 0 2.5333 6L6 6" transform="translate(16 0) scale(-1 1)" />          </svg>Rotate right</button>      </div>    </div>    <div class="flex flex-col gap-md min-w(0)">      <div class="well overflow-hidden flex items-center justify-center h(220)" data-stage aria-label="The adjusted picture" role="img"></div>      <div class="segmented segmented-sm" role="radiogroup" aria-label="Show the picture">        <label class="segmented-item">          <input type="radio" class="segmented-input" name="adjust-mode" value="fit" checked>Fit</label>        <label class="segmented-item">          <input type="radio" class="segmented-input" name="adjust-mode" value="fill">Fill</label>        <label class="segmented-item">          <input type="radio" class="segmented-input" name="adjust-mode" value="tile">Tile</label>      </div>      <div class="grid gap-sm">        <div class="field">          <div class="flex items-baseline justify-between">            <label for="adjust-brightness">Brightness</label>            <output class="fg-text-muted" for="adjust-brightness" data-for="brightness">100%</output>          </div>          <input type="range" class="slider" id="adjust-brightness" name="brightness" min="0" max="200" value="100">        </div>        <div class="field">          <div class="flex items-baseline justify-between">            <label for="adjust-contrast">Contrast</label>            <output class="fg-text-muted" for="adjust-contrast" data-for="contrast">100%</output>          </div>          <input type="range" class="slider" id="adjust-contrast" name="contrast" min="0" max="200" value="100">        </div>        <div class="field">          <div class="flex items-baseline justify-between">            <label for="adjust-saturation">Saturation</label>            <output class="fg-text-muted" for="adjust-saturation" data-for="saturation">100%</output>          </div>          <input type="range" class="slider" id="adjust-saturation" name="saturation" min="0" max="200" value="100">        </div>      </div>      <div class="flex flex-wrap gap-xs">        <button type="button" class="btn btn-sm" data-reset>Reset</button>      </div>    </div>  </div>  <details class="mt-lg">    <summary class="text-sm fg-text-strong">What the page hands on</summary> <pre class="code-block mt-sm"><code data-output></code></pre>  </details></section>
選擇你自己的圖片,或調整範例圖片:裁切、旋轉、設定色調,再展開頁面交出的資料。

#分工

部分負責
裁切器及其行為裁切框:它的邊緣、按鍵、指標操作,以及以圖片本身像素表示的裁切範圍
原生輸入元件選擇檔案、色調的範圍滑桿、顯示方式的單選按鈕
你的頁面讀取檔案、用來顯示檔案的物件 URL、調整內容的預覽,以及要交出的資料
你的應用程式裁切、旋轉、套用濾鏡並編碼要儲存的圖片,以及上傳圖片

頁面在自己的預覽上設定濾鏡和變形;沒有任何 Master UI 行為會繪製圖片。這是本範例中唯一由資料決定樣式的地方,而這件事留在頁面中處理。

#檔案

  • 選擇。 放在按鈕標籤內的檔案輸入欄位設定 accept="image/*"。圖片在顯示前會以 HTMLImageElement.decode() 解碼,因此不是圖片的檔案會被拒絕,並在提示訊息中說明原因,目前顯示的圖片則維持不變。
  • 顯示。 所選檔案以物件 URL 顯示。替換或清除圖片時,會以 URL.revokeObjectURL() 釋放舊的 URL;範例停止時,mount() 傳回的函式也會這麼做。
  • 重新開始。 裁切器從 cropper-image 的 width 和 height 讀取圖片尺寸,因此換新圖片時,會依其原始尺寸設定這兩個屬性,並重新掛上裁切器,涵蓋整張圖片。
  • 隱私。 檔案從不離開瀏覽器;請在按鈕旁說明這一點。

#調整內容

  • 裁切。 裁切器的 value,即以圖片像素表示的 { x, y, width, height },在裁切框每次觸發 input 和 change 時讀取。
  • 旋轉。 每次向左或向右旋轉四分之一圈,以 0、90、180 或 270 度表示。裁切範圍保持在圖片原本的方向;預覽則以中心為軸旋轉裁切後的圖片,因此在 90 和 270 度時寬高會互換。
  • 色調。 亮度、對比和飽和度,範圍從 0% 到 200%,100% 代表圖片原樣。CSS 剛好有這三種對應的 brightness()、contrast() 和 saturate(),因此預覽的結果,就是影像處理流程套用相同三種調整後會產生的結果。
  • 顯示方式。 「完整顯示」會在預覽區內顯示完整結果,「填滿」會讓結果覆蓋整個預覽區,「平鋪」則會像背景圖片一樣重複排列。

「重設」會還原色調和裁切範圍,但保留圖片。

adjustments.json
{  "file": { "name": "dawn.jpg", "type": "image/jpeg", "size": 482113 },  "crop": { "x": 160, "y": 80, "width": 1280, "height": 800 },  "rotate": 90,  "brightness": 110,  "contrast": 100,  "saturation": 85,  "mode": "fit"}

請將原始的 File 和這個物件一起交給應用程式:應用程式可以用相同方式以原始解析度裁切、旋轉圖片並套用濾鏡,同時保留原始檔案,以便日後再次調整。

#未涵蓋的部分

完整的相片編輯器不只有這三種色調,還有色溫、亮部與陰影、曲線,以及匯出的檔案。CSS 沒有對應這些功能的濾鏡,而近似的做法會讓讀者看到儲存的圖片實際上不會有的結果,因此本範例選擇省略,而不是模仿。在瀏覽器中以 canvas 或 OffscreenCanvas 編碼結果,是應用程式的選擇,這裡不予示範。

#無障礙

  • 檔案輸入欄位是放在有標籤的按鈕內的真正輸入元件,可以用 Tab 抵達;其下方的提示說明圖片會被送往何處。
  • 裁切框具備裁切器的按鍵操作,其狀態區域會在每次變更後說明裁切範圍。
  • 每種色調都是一個有標籤的範圍滑桿,旁邊寫出它的數值;旋轉則是以方向命名的按鈕。
  • 預覽以圖片本身的描述加上「調整後」來命名,例如「黎明時的碼頭,調整後」;對圖片所做的調整寫在頁面顯示的資料中,而不只呈現在圖像裡。