<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: David Miller</title>
    <description>The latest articles on DEV Community by David Miller (@morphio).</description>
    <link>https://dev.to/morphio</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4179400%2F09c81e5a-b04c-4ad5-bc95-3aaba5e95a9f.png</url>
      <title>DEV Community: David Miller</title>
      <link>https://dev.to/morphio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/morphio"/>
    <language>en</language>
    <item>
      <title>Compare intrinsic and rendered image widths when debugging layout</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Mon, 23 Nov 2026 15:33:34 +0000</pubDate>
      <link>https://dev.to/morphio/compare-intrinsic-and-rendered-image-widths-when-debugging-layout-23cc</link>
      <guid>https://dev.to/morphio/compare-intrinsic-and-rendered-image-widths-when-debugging-layout-23cc</guid>
      <description>&lt;p&gt;Start with two numbers when a photo preview looks blurry. After the image loads, read its naturalWidth and its getBoundingClientRect().width. The first is the intrinsic width in CSS pixels. The second is the width of the rendered box. Use an image with no border or transform for this check.&lt;/p&gt;

&lt;p&gt;If the intrinsic width is 400 CSS pixels and the box is 600 CSS pixels wide, look for the layout rule that widens the preview. For an image with responsive sources, check currentSrc as well. It shows which resource the browser selected.&lt;/p&gt;

&lt;p&gt;Put both widths and the viewport size in the bug report. That gives the next person enough information to reproduce the same layout.&lt;/p&gt;

&lt;p&gt;This article was generated by AI.&lt;/p&gt;

</description>
      <category>css</category>
      <category>debugging</category>
      <category>frontend</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Cap canvas image exports by total pixel count</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Wed, 18 Nov 2026 07:42:08 +0000</pubDate>
      <link>https://dev.to/morphio/cap-canvas-image-exports-by-total-pixel-count-423n</link>
      <guid>https://dev.to/morphio/cap-canvas-image-exports-by-total-pixel-count-423n</guid>
      <description>&lt;p&gt;A 4000 × 3000 photo has 12 million pixels. To draw it into 3 million pixels, cut each side in half. The scale for one side is the square root of the area ratio, since the same scale applies to both axes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fitPixelBudget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;budget&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;budget&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;budget&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;budget&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;budget&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The helper expects positive integer dimensions and a positive integer budget. With a budget of 3,000,000, the photo comes out at 2000 × 1500. For a very narrow input, rounding down can leave one side at zero. The one-pixel minimum keeps it drawable, and the extra cap keeps the area within budget.&lt;/p&gt;

&lt;p&gt;Set the canvas width and height before drawing. If the download also has to fit a byte limit, measure the encoded file separately.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>performance</category>
      <category>programming</category>
    </item>
    <item>
      <title>Let users retry the same photo in a JavaScript file picker</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Fri, 13 Nov 2026 07:40:16 +0000</pubDate>
      <link>https://dev.to/morphio/let-users-retry-the-same-photo-in-a-javascript-file-picker-3386</link>
      <guid>https://dev.to/morphio/let-users-retry-the-same-photo-in-a-javascript-file-picker-3386</guid>
      <description>&lt;p&gt;Select a photo, simulate a failed upload, and select the same photo again. If the input still holds that file, the browser can treat the second selection as a cancellation, so a &lt;code&gt;change&lt;/code&gt; handler may never run.&lt;/p&gt;

&lt;p&gt;For a JavaScript-managed queue, take a reference to the &lt;code&gt;File&lt;/code&gt; before clearing the picker. In this example &lt;code&gt;addPhotoToQueue&lt;/code&gt; is the application function that stores the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"photo"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Add a photo&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"photo"&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"file"&lt;/span&gt; &lt;span class="na"&gt;accept&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"image/*"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;picker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#photo"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"change"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;addPhotoToQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Show the filename from the queue once the picker resets. If the form submits directly to the server, keep the input populated so the browser can send the selected file with the form.&lt;/p&gt;

&lt;p&gt;To check the queue, pick the same image twice and confirm both selections reach the handler. Then open the picker and dismiss it. The queued files should stay as they were.&lt;/p&gt;

