<?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: can4hou6joeng4</title>
    <description>The latest articles on DEV Community by can4hou6joeng4 (@can4hou6joeng4).</description>
    <link>https://dev.to/can4hou6joeng4</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%2F4061684%2F8fa5343c-5090-496a-b58a-3f43205b8fcd.jpg</url>
      <title>DEV Community: can4hou6joeng4</title>
      <link>https://dev.to/can4hou6joeng4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/can4hou6joeng4"/>
    <language>en</language>
    <item>
      <title>How I built a local image-to-ASCII converter with TypeScript and Canvas</title>
      <dc:creator>can4hou6joeng4</dc:creator>
      <pubDate>Tue, 04 Aug 2026 06:01:49 +0000</pubDate>
      <link>https://dev.to/can4hou6joeng4/how-i-built-a-local-image-to-ascii-converter-with-typescript-and-canvas-2fpa</link>
      <guid>https://dev.to/can4hou6joeng4/how-i-built-a-local-image-to-ascii-converter-with-typescript-and-canvas-2fpa</guid>
      <description>&lt;p&gt;Most image-to-ASCII tools begin with the same request: upload your image.&lt;/p&gt;

&lt;p&gt;That is a poor default for screenshots, private photos, diagrams, and anything else you would rather keep on your own device. I built &lt;a href="https://semaphore.bobochang.cn/" rel="noopener noreferrer"&gt;Semaphore&lt;/a&gt; so the complete conversion and export pipeline stays inside the browser tab.&lt;/p&gt;

&lt;p&gt;This post explains the small TypeScript and Canvas pipeline behind it, the reason braille characters preserve more detail, and how the deployed site enforces its privacy claim.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pipeline
&lt;/h2&gt;

&lt;p&gt;Semaphore deliberately has no application framework and no runtime dependency. The conversion path is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image -&amp;gt; canvas sampling -&amp;gt; luminance grid -&amp;gt; character mapping -&amp;gt; text or PNG
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser decodes the selected file, an offscreen canvas resamples it to the requested output dimensions, and &lt;code&gt;getImageData()&lt;/code&gt; supplies the RGBA pixels. The engine adjusts brightness and contrast, optionally inverts the channels, and computes luminance for each sample:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;luminance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.2126&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;red&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.7152&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;green&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.0722&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a conventional charset, the luminance value selects a character from a dark-to-light ramp. Semaphore includes six presets:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;standard&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;detailed&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;blocks&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;minimal&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;binary&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;braille&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first five use ramps of different sizes. For example, the standard ramp is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; .:-=+*#%@
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why braille retains more detail
&lt;/h2&gt;

&lt;p&gt;Unicode braille characters are miniature 2 by 4 dot matrices. Instead of taking one luminance sample per output character, Semaphore samples two columns by four rows and packs the eight thresholded dots into one code point beginning at &lt;code&gt;U+2800&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At the same text width, every braille cell therefore represents eight source samples. The output does not need extra terminal columns to preserve finer spatial structure.&lt;/p&gt;

&lt;p&gt;Binary thresholding can make gradients look harsh, so braille mode optionally applies Floyd-Steinberg error diffusion before packing the dots. Each threshold error is distributed to neighboring samples using the familiar &lt;code&gt;7/16&lt;/code&gt;, &lt;code&gt;3/16&lt;/code&gt;, &lt;code&gt;5/16&lt;/code&gt;, and &lt;code&gt;1/16&lt;/code&gt; weights. Dithering is intentionally limited to braille; the other presets already have multi-level character ramps.&lt;/p&gt;

&lt;p&gt;Try the dedicated &lt;a href="https://semaphore.bobochang.cn/charsets/braille" rel="noopener noreferrer"&gt;image-to-braille example&lt;/a&gt;, or open the main tool and switch charsets while the image is live.&lt;/p&gt;

&lt;h2&gt;
  
  
  Local files without a lingering object URL
&lt;/h2&gt;

&lt;p&gt;The selected file never needs a server URL. &lt;code&gt;fileToImage()&lt;/code&gt; creates a temporary object URL, lets the browser decode the image, and revokes the URL immediately on either success or failure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fileToImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;File&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLImageElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&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;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Image&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;onload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;revokeObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;resolve&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="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;revokeObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;unsupported image file&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="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&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 decoded image remains drawable on the canvas, but the temporary blob reference is released as soon as it is no longer needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Text, color, and PNG export
&lt;/h2&gt;

&lt;p&gt;Plain output is just text. When original or grayscale color is enabled, consecutive characters with the same quantized color are grouped into &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; runs instead of creating one element per character.&lt;/p&gt;

&lt;p&gt;PNG export uses another canvas. It measures the monospace advance width, draws each text row, and returns a blob through &lt;code&gt;canvas.toBlob()&lt;/code&gt;. The UI also supports copying plain text, downloading &lt;code&gt;.txt&lt;/code&gt;, and producing a share card.&lt;/p&gt;

&lt;h2&gt;
  
  
  Privacy enforced by the deployed page
&lt;/h2&gt;

&lt;p&gt;Keeping conversion code in the browser is only part of the guarantee. The production response also sends a Content Security Policy containing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;connect-src 'none'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That prevents page scripts from opening &lt;code&gt;fetch&lt;/code&gt;, XHR, or WebSocket connections. Semaphore has no backend API, no client-side analytics script, no cookies, and no third-party runtime request. JetBrains Mono is served from the same site as a self-hosted subset.&lt;/p&gt;

&lt;p&gt;Edge servers still receive ordinary requests for the static HTML, JavaScript, CSS, fonts, and sample assets. The selected image bytes never enter those requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it and inspect the source
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Live tool: &lt;a href="https://semaphore.bobochang.cn/" rel="noopener noreferrer"&gt;semaphore.bobochang.cn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Source: &lt;a href="https://github.com/can4hou6joeng4/Semaphore" rel="noopener noreferrer"&gt;github.com/can4hou6joeng4/Semaphore&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The project is MIT licensed. Feedback on the conversion output, charsets, accessibility, or export formats is welcome.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