&lt;p&gt;This article was drafted with AI assistance and checked against the HTML Standard.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Pin the exact image fixture used by a browser test</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Sun, 08 Nov 2026 02:18:11 +0000</pubDate>
      <link>https://dev.to/morphio/pin-the-exact-image-fixture-used-by-a-browser-test-123h</link>
      <guid>https://dev.to/morphio/pin-the-exact-image-fixture-used-by-a-browser-test-123h</guid>
      <description>&lt;p&gt;A test named “upload a portrait” can start producing different results if portrait.jpg is replaced with a newer export. If the filename stays the same, a review may focus on the upload code while the test is exercising a different file.&lt;/p&gt;

&lt;p&gt;Give each fixture a stable path and describe the property the test needs. For a test that checks whether a preview keeps the right proportions, record the source dimensions next to the fixture. A file named portrait-600x900.jpg still needs a check: the name does not change if somebody replaces the file with a 900 by 600 export.&lt;/p&gt;

&lt;p&gt;Include a digest of the file in the result when the test runs on multiple machines. Calculate it from the fixture bytes in the test setup and include it in the failure output with the expected dimensions. If two runs report different digests, compare the test inputs before changing the code. If the digests match, compare what the decoder and preview received.&lt;/p&gt;

&lt;p&gt;When a test fails, report the dimensions from each stage separately. “Input 600×900; preview 300×450; downloaded copy 450×300” shows where the orientation first changed. A line that says only “unexpected size” hides where the first change occurred.&lt;/p&gt;

&lt;p&gt;Use a small synthetic fixture when the content of the picture does not matter. Commit a replacement as a deliberate fixture change and update the expected values in the same review. Then the reviewer can see which behavior the revised test covers.&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>softwareengineering</category>
      <category>testing</category>
    </item>
    <item>
      <title>Render a selected image filename as text</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Fri, 06 Nov 2026 03:28:24 +0000</pubDate>
      <link>https://dev.to/morphio/render-a-selected-image-filename-as-text-1glh</link>
      <guid>https://dev.to/morphio/render-a-selected-image-filename-as-text-1glh</guid>
      <description>&lt;p&gt;Show a selected image’s filename literally beside its preview. Angle brackets and ampersands in that name should appear as characters on the page.&lt;/p&gt;

&lt;p&gt;Read file.name from the selected File object, create a separate span for the label, and assign the name to that span’s textContent. Then append the span to the preview row. Do not interpolate the name into an HTML string and assign that string to innerHTML. The browser will parse HTML assigned to innerHTML; textContent inserts text instead.&lt;/p&gt;

&lt;p&gt;Keep the label in its own element. Setting textContent replaces the element’s existing children, so assigning it to a row that already holds an image and a remove button will remove those children. Build the row once, and update only the label element when the selection changes.&lt;/p&gt;

&lt;p&gt;Test the label renderer with synthetic filenames containing an ampersand and angle brackets. You can construct File objects for this test instead of depending on which characters your operating system allows in a real filename. Check that the characters appear literally and that the row still contains its preview and controls. The test does not need to decode an image: it exercises filename rendering only.&lt;/p&gt;

&lt;p&gt;The filename label tells you nothing about whether the bytes decode as an image or pass an upload policy. Keep decoding, file limits and server validation as separate checks. Also avoid publishing real filenames in screenshots if those names contain private information.&lt;/p&gt;

&lt;p&gt;References: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/File/name" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/File/name&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Give a JPEG file picker useful hints without treating them as validation</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Thu, 05 Nov 2026 14:10:59 +0000</pubDate>
      <link>https://dev.to/morphio/give-a-jpeg-file-picker-useful-hints-without-treating-them-as-validation-5h42</link>
      <guid>https://dev.to/morphio/give-a-jpeg-file-picker-useful-hints-without-treating-them-as-validation-5h42</guid>
      <description>&lt;p&gt;A JPEG upload field should tell the browser what to offer and tell the user what the endpoint accepts. Those hints do not establish that the selected file is a valid upload.&lt;/p&gt;

&lt;p&gt;The accept attribute supports comma-separated filename extensions and MIME types. MDN recommends accounting for the different ways a required type can be identified. For a JPEG-only field, you can include .jpg, .jpeg and image/jpeg:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="photo"&amp;gt;Choose a JPEG photograph&amp;lt;/label&amp;gt;
&amp;lt;input
  id="photo"
  type="file"
  accept=".jpg,.jpeg,image/jpeg"
&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The browser uses that list to guide file selection. It does not convert a photograph or prove that the encoded image is JPEG. As MDN explains, accept is not type validation, and users can often override the picker filter.&lt;/p&gt;

&lt;p&gt;Explain the endpoint's JPEG requirement beside the field, before selection. If conversion is needed, ask for a converted copy while retaining the original. Changing a filename extension does not change its encoding.&lt;/p&gt;

&lt;p&gt;Check the server's upload requirements independently. Give a rejected file type a distinct message from a file-size rejection or another upload error. That tells the user what to fix without treating the picker filter as a guarantee.&lt;/p&gt;

&lt;p&gt;Reference:&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/accept" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/accept&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I work on Morphio (&lt;a href="https://morphio.online/" rel="noopener noreferrer"&gt;https://morphio.online/&lt;/a&gt;). This is a general image-file workflow example, not a claim about a product's implementation.&lt;/p&gt;

</description>
      <category>webdev</category>
    </item>
    <item>
      <title>Map a displayed crop rectangle into intrinsic image coordinates</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Wed, 04 Nov 2026 04:00:15 +0000</pubDate>
      <link>https://dev.to/morphio/map-a-displayed-crop-rectangle-into-intrinsic-image-coordinates-38eb</link>
      <guid>https://dev.to/morphio/map-a-displayed-crop-rectangle-into-intrinsic-image-coordinates-38eb</guid>
      <description>&lt;p&gt;A crop picker often displays an image smaller than its source. If the picker reports a rectangle in displayed coordinates, passing those numbers directly to &lt;code&gt;drawImage()&lt;/code&gt; selects the wrong part of the source. Scale the selection into intrinsic image coordinates first.&lt;/p&gt;

&lt;p&gt;For an &lt;code&gt;HTMLImageElement&lt;/code&gt;, &lt;code&gt;drawImage()&lt;/code&gt; uses intrinsic dimensions. &lt;code&gt;image.width&lt;/code&gt; can describe a displayed size, so use &lt;code&gt;naturalWidth&lt;/code&gt; and &lt;code&gt;naturalHeight&lt;/code&gt; when calculating the source rectangle. See &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage#notes" rel="noopener noreferrer"&gt;MDN's drawImage notes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Assume the entire decoded image is displayed in a 600 by 400 coordinate area. Its intrinsic dimensions are 2400 by 1600. A selection beginning at displayed point (150, 100), with displayed width 300 and height 200, maps to source point (600, 400), width 1200 and height 800.&lt;/p&gt;

&lt;p&gt;This helper performs the scaling. The rectangle must already be relative to the displayed image content, excluding borders, padding, and any letterboxing. All arguments are numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sourceRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;shownWidth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;shownHeight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;shownWidth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;shownHeight&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;isFinite&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;shownWidth&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;shownHeight&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;naturalWidth&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;naturalHeight&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;shownWidth&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;shownHeight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RangeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Selection must be inside a loaded image&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;scaleX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;naturalWidth&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;shownWidth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;scaleY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;naturalHeight&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;shownHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scaleX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scaleY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scaleX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scaleY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the result as the four source arguments in the nine-argument &lt;code&gt;drawImage()&lt;/code&gt; overload. Choose the destination width and height separately; those describe the output canvas region, not the selection in the original image.&lt;/p&gt;

&lt;p&gt;This helper does not translate pointer coordinates or model CSS transforms. A picker using &lt;code&gt;object-fit: contain&lt;/code&gt; must first locate the actual image rectangle inside its element. A rotated or mirrored preview also needs its own inverse mapping. Resolve those layout details before calling the helper.&lt;/p&gt;

&lt;p&gt;Put a different mark near each corner of a test image. Select one corner in the reduced preview and inspect the resulting crop. Then repeat at another corner to catch an incorrect offset.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>html</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Validate a contained pixel rectangle before reading a canvas crop</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Tue, 03 Nov 2026 02:40:26 +0000</pubDate>
      <link>https://dev.to/morphio/validate-a-contained-pixel-rectangle-before-reading-a-canvas-crop-iok</link>
      <guid>https://dev.to/morphio/validate-a-contained-pixel-rectangle-before-reading-a-canvas-crop-iok</guid>
      <description>&lt;p&gt;A crop rectangle needs a stated coordinate space. If an application requires a rectangle fully inside the drawing buffer, check that requirement before calling getImageData().&lt;/p&gt;

&lt;p&gt;The browser API can return transparent black for the part of a requested rectangle outside the canvas. A stricter application rule can reject that rectangle instead. This example accepts only positive, integer-sized rectangles with nonnegative integer origins.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;containedPixelRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;canvasWidth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;canvasHeight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rect&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Expected a rectangle object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;canvasWidth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;canvasHeight&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isSafeInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;canvasWidth&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;canvasHeight&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;canvasWidth&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;canvasHeight&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RangeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rectangle must fit inside the drawing buffer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;containedPixelRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// In browser code, after obtaining a 2D context:&lt;/span&gt;
&lt;span class="c1"&gt;// const pixels = ctx.getImageData(rect.x, rect.y, rect.width, rect.height);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the canvas drawing-buffer dimensions for this check. A rectangle expressed in displayed CSS coordinates needs to be mapped into that coordinate space first. Perform that mapping before using this validator.&lt;/p&gt;

&lt;p&gt;Passing the numeric check also does not grant permission to read a canvas containing restricted cross-origin image content. Handle the actual read failure at the call site. Keep that failure distinct from a rejected rectangle so a report can say which condition stopped the operation.&lt;/p&gt;

&lt;p&gt;I work on &lt;a href="https://morphio.online/" rel="noopener noreferrer"&gt;Morphio&lt;/a&gt;. For an image crop request associated with that project, I would state the coordinate space and the containment rule with the rectangle values.&lt;/p&gt;

&lt;p&gt;API reference: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getImageData" rel="noopener noreferrer"&gt;getImageData()&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Do not treat image complete as a successful decode result</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Mon, 02 Nov 2026 00:32:15 +0000</pubDate>
      <link>https://dev.to/morphio/do-not-treat-image-complete-as-a-successful-decode-result-23pf</link>
      <guid>https://dev.to/morphio/do-not-treat-image-complete-as-a-successful-decode-result-23pf</guid>
      <description>&lt;p&gt;&lt;code&gt;HTMLImageElement.complete&lt;/code&gt; can be &lt;code&gt;true&lt;/code&gt; for a broken image. It can also be true when the element has no source, so it cannot establish that an image loaded successfully.&lt;/p&gt;

&lt;p&gt;For an image-processing step, handle the operation you actually need. If the next step requires decoding, await &lt;code&gt;decode()&lt;/code&gt; and handle rejection before replacing the preview. Keep the selected job identifier available when your application allows another file to be chosen during that wait.&lt;/p&gt;

&lt;p&gt;The following is an illustrative boundary around an existing image element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;requireDecodedImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Handle rejection in the caller. A rejected promise should leave the previous preview intact when that is the intended interface behavior. Do not label an export ready merely because an earlier &lt;code&gt;complete&lt;/code&gt; check returned true.&lt;/p&gt;

&lt;p&gt;Keep load or decode status separate from the later canvas drawing and export result. Successful decoding does not establish that every subsequent operation will succeed. Name the failing stage in the interface so a missing image and a failed export are distinguishable.&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/complete" rel="noopener noreferrer"&gt;MDN complete&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>html</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Label File.lastModified as a file timestamp in an image list</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Sun, 01 Nov 2026 02:43:29 +0000</pubDate>
      <link>https://dev.to/morphio/label-filelastmodified-as-a-file-timestamp-in-an-image-list-3c3d</link>
      <guid>https://dev.to/morphio/label-filelastmodified-as-a-file-timestamp-in-an-image-list-3c3d</guid>
      <description>&lt;p&gt;Keep the label for &lt;code&gt;File.lastModified&lt;/code&gt; specific: it is a file modification timestamp. A photograph's capture time needs a separate source.&lt;/p&gt;

&lt;p&gt;The value is a number of milliseconds since the Unix epoch. You can format it with &lt;code&gt;new Date(file.lastModified).toISOString()&lt;/code&gt; when a UTC display suits the interface. Keep the numeric value in the job record if later comparisons depend on it.&lt;/p&gt;

&lt;p&gt;A file created in JavaScript can receive an explicit &lt;code&gt;lastModified&lt;/code&gt; option. When that option is omitted, it is normally assigned the current time at creation; this makes it unsuitable as proof of when the image was photographed. A file without a known modification date can also report the current date.&lt;/p&gt;

&lt;p&gt;Keep any capture date under a separate field with its own source and uncertainty. Leave a missing capture date empty instead of substituting the file timestamp. If the user is comparing two exports, identify which timestamp was inspected and which file it belongs to.&lt;/p&gt;

&lt;p&gt;I work on &lt;a href="https://morphio.online/" rel="noopener noreferrer"&gt;Morphio&lt;/a&gt; and write about browser image workflows. This distinction concerns the file record and does not claim that a conversion tool preserves or reads camera metadata. Reference: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/File/lastModified" rel="noopener noreferrer"&gt;MDN File.lastModified&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Let the browser set the multipart boundary for an image upload</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Sat, 31 Oct 2026 07:41:15 +0000</pubDate>
      <link>https://dev.to/morphio/let-the-browser-set-the-multipart-boundary-for-an-image-upload-444f</link>
      <guid>https://dev.to/morphio/let-the-browser-set-the-multipart-boundary-for-an-image-upload-444f</guid>
      <description>&lt;p&gt;Leave &lt;code&gt;Content-Type&lt;/code&gt; unset in the request options when &lt;code&gt;fetch()&lt;/code&gt; sends &lt;code&gt;FormData&lt;/code&gt;. The browser adds the multipart boundary needed by that body.&lt;/p&gt;

&lt;p&gt;Build the form with &lt;code&gt;const data = new FormData(); data.append("image", file);&lt;/code&gt; and pass it as the request body. A manually supplied &lt;code&gt;Content-Type: multipart/form-data&lt;/code&gt; omits the boundary that the browser normally adds. Apply this rule to requests whose body is &lt;code&gt;FormData&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Check a shared request helper for an unconditional JSON or multipart header before blaming the image bytes. Keep any authorization headers your application actually requires. Inspect the request and the server's response using a small non-sensitive test image.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest_API/Using_FormData_Objects" rel="noopener noreferrer"&gt;MDN FormData guide&lt;/a&gt; explains the boundary requirement. In a bug report, identify the body type and header construction without copying credentials into the report.&lt;/p&gt;

&lt;p&gt;I work on Morphio, our image project at &lt;a href="https://morphio.online/" rel="noopener noreferrer"&gt;https://morphio.online/&lt;/a&gt;. These notes describe image preparation and review; they do not require a particular editing tool.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Keep the filename separate when appending a Blob to FormData</title>
      <dc:creator>David Miller</dc:creator>
      <pubDate>Fri, 30 Oct 2026 03:47:49 +0000</pubDate>
      <link>https://dev.to/morphio/keep-the-filename-separate-when-appending-a-blob-to-formdata-5hj7</link>
      <guid>https://dev.to/morphio/keep-the-filename-separate-when-appending-a-blob-to-formdata-5hj7</guid>
      <description>&lt;p&gt;Keep a Blob's uploaded filename explicit in FormData. A plain Blob has no File name, so supply the name as append()'s third argument. The Blob contains data and a type, but has no File name.&lt;/p&gt;

&lt;p&gt;The third argument to append() supplies the name reported to the server. For example, form.append("image", blob, "preview.png") names the uploaded part. The default name for a plain Blob is "blob"; a File uses its own filename unless another is supplied.&lt;/p&gt;

&lt;p&gt;Choose the extension from the output format you actually produced. Giving the part a .png name does not convert its bytes. When reading the server's upload report, check the field name image and filename preview.png separately.&lt;/p&gt;

&lt;p&gt;Test with a small sample and inspect the name the server receives. Keep server-side validation in place; a supplied filename is descriptive metadata.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/FormData/append" rel="noopener noreferrer"&gt;MDN FormData.append()&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
