<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rik Schennink on Medium]]></title>
        <description><![CDATA[Stories by Rik Schennink on Medium]]></description>
        <link>https://medium.com/@rikschennink?source=rss-d5baff86691------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*iq4WSlSRWke38EKK_UzPkg.jpeg</url>
            <title>Stories by Rik Schennink on Medium</title>
            <link>https://medium.com/@rikschennink?source=rss-d5baff86691------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 00:26:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rikschennink/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Integrating Uppy with Doka a JavaScript Image Editor]]></title>
            <link>https://itnext.io/integrating-uppy-with-doka-a-javascript-image-editor-2bd8821f6adc?source=rss-d5baff86691------2</link>
            <guid isPermaLink="false">https://medium.com/p/2bd8821f6adc</guid>
            <category><![CDATA[webdesing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[webdev]]></category>
            <dc:creator><![CDATA[Rik Schennink]]></dc:creator>
            <pubDate>Mon, 17 Jun 2019 19:03:33 GMT</pubDate>
            <atom:updated>2019-06-17T19:03:33.561Z</atom:updated>
            <content:encoded><![CDATA[<h4>Transloadit recently released the 1.0 version of their Uppy file uploader. In this tutorial we’ll integrate Uppy with Doka to visually optimise and compress image data before it’s uploaded to the server.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1d2Q-mUpjJFb8OcHr-cj8g.png" /><figcaption>Doka Image Editor</figcaption></figure><p>Compressing and cropping images before they’re send to the server can save bandwidth, save manual cropping work, and allows the user to have more control over their own content. Use the techniques described in this article to enable your users to edit profile pictures, photo albums, or any other imagery that they upload to your server. All while still being able to set content restrictions, like crop aspect ratio, minimum image size, and available filters.</p><p>We’ll start with the default <a href="https://uppy.io/">Uppy</a> implementation. Once we have that up and running we’ll add the <a href="https://pqina.nl/doka/">Doka</a> integration.</p><h3>Setting up Uppy</h3><p>Let’s begin by adding the Uppy stylesheet and script to the page.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>    &lt;meta charset=&quot;UTF-8&quot;&gt;<br>    &lt;title&gt;Uppy&lt;/title&gt;</pre><pre>    <strong>&lt;!-- add the Uppy styles to the head --&gt;<br>    &lt;link href=&quot;</strong><a href="https://transloadit.edgly.net/releases/uppy/v1.2.0/uppy.min.css"><strong>https://transloadit.edgly.net/releases/uppy/v1.2.0/uppy.min.css</strong></a><strong>&quot; rel=&quot;stylesheet&quot;&gt;</strong><br>  <br>&lt;/head&gt;<br>&lt;body&gt;</pre><pre>    <strong>&lt;!-- add the Uppy script to right before the closing body element --&gt;<br>    &lt;script src=&quot;</strong><a href="https://transloadit.edgly.net/releases/uppy/v1.2.0/uppy.min.js"><strong>https://transloadit.edgly.net/releases/uppy/v1.2.0/uppy.min.js</strong></a><strong>&quot;&gt;&lt;/script&gt;</strong></pre><pre>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>Now we’ll add the HTML element that will contain the Uppy instance.</p><p>Note that I’ve shortened the style and script tags to keep the code snippet readable.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>    &lt;meta charset=&quot;UTF-8&quot;&gt;<br>    &lt;title&gt;Uppy&lt;/title&gt;<br>    &lt;link href=&quot;uppy.min.css&quot; rel=&quot;stylesheet&quot;&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    <strong>&lt;!-- add the Uppy HTML element --&gt;<br>    &lt;div id=&quot;drag-drop-area&quot;&gt;&lt;/div&gt;</strong></pre><pre>    &lt;script src=&quot;uppy.min.js&quot;&gt;&lt;/script&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>All right, let’s extend the script to load Uppy.</p><pre>&lt;body&gt;<br>    &lt;div id=&quot;drag-drop-area&quot;&gt;&lt;/div&gt;<br>    &lt;script src=&quot;uppy.min.js&quot;&gt;&lt;/script&gt;</pre><pre>    <strong>&lt;!-- add the Uppy script init logic --&gt;<br>    &lt;script&gt;<br>      var uppy = Uppy.Core()<br>        .use(Uppy.Dashboard, {<br>          inline: true,<br>          target: &#39;#drag-drop-area&#39;<br>        })<br>        .use(Uppy.Tus, {endpoint: &#39;</strong><a href="https://master.tus.io/files/&#39;"><strong>https://master.tus.io/files/&#39;</strong></a><strong>})</strong></pre><pre><strong>uppy.on(&#39;complete&#39;, (result) =&gt; {<br>        console.log(&#39;Upload complete! We’ve uploaded these files:&#39;, result.successful)<br>      })<br>    &lt;/script&gt;</strong><br>&lt;/body&gt;</pre><p>Refresh your browser window and Uppy should now appear like shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*_esqyyHeQa0UhShoDPrI3Q.png" /></figure><p>You can drop files and upload them as well, it’ll all work automagically.</p><p>Let’s move on to the Doka integration.</p><h3>Integrating Doka with Uppy</h3><p>To allow editing of the image we need to load Doka when an image is dropped. We can do this by leveraging the onBeforeFileAdded function.</p><p>First however we’ll need to add the Doka scripts and styles.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>    &lt;meta charset=&quot;UTF-8&quot;&gt;<br>    &lt;title&gt;Uppy&lt;/title&gt;<br>    &lt;link href=&quot;uppy.min.css&quot; rel=&quot;stylesheet&quot;&gt;</pre><pre><strong>    &lt;!-- add the Doka style tag --&gt;<br>    &lt;link href=&quot;doka.min.css&quot; rel=&quot;stylesheet&quot;&gt;</strong><br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;div id=&quot;drag-drop-area&quot;&gt;&lt;/div&gt;<br>    &lt;script src=&quot;uppy.min.js&quot;&gt;&lt;/script&gt;</pre><pre>    <strong>&lt;!-- add the Doka script tag --&gt;<br>    &lt;script src=&quot;doka.min.js&quot;&gt;&lt;/script&gt;</strong><br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>Now we’ll link up with the Uppy onBeforeFileAdded function. We’ll zoom in on the JavaScript code to make things easier to read.</p><p>The onBeforeFileAdded function allows us to validate the dropped file. If we return false the file will be rejected, if we return true, it will be added to the list. We need to edit the file and then add the result of the user edit to the list of files.</p><p>What we need to do:</p><ol><li>Prevent the original file from being added</li><li>Edit the original file with Doka</li><li>Add the file returned by Doka to the list</li></ol><p>Let’s start by preventing the file from being added.</p><pre><strong>// our uppy options object<br>var uppyOptions = {<br>    onBeforeFileAdded: function(file) {</strong></pre><pre><strong>        // prevent the file from being added<br>        return false;</strong></pre><pre><strong>    }<br>}</strong></pre><pre>// initialise Uppy<br>var uppy = Uppy.Core(<strong>uppyOptions</strong>)<br>    .use(Uppy.Dashboard, {<br>        inline: true,<br>        target: &#39;#drag-drop-area&#39;<br>    })<br>    .use(Uppy.Tus, {endpoint: &#39;<a href="https://master.tus.io/files/&#39;">https://master.tus.io/files/&#39;</a>})</pre><p>Now we need to edit the file we just rejected with Doka. We’ll zoom in on the uppyOptions object.</p><p>We create our Doka instance and then we edit the file data with Doka.</p><pre><strong>// create our Doka instance<br>var doka = Doka.create({<br>    utils: [&#39;crop&#39;, &#39;filter&#39;, &#39;color&#39;]<br>});</strong></pre><pre>var uppyOptions = {<br>    onBeforeFileAdded: function(file) {</pre><pre><strong>        // edit the file data with Doka<br>        doka.edit(file.data);</strong></pre><pre>        return false;<br>    }<br>}</pre><p>Great, the Doka image editor now opens when dropping a file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*vS8RBC2KhLCL_cy-N7efIQ.png" /></figure><p>The Doka.edit function returns a Promise which is resolved with a file object when the Done button is pressed in the Doka editor. When this happens we want to return the resulting file to Uppy.</p><pre>var uppyOptions = {<br>    onBeforeFileAdded: function(file) {</pre><pre>        doka.edit(file.data).then(output =&gt; {</pre><pre>            <strong>// add the Doka edit result back to Uppy<br>            uppy.addFile({<br>                ...file,<br>                data: output.file<br>            });</strong><br>            <br>        });</pre><pre>        return false;<br>    }<br>}</pre><p>That’s all fine, but now we’re running in circles. When the file is edited by Doka the output file is added to Uppy and again, is edited by Doka, and this continues untill there are no more bytes left to edit. Not good.</p><p>We need to tell Uppy that the file that is being added has already been edited. Let’s do this by adding a special property to the file.</p><pre>var uppyOptions = {<br>    onBeforeFileAdded: function(file) {</pre><pre>        <strong>// only allow file if special boolean property is present<br>        if (file.handledByDoka) return true;</strong></pre><pre>        doka.edit(file.data).then(output =&gt; {<br>            uppy.addFile({<br>                ...file,<br>                data: output.file,<br>                <br>                <strong>// add a special boolean to the file<br>                handledByDoka: true</strong><br>            });<br>        });</pre><pre>        return false;<br>    }<br>}</pre><p>That’s it!</p><p>When a user drops an image the image is now sent to Doka, the user can then edit the image and the resulting data is sent to Uppy. We remember the edit by setting the handledByDoka property to true.</p><p>By setting the crop aspect ratio, minimum image size, available utilities you can now control how much the user is allowed to edit the image data before it is uploaded to your server.</p><p>Please note that the example above doesn’t handle multiple dropped files, this would trigger multiple image edits at once. The Doka library ships with a handy useDokaWithUppy function that takes care of queueing the images so they nicely wait their turn to be edited.</p><p>Using this function the JavaScript code snippet now looks like this:</p><pre>// connect with `useDokaWithUppy` and pass Doka properties<br>var uppyOptions = {<br>    <strong>onBeforeFileAdded: useDokaWithUppy({<br>        utils: [&#39;crop&#39;, &#39;filter&#39;, &#39;color&#39;]<br>    })</strong><br>};</pre><pre>// initialise Uppy<br>var uppy = Uppy.Core(uppyOptions)<br>    .use(Uppy.Dashboard, {<br>        inline: true,<br>        target: &#39;#drag-drop-area&#39;<br>    })<br>    .use(Uppy.Tus, {endpoint: &#39;<a href="https://master.tus.io/files/&#39;">https://master.tus.io/files/&#39;</a>})</pre><p>You can now use the Uppy file upload library together with Doka to handle image uploads.</p><p>See the results below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*Eqac_NOrJFTzizOGxwOSJA.gif" /></figure><p><a href="https://pqina.nl/doka/">Find out more about Doka on the product site</a></p><p>If you’re using DropZoneJS you might also be interested in <a href="https://itnext.io/integrating-dropzone-with-javascript-image-cropper-optimise-image-upload-e22b12ac0d8a">Integrating DropzoneJS with a JavaScript Image Cropper to Optimise Image Uploads</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2bd8821f6adc" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/integrating-uppy-with-doka-a-javascript-image-editor-2bd8821f6adc">Integrating Uppy with Doka a JavaScript Image Editor</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integrating DropzoneJS with a JavaScript Image Cropper to Optimise Image Uploads]]></title>
            <link>https://itnext.io/integrating-dropzone-with-javascript-image-cropper-optimise-image-upload-e22b12ac0d8a?source=rss-d5baff86691------2</link>
            <guid isPermaLink="false">https://medium.com/p/e22b12ac0d8a</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[image]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[image-editing]]></category>
            <dc:creator><![CDATA[Rik Schennink]]></dc:creator>
            <pubDate>Thu, 03 Jan 2019 15:29:37 GMT</pubDate>
            <atom:updated>2019-01-22T18:42:01.175Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial we’ll integrate Dropzone with three different image cropper plugins to optimise image data before it’s uploaded to the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*atO19Z0pHrGmOgRdqIGbSQ.jpeg" /></figure><p>Wouldn’t it be nice if we could optimise images before they’re sent to the server. This can save bandwidth, result in a cleaner database, and allows the user to have more control over their own content. Use the techniques described in this article to enable your users to edit profile pictures, photo albums, or any other imagery that they upload to your server.</p><p>We’ll set up a basic <a href="https://www.dropzonejs.com/"><strong>DropzoneJS</strong></a> vanilla JavaScript drop area, extend it to better integrate with a third party image cropper and then show how to link it up with three popular image croppers: <a href="https://github.com/fengyuanchen/cropperjs"><strong>Cropper.js</strong></a>, <a href="https://github.com/foliotek/croppie"><strong>Croppie</strong></a>, and last but not least <a href="https://pqina.nl/doka/"><strong>Doka</strong></a></p><p>Let’s start by setting up DropzoneJS first.</p><h3>Setting up DropzoneJS</h3><p>We’ll create a basic HTML page and add the DropzoneJS styles and scripts like shown below.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>  &lt;meta charset=&quot;UTF-8&quot;&gt;<br>  &lt;title&gt;JavaScript Image Cropping with DropzoneJS&lt;/title&gt;<br>  <strong>&lt;link href=&quot;https://unpkg.com/dropzone/dist/dropzone.css&quot; rel=&quot;stylesheet&quot;/&gt;</strong><br>&lt;/head&gt;<br>&lt;body&gt;<br>  <strong>&lt;div class=&quot;dropzone&quot; id=&quot;myDropzone&quot;&gt;&lt;/div&gt;</strong><br>  <strong>&lt;script src=&quot;https://unpkg.com/dropzone&quot;&gt;&lt;/script&gt;</strong><br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>With our basic page set up, we can now focus on configuring DropzoneJS. Before it’s going to do anything it wants us to set up a location for uploading the files to, let’s do that now. We can pass options to our specific uploader using the id we assigned to the DropzoneJS &lt;div&gt;.</p><p><em>Note that we’ve zoomed in on the code just before the closing </em><em>body tag.</em></p><pre>&lt;div class=&quot;dropzone&quot; id=&quot;myDropzone&quot;&gt;&lt;/div&gt;<br>&lt;script src=&quot;https://unpkg.com/dropzone&quot;&gt;&lt;/script&gt;</pre><pre><strong>&lt;script&gt;<br>Dropzone.options.myDropzone = {<br> url: &#39;/post&#39;<br>};<br>&lt;/script&gt;</strong></pre><p>We should now see the following appear on the page, and if we drop an image DropzoneJS will show a small thumbnail.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*sTcXQ-aM2zLTUeDjjTSOWA.gif" /></figure><p>We can handle the file object on the server just as we would a normal file posted using a &lt;input type=”file”/&gt; with a classic multi-part form. This tutorial will focus on editing the image on the front-end before it’s uploaded to the server. So we’ll leave the server solution be for now.</p><h3>Extending Dropzone it with an image editor popup</h3><p>Dropzone has a special callback function called transformFile we can ask our users to edit a file when this function is called. Let’s</p><p><em>Note that we’ve zoomed in on the code inside the </em><em>&lt;script&gt; tag.</em></p><pre>Dropzone.options.myDropzone = {<br>  url: &#39;/post&#39;,<br>  <strong>transformFile: function(file, done) { </strong></pre><pre><strong>  }</strong><br>};</pre><p>Dropzone locks up. We need to call the done function (second parameter) when we’re done editing the file, we’ll keep this in mind. For now this is fine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*HSDOae5oZMFvlDRGVfDn1w.gif" /></figure><p>Alright, we want to edit the file in an image editor overlay, on top of Dropzone, this automatically moves the user focus to the editor.</p><pre>transformFile: function(file, done) {</pre><pre>  // Create the image editor overlay<br>  <strong>var editor = document.createElement(&#39;div&#39;);<br>  editor.style.position = &#39;fixed&#39;;<br>  editor.style.left = 0;<br>  editor.style.right = 0;<br>  editor.style.top = 0;<br>  editor.style.bottom = 0;<br>  editor.style.zIndex = 9999;<br>  editor.style.backgroundColor = &#39;#000&#39;;<br>  document.body.appendChild(editor);</strong></pre><pre>}</pre><p>The result below seems all kinds of wrong but it’s actually what we want. The black layer will be the location of our editor later on. Hang in there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*fmQSsmXpciiS32creN6rRg.gif" /></figure><p>We need a way to escape from this black void, so let’s add a button with a click event to the editor element.</p><pre>transformFile: function(file, done) {</pre><pre>  // Create the image editor overlay<br>  var editor = document.createElement(&#39;div&#39;);<br>  editor.style.position = &#39;fixed&#39;;<br>  editor.style.left = 0;<br>  editor.style.right = 0;<br>  editor.style.top = 0;<br>  editor.style.bottom = 0;<br>  editor.style.zIndex = 9999;<br>  editor.style.backgroundColor = &#39;#000&#39;;<br>  document.body.appendChild(editor);</pre><pre>  // Create confirm button at the top left of the viewport<br>  <strong>var buttonConfirm = document.createElement(&#39;button&#39;);<br>  buttonConfirm.style.position = &#39;absolute&#39;;<br>  buttonConfirm.style.left = &#39;10px&#39;;<br>  buttonConfirm.style.top = &#39;10px&#39;;<br>  buttonConfirm.style.zIndex = 9999;<br>  buttonConfirm.textContent = &#39;Confirm&#39;;<br>  editor.appendChild(buttonConfirm);<br>  buttonConfirm.addEventListener(&#39;click&#39;, function() {</strong></pre><pre>    // Remove the editor from the view<br>    <strong>document.body.removeChild(editor);</strong></pre><pre>  <strong>});</strong></pre><pre>}</pre><p>Yay! We can now escape the void. Wonderful!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*x85rH-odZww7iQumQ5OCEw.gif" /></figure><p>The is all we need to set up to integrate with our first and second image cropper candidates, let’s give Cropper.js a go.</p><h3>Integrating Dropzone with Cropper.js</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*9lrxxiFyoHt0v5GWbYMwZg.png" /></figure><p>Time to link up our first image cropper. Let’s add the styles and scripts required for <a href="https://github.com/fengyuanchen/cropperjs">Cropper.js</a>.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>  &lt;meta charset=&quot;UTF-8&quot;&gt;<br>  &lt;title&gt;JavaScript Image Cropping with DropzoneJS&lt;/title&gt;<br>  &lt;link href=&quot;https://unpkg.com/dropzone/dist/dropzone.css&quot; rel=&quot;stylesheet&quot;/&gt;<br>  <strong>&lt;link href=&quot;https://unpkg.com/cropperjs/dist/cropper.css&quot; rel=&quot;stylesheet&quot;/&gt;</strong><br>&lt;/head&gt;<br>&lt;body&gt;<br>  &lt;div class=&quot;dropzone&quot; id=&quot;myDropzone&quot;&gt;&lt;/div&gt;<br>  &lt;script src=&quot;https://unpkg.com/dropzone&quot;&gt;&lt;/script&gt;<br>  <strong>&lt;script src=&quot;https://unpkg.com/cropperjs&quot;&gt;&lt;/script&gt;<br></strong>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>Everything is still the same, and that’s okay.</p><p>Let‘s add Cropper.js to our editor element and load the dropped image. Cropper.js expects an &lt;img&gt; element, so let’s turn the file object into a URL and feed it to a new image.</p><p><em>Note that we’ve zoomed in on the </em><em>transformFile function itself.</em></p><pre>transformFile: function(file, done) {</pre><pre>  // Create the image editor overlay<br>  var editor = document.createElement(&#39;div&#39;);<br>  editor.style.position = &#39;fixed&#39;;<br>  editor.style.left = 0;<br>  editor.style.right = 0;<br>  editor.style.top = 0;<br>  editor.style.bottom = 0;<br>  editor.style.zIndex = 9999;<br>  editor.style.backgroundColor = &#39;#000&#39;;<br>  document.body.appendChild(editor);</pre><pre>  // Create confirm button at the top left of the viewport<br>  var buttonConfirm = document.createElement(&#39;button&#39;);<br>  buttonConfirm.style.position = &#39;absolute&#39;;<br>  buttonConfirm.style.left = &#39;10px&#39;;<br>  buttonConfirm.style.top = &#39;10px&#39;;<br>  buttonConfirm.style.zIndex = 9999;<br>  buttonConfirm.textContent = &#39;Confirm&#39;;<br>  editor.appendChild(buttonConfirm);<br>  buttonConfirm.addEventListener(&#39;click&#39;, function() {</pre><pre>    // Remove the editor from the view<br>    document.body.removeChild(editor);</pre><pre><strong>  </strong>});</pre><pre>  // Create an image node for Cropper.js<br><strong>  var image = new Image();<br>  image.src = URL.createObjectURL(file);<br>  editor.appendChild(image);<br>  <br>  </strong>// Create Cropper.js<br>  <strong>var cropper = new Cropper(image, { aspectRatio: 1 });</strong></pre><pre>}</pre><p>Cropper.js now opens dropped images in the editor popup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*S9SHM4izvzaYzwFTA8ysIw.gif" /></figure><p>When we click our button the editor is closed. We want it to tell Cropper.js to make a crop and update the DropzoneJS file.</p><p><em>Note that we’ve zoomed in on the confirm button addEventListener function.</em></p><pre>buttonConfirm.addEventListener(&#39;click&#39;, function() {</pre><pre>  // Get the canvas with image data from Cropper.js<br>  <strong>var canvas = cropper.getCroppedCanvas({<br>    width: 256,<br>    height: 256<br>  });</strong></pre><pre>  // Turn the canvas into a Blob (file object without a name)<br>  <strong>canvas.toBlob(function(blob) {</strong></pre><pre><strong>    </strong>// Return the file to Dropzone<strong><br>    done(blob);</strong></pre><pre>  <strong>});</strong></pre><pre>  // Remove the editor from the view<strong><br>  </strong>document.body.removeChild(editor);</pre><pre>});</pre><p>Weird! The file thumbnail is not updated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*FrJeY_5c1al2EflYnlRaQQ.gif" /></figure><p>Dropzone does not automatically create new thumbnails. We’ll have to instruct it to also redraw it based on the updated file. We can do this using the Dropzone createThumbnail function. But as we need to access the Dropzone instance, we’ll also have to create a reference to the instance (we’ll call it myDropZone).</p><pre>transformFile: function(file, done) {</pre><pre>  // Create Dropzone reference for use in confirm button click handler<br>  <strong>var myDropZone = this;</strong></pre><pre>  // Create the image editor overlay<br>  var editor = document.createElement(&#39;div&#39;);<br>  editor.style.position = &#39;fixed&#39;;<br>  editor.style.left = 0;<br>  editor.style.right = 0;<br>  editor.style.top = 0;<br>  editor.style.bottom = 0;<br>  editor.style.zIndex = 9999;<br>  editor.style.backgroundColor = &#39;#000&#39;;<br>  document.body.appendChild(editor);</pre><pre>  // Create confirm button at the top left of the viewport<br>  var buttonConfirm = document.createElement(&#39;button&#39;);<br>  buttonConfirm.style.position = &#39;absolute&#39;;<br>  buttonConfirm.style.left = &#39;10px&#39;;<br>  buttonConfirm.style.top = &#39;10px&#39;;<br>  buttonConfirm.style.zIndex = 9999;<br>  buttonConfirm.textContent = &#39;Confirm&#39;;<br>  editor.appendChild(buttonConfirm);<br>  buttonConfirm.addEventListener(&#39;click&#39;, function() {</pre><pre>    // Get the canvas with image data from Cropper.js<br>    var canvas = cropper.getCroppedCanvas({<br>      width: 256,<br>      height: 256<br>    });</pre><pre>    // Turn the canvas into a Blob (file object without a name)<br>    canvas.toBlob(function(blob) {</pre><pre>      // Create a new Dropzone file thumbnail<br>      <strong>myDropZone.createThumbnail(<br>        blob,<br>        myDropZone.options.thumbnailWidth,<br>        myDropZone.options.thumbnailHeight,<br>        myDropZone.options.thumbnailMethod,<br>        false, <br>        function(dataURL) {<br>          <br>          </strong>// Update the Dropzone file thumbnail<strong><br>          myDropZone.emit(&#39;thumbnail&#39;, file, dataURL);</strong></pre><pre><strong>          </strong>// Return the file to Dropzone<strong><br>          done(blob);</strong></pre><pre><strong>      });</strong></pre><pre>    });</pre><pre>    // Remove the editor from the view<strong><br>    </strong>document.body.removeChild(editor);</pre><pre>  });</pre><pre>  // Create an image node for Cropper.js<br><strong>  </strong>var image = new Image();<br>  image.src = URL.createObjectURL(file);<br>  editor.appendChild(image);<strong><br>  <br>  </strong>// Create Cropper.js<br>  var cropper = new Cropper(image, { aspectRatio: 1 });</pre><pre>}</pre><p>That’s it!</p><p>See the animation below for the resulting solution or <a href="https://codepen.io/rikschennink/pen/de53582f0ce6d723a47e0a99716a8cfd">play around with the code on CodePen</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*gzlT7cUFz11VoDquaWpK6w.gif" /><figcaption>DropzoneJS and Cropper.js integration</figcaption></figure><h3>Linking up Croppie</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*vzRM_tryTAG0Y4yoLp6Euw.png" /></figure><p>A lot of the code required to link DropzoneJS and <a href="https://github.com/foliotek/croppie">Croppie</a> is the same as we’ve just seen with Cropper.js. We’ll first have to switch the styles and scripts to point to the Croppie source files.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>  &lt;meta charset=&quot;UTF-8&quot;&gt;<br>  &lt;title&gt;JavaScript Image Cropping with DropzoneJS&lt;/title&gt;<br>  &lt;link href=&quot;https://unpkg.com/dropzone/dist/dropzone.css&quot; rel=&quot;stylesheet&quot;/&gt;<br>  <strong>&lt;link href=&quot;https://unpkg.com/croppie/croppie.css&quot; rel=&quot;stylesheet&quot;/&gt;</strong><br>&lt;/head&gt;<br>&lt;body&gt;<br>  &lt;div class=&quot;dropzone&quot; id=&quot;myDropzone&quot;&gt;&lt;/div&gt;<br>  &lt;script src=&quot;https://unpkg.com/dropzone&quot;&gt;&lt;/script&gt;<br>  <strong>&lt;script src=&quot;https://unpkg.com/croppie&quot;&gt;&lt;/script&gt;</strong><br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>With the correct files loaded, we can zoom in on the transformFile function. Croppie can load a file object without first creating an image, it expects a URL.</p><pre>function(file, done) {</pre><pre>  // Create Dropzone reference for use in confirm button click handler<br>  var myDropZone = this;</pre><pre>  // Create the image editor overlay<br>  var editor = document.createElement(&#39;div&#39;);<br>  editor.style.position = &#39;fixed&#39;;<br>  editor.style.left = 0;<br>  editor.style.right = 0;<br>  editor.style.top = 0;<br>  editor.style.bottom = 0;<br>  editor.style.zIndex = 9999;<br>  editor.style.backgroundColor = &#39;#000&#39;;<br>  document.body.appendChild(editor);</pre><pre>  // Create confirm button at the top left of the viewport<br>  var buttonConfirm = document.createElement(&#39;button&#39;);<br>  buttonConfirm.style.position = &#39;absolute&#39;;<br>  buttonConfirm.style.left = &#39;10px&#39;;<br>  buttonConfirm.style.top = &#39;10px&#39;;<br>  buttonConfirm.style.zIndex = 9999;<br>  buttonConfirm.textContent = &#39;Confirm&#39;;<br>  editor.appendChild(buttonConfirm);<br>  buttonConfirm.addEventListener(&#39;click&#39;, function() {</pre><pre>    // Remove the editor from the view<strong><br>    </strong>document.body.removeChild(editor);</pre><pre>  });</pre><pre><strong>  </strong>// Create the Croppie editor<strong><br>  var croppie = new Croppie(editor, {<br>    enableResize: true<br>  });</strong></pre><pre>  // Tell Croppie to load the file<br><strong>  croppie.bind({<br>    url: URL.createObjectURL(file)<br>  });</strong></pre><pre>}</pre><p>Now we zoom in on the confirm button click handler and add the code required to make it function with Croppie.</p><p><em>Note that we’ve also added the Dropzone thumbnail creation logic.</em></p><pre>confirm.addEventListener(&#39;click&#39;, function() {</pre><pre>  // Get the output file data from Croppie<br>  <strong>croppie.result({<br>      type:&#39;blob&#39;,<br>      size: {<br>        width: 256,<br>        height: 256<br>      }<br>    }).then(function(blob) {<br>    <br>      </strong>// Create a new Dropzone file thumbnail<strong><br>      </strong>myDropZone.createThumbnail(<br>        blob,<br>        myDropZone.options.thumbnailWidth,<br>        myDropZone.options.thumbnailHeight,<br>        myDropZone.options.thumbnailMethod,<br>        false, <br>        function(dataURL) {<br>          <br>          // Update the Dropzone file thumbnail<br>          myDropZone.emit(&#39;thumbnail&#39;, file, dataURL);</pre><pre>          // Tell Dropzone of the new file<br>          done(blob);</pre><pre>        });</pre><pre><strong>    </strong>});</pre><pre>  // Remove the editor from view<strong><br>  </strong>editor.parentNode.removeChild(editor);</pre><pre>});</pre><p>That’s it! We’re already done!</p><p>When a file is dropped it’s now automatically sent to Croppie. The crop process takes over, and when the confirm button is clicked, the file is returned to Dropzone for uploading.</p><p>The animation below shows the result, <a href="https://codepen.io/rikschennink/details/87fdcbf565d3dba6e248cf1788619493">an example on CodePen is available as well</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*JXT3trvk3dw-AkoDLz4Nvg.gif" /><figcaption>DropzoneJS and Croppie integration</figcaption></figure><h3>Integrating DropzoneJS with Doka</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*m6Z0poHJb6021TxIlvwR2g.png" /></figure><p><a href="https://pqina.nl/doka">Doka.js</a> walks a different path, instead of being embedded in the page it brings its own editor popup. This means we can remove our dear editor and confirm button code.</p><p><em>Please note that Doka is a commercial JavaScript plugin and therefor source files are loaded from the server instead of a CDN.</em></p><p>Let’s take a look. First we’ll link up the CSS and JavaScript files.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>  &lt;meta charset=&quot;UTF-8&quot;&gt;<br>  &lt;title&gt;JavaScript Image Cropping with DropzoneJS&lt;/title&gt;<br>  &lt;link href=&quot;https://unpkg.com/dropzone/dist/dropzone.css&quot; rel=&quot;stylesheet&quot;/&gt;<br>  <strong>&lt;link href=&quot;doka.min.css&quot; rel=&quot;stylesheet&quot;/&gt;</strong><br>&lt;/head&gt;<br>&lt;body&gt;<br>  &lt;div class=&quot;dropzone&quot; id=&quot;myDropzone&quot;&gt;&lt;/div&gt;<br>  &lt;script src=&quot;https://unpkg.com/dropzone&quot;&gt;&lt;/script&gt;<br>  <strong>&lt;script src=&quot;doka.min.js&quot;&gt;&lt;/script&gt;</strong><br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>With the source files linked up, we can now take another look at the transformFile function. With Doka we’ll create an editor outside of the Dropzone configuration like shown below.</p><p><em>Note that we’ve again zoomed in on the last </em><em>&lt;script&gt; tag.</em></p><pre>&lt;script&gt;<br>// Create our Doka image editor<br><strong>var doka = Doka.create({<br>  cropAspectRatio: 1<br>});</strong></pre><pre>// Configure Dropzone<br>Dropzone.options.myDropzone = {<br>  url: &#39;/post&#39;,<br>  transformFile: function(file, done) {</pre><pre>  }<br>};<br>&lt;/script&gt;</pre><p>With Doka we can call doka.edit to open the file. It will automatically resolve a Promise when the crop has been confirmed. Let’s add the required logic to the transformFile function.</p><p><em>The Dropzone thumbnail creation logic remains the same.</em></p><pre>function(file, done) {<br>  <br>  // Shortcut to this dropzone<br>  var myDropZone = this;</pre><pre>  // Edit the file and wait for confirm<br>  <strong>doka.edit(file).then(function(output) {</strong></pre><pre>    // Get blob from Doka output object<br>    <strong>var blob = output.file;</strong><br>    <br>    // Create a new Dropzone file thumbnail<strong><br>    </strong>myDropZone.createThumbnail(<br>      blob,<br>      myDropZone.options.thumbnailWidth,<br>      myDropZone.options.thumbnailHeight,<br>      myDropZone.options.thumbnailMethod,<br>      false, <br>      function(dataURL) {<br>          <br>        // Update the Dropzone file thumbnail<br>        myDropZone.emit(&#39;thumbnail&#39;, file, dataURL);</pre><pre>        // Tell Dropzone of the new file<br>        done(blob);</pre><pre>     });</pre><pre>  });</pre><pre>}</pre><p>We’re done! 🎉</p><p>See the result below, <a href="https://pqina.nl/doka/">find out more about Doka on the product site</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*fZi8RyuiY1lW28aJtNRC4Q.gif" /></figure><p>If you like the article or have any questions, <a href="https://twitter.com/rikschennink">let me know on Twitter</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e22b12ac0d8a" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/integrating-dropzone-with-javascript-image-cropper-optimise-image-upload-e22b12ac0d8a">Integrating DropzoneJS with a JavaScript Image Cropper to Optimise Image Uploads</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Smooth File Uploading with React]]></title>
            <link>https://itnext.io/uploading-files-with-react-and-filepond-f8a798308557?source=rss-d5baff86691------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8a798308557</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[filepond]]></category>
            <category><![CDATA[file-management]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Rik Schennink]]></dc:creator>
            <pubDate>Fri, 10 Aug 2018 14:43:17 GMT</pubDate>
            <atom:updated>2019-04-12T12:56:20.555Z</atom:updated>
            <content:encoded><![CDATA[<h4>A tutorial on how to quickly set up the FilePond React Adapter</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A66M6hR0oBaAqNoLqBXZ9A.jpeg" /></figure><p><a href="https://pqina.nl/filepond">FilePond</a> is a relatively new JavaScript file upload library, in this tutorial we’ll set up a new <a href="https://reactjs.org/">React</a> app, integrate with the <a href="https://github.com/pqina/react-filepond">FilePond React Adapter</a> component and set up a connection to a backend which will receive our uploaded files.</p><p>We’ll use the <a href="https://github.com/facebook/create-react-app">Create React App</a> node module to quickly assemble our base React application. If you’re familiar with this process you can probably skip to the <a href="#17db">Integrating with FilePond</a> section.</p><h3>Creating a base React app</h3><p>Assuming you‘ve already installed <a href="https://nodejs.org/">Node</a> version 6 or higher run the following command from your terminal (if not, this is the time to install it). It will create a folder “my-app” which will contain our react app.</p><pre>npx create-react-app <strong>my-app</strong></pre><p>Alrighty, we got our starting point let’s navigate to the “my-app” directory.</p><pre>cd my-app</pre><p>Now start the development server, it will automatically open a browser window.</p><pre>npm start</pre><p>You can stop the app from the command line by pressing both theCTRL and C key at the same time.</p><h3>Integrating with FilePond</h3><p>With our React app running we can now add the FilePond React adapter.</p><p>Let’s stop the app and install the adapter.</p><pre>npm install <strong>filepond</strong> <strong>react-filepond</strong> --save</pre><p>With the adapter files installed, let’s fire up the app again so we can start making changes.</p><p>We’ll add the FilePond component to the app landing page, in reality you’ll probably move it somewhere else, but for the purpose of this article this will suffice.</p><p>We first need to import the FilePond component and its CSS file.</p><p>Open the “App.js” file and add the following lines to the top of the file.</p><pre>import { <strong>FilePond</strong> } from &#39;react-filepond&#39;;<br>import <strong>&#39;filepond/dist/filepond.min.css&#39;;</strong></pre><p>Next step is to add the component to the HTML. Let’s edit the HTML in the render() function and add the &lt;FilePond/&gt; tag.</p><pre>&lt;div <em>className</em>=&quot;App&quot;&gt;</pre><pre> &lt;header <em>className</em>=&quot;App-header&quot;&gt;<br>   &lt;img <em>src</em>={logo} <em>className</em>=&quot;App-logo&quot; <em>alt</em>=&quot;logo&quot; /&gt;<br>   &lt;h1 <em>className</em>=&quot;App-title&quot;&gt;Welcome to React&lt;/h1&gt;<br>  &lt;/header&gt;</pre><pre>  &lt;p <em>className</em>=&quot;App-intro&quot;&gt;<br>    To get started, edit &lt;code&gt;src/App.js&lt;/code&gt; and save to reload.<br>  &lt;/p&gt;</pre><pre>  <strong>&lt;FilePond/&gt;</strong></pre><pre>&lt;/div&gt;</pre><p>The FilePond drop area should now show up below the introduction text. It’ll look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*tMmUHba39VqaKbCyry9K8A.png" /></figure><p>Now we can start configuring FilePond to our wishes.</p><p>By default FilePond only accepts one file, this is because it enhances the default file input element and copies its standard behavior.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*27IG4zyokQcRTxh2zU6YWg.gif" /></figure><p>To enable multiple file mode lets add the allowMultiple prop.</p><pre>&lt;FilePond <strong>allowMultiple={true}</strong>/&gt;</pre><p>Drop a folder or select multiple files to see it in action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*EQ3uuEJ4nwtsBH5-5GYGYA.gif" /></figure><p>Just like the classic file input, files are loaded to FilePond but don’t do anything else, they just sit there. We most likely want to send them to our backend. We’ll do this by supplying FilePond with the server prop.</p><p>If you’ve got a server end point that can handle file objects being posted to it you can probably use that, if not, we can set up the <a href="https://github.com/pqina/filepond-boilerplate-php">FilePond PHP Boilerplate</a> (download the repository and run vagrant up).</p><pre>&lt;FilePond allowMultiple={true} <strong>server=&quot;http://192.168.33.10&quot;</strong>/&gt;</pre><p>With the server location configured, FilePond will automatically POST dropped files to the supplied URL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*8tN-yfQKQPkkHfM07T5NQg.gif" /></figure><p>The default FilePond server calls are described in the <a href="https://pqina.nl/filepond/docs/patterns/api/server/">server configuration docs</a>. The server property can be finetuned to the lowest level (see docs), this allows FilePond to be integrate with basically any remote or local file storage solution.</p><p>To spice things up a bit let’s enable image previews.</p><p>We’ll start with installing the image preview plugin.</p><pre>npm install <strong>filepond-plugin-image-preview</strong> --save</pre><p>Now we need to import and register the plugin with the FilePond core. We’ll have to alter the FilePond import to also import the registerPlugin method.</p><pre>import { FilePond, <strong>registerPlugin</strong> } from &#39;react-filepond&#39;;</pre><p>We’re ready to import and register the image preview plugin.</p><pre>import <strong>FilePondPluginImagePreview</strong> from &#39;filepond-plugin-image-preview&#39;<strong>;</strong><br>import <strong>&#39;filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.css&#39;</strong>;</pre><pre>registerPlugin(<strong>FilePondPluginImagePreview</strong>);</pre><p>Done! Let’s restart the app and drop an image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*mwkOVJH-HlWwIMYa6xkRHw.gif" /></figure><p>And that’s it, the basics of file uploading with FilePond.</p><p>You can further enhance the component with features like automatic <a href="https://github.com/pqina/filepond-plugin-image-exif-orientation">EXIF orientation correction</a>, <a href="https://github.com/pqina/filepond-plugin-image-crop">image cropping</a>, <a href="https://github.com/pqina/filepond-plugin-image-resize">resizing</a>, <a href="https://github.com/pqina/filepond-plugin-image-transform">clientside image transforms</a> and <a href="https://github.com/pqina/filepond#plugins">various other plugins</a>.</p><p>There’s a whole <a href="https://pqina.nl/filepond/docs/patterns/api/filepond-instance/#properties">list of props</a> and <a href="https://pqina.nl/filepond/docs/patterns/api/filepond-instance/#callbacks">events</a> you can configure to make FilePond fit your needs.</p><p>If you have any questions, <a href="https://twitter.com/rikschennink">find me on Twitter</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8a798308557" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/uploading-files-with-react-and-filepond-f8a798308557">Smooth File Uploading with React</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[5 interesting technical challenges I faced when building FilePond]]></title>
            <link>https://itnext.io/filepond-frontend-trickery-a3073c934c77?source=rss-d5baff86691------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3073c934c77</guid>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[file-upload]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Rik Schennink]]></dc:creator>
            <pubDate>Tue, 03 Jul 2018 12:17:03 GMT</pubDate>
            <atom:updated>2018-07-03T18:43:40.565Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://pqina.nl/filepond">FilePond</a> is an MIT licensed JavaScript file upload library released in March 2018. It’s available as a native JavaScript plugin but can also be used with <a href="https://github.com/pqina/react-filepond">Vue</a>, <a href="https://github.com/pqina/react-filepond">React</a>, <a href="https://github.com/pqina/ngx-filepond">Angular</a>, and <a href="https://github.com/pqina/jquery-filepond">jQuery</a> using special adapter components.</p><p>We’re going to take a look at the FilePond animation engine, the way it renders the drop area, how the image preview plugin leverages the new createImageBitmap API and lastly we’ll look into the limitations of our good old friend the file input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*S30UjPcfsrh95S3fnrOyzQ.gif" /></figure><h3>Animation Engine</h3><p>I wanted to make a central animation loop drive the entire FilePond interface as it can become very tricky to create high-performance animations with multiple loops while also reading information from the DOM.</p><p>The FilePond interface consists of a tree of views, each of these views has a root element and has a read and write function. For every tick of the core animation loop, FilePond calls the read method of the root view, which then calls the read methods of its child views, and so on. Then, when all views have been read, it calls the write method on the root view and this view calls the write methods on its child views, and so on till the end of the view tree is reached. So it basically does a read on the FilePond DOM subtree and then a write on that same subtree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*oUamfM3H0z_bMR2fqYtduQ.png" /><figcaption>Chrome Layers inspector reveals FilePond views</figcaption></figure><p>The read method, reads layout information from the DOM. This contains data like the current element position, width, height and margins. The retrieved layout information is then stored in each view so it can be used in the write method. Because all reads are grouped the browser only has to do layout calculations once, subsequent requests for layout information are “free”.</p><p>The write method is in charge of updating the view. It will apply the layout information and update text elements and apply animations attached to the view. These animations can be tweens or spring based animations.</p><p>The core animation loop runs till no more actions are received, views have been updated and all animations have been completed and the interface reaches an idle state. FilePond then waits till the idle state is disturbed by either the user or an API call.</p><p>Because the DOM reads and writes have been grouped and we only animate using `transform` and `opacity` there’s no <a href="https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing">layout trashing</a> (write methods can’t invalidate layout as reads have already finished), the browser only has to do compositing, resulting in very fast performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j3ugtsvBmvt853CScedofw.png" /><figcaption>A typical FilePond frame at 6 times CPU slowdown: All logic runs, DOM is updated, browser recalculates styles, and finally handles compositing</figcaption></figure><h3>Animating the File Drop Area</h3><p>The FilePond drop area needed to grow vertically with each file dropped. I badly wanted to animate this as all other elements are animated as well and having the drop area size change instantly felt very out of place.</p><ul><li>I could use scaleY to scale the drop area on the GPU but scaling a rounded rectangle will cause its rounded corners to stretch, which looks weird.</li><li>Or I could animate the height property but that would be very slow as it can’t be composited on the GPU. Also, it doesn’t do subpixels so as you can see in the animation below when the left square is near max height, the animation becomes less smooth.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*UcwrLvi9bkoPCm-XVUuApQ.gif" /><figcaption>Left animated height, Right animated transform scaleY (<a href="https://codepen.io/rikschennink/pen/jKJgOb">codepen</a>)</figcaption></figure><p>To circumvent this we can use a technique called <a href="https://w3.eleqtriq.com/2014/03/the-holy-grail-of-image-scaling/">9-Slice Scaling</a></p><p>Instead of one div, we use three separate divs for drop area.</p><ol><li>A static top div that renders the top-left and top-right rounded corner.</li><li>A middle div that is scaled over the y-axis.</li><li>A bottom div that is translated and renders the bottom-left and bottom-right corner.</li></ol><p>To simulate the height animation we set the middle div height to 1px and use the scaleY() transform to change its height. Setting transform: scaleY(100) will result in a 100px high div. At the same time, we can move the bottom div to a 100 pixel vertical offset using transform: translateY(100px)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/1*wrhiemLARXGutUjqo6eaKQ.png" /><figcaption>This is how it looks in the Chrome web inspector Layers view</figcaption></figure><p>By combining both transforms we can create the illusion of animating height, the animation performs well, our corners stay nice and sharp and we get the smoothness of subpixel positioning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*cUGV5BD6wnTG4S0GV0iBcw.gif" /></figure><h3>Fake Progress Indicator</h3><p>On a fast connection, a tiny file can be uploaded to the server in the blink of an eye. This might cause your users to wonder if their files have actually been uploaded as the status almost immediately switches to<em>“upload complete” </em>skipping over the “<em>busy uploading…”</em> state.</p><p>To prevent this uncertainty, FilePond will show a fake progress indicator for the first second of each upload. If the upload takes longer the progress of the actual file upload takes over. This might give the user additional confidence that the file has actually been uploaded.</p><p>Try for yourself, which version feels better?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*h2QzZa_3MjuQUf4L5COz6Q.gif" /><figcaption>Left: with fake progress indicator, Right: without</figcaption></figure><h3>What’s up with the File Input?</h3><p>It’s very unfortunate but apart from serious style issues, our beloved file input has another more pressing limitation. It’s impossible to set a file input’s files property. I tried, vigorously.</p><p>I fully understand the reason why setting a path to the value attribute is all but secure (you could point it to a file on the user’s file system), but having the option to add a newly created File object to the files list would be very useful.</p><p>At the moment files that are drag-n-dropped have to be uploaded asynchronously. As the File objects are contained in the drop event they can’t be stored in the file input. They either have to be kept in memory or have to be uploaded using XMLHttpRequest (or fetch) at once.</p><blockquote>The same goes for File objects created in the browser. For instance, files generated with a text editor, or images that have been modified on the client.</blockquote><p>If we could set or add files to the files property of the file input.</p><ul><li>It would be easier to set up form validation, as both dropped files and files selected on the file system via the browse window can be stored in the same list.</li><li>We would no longer have to modify the server to accept asynchronous uploads. Often we’ll want to know which files have been uploaded so we can reference them in the final form submit.</li><li>We could easier apply progressive enhancement techniques. The server will only receive a list of files, no matter where they originated from. If a certain CMS exposes a file input in its form module, we could do whatever we want on the front-end, as long as we set the files list.</li></ul><p>To circumvent this problem the <a href="https://github.com/pqina/filepond-plugin-file-encode">FilePond file encode plugin</a> can encode files as base64 strings. To do this without stalling animations files are sent to a Worker thread for encoding. When the worker is done encoding, a base64 encoded string is sent back and stored in a hidden input field.</p><p>Once uploaded the base64 string can then be turned back into an actual file on the server.</p><p>While this creates the opportunity to send files synchronous along with the parent form post, this creates some other problems. One has already been stated, the data needs to be turned back into a file on the server. Another one has to do with memory usage. When submitted, strings take up a lot more memory than file objects. This causes some browsers to bug out when a form with lots of data is being submitted. Another thing to keep in mind is that some server security software will mark the form data as suspicious (cause of the length of the values).</p><h3>Image Loading</h3><p>When dropping an image on FilePond I wanted to show a preview, this adds a bit of color to an otherwise monotone experience. As previewing images should be optional I moved this functionality to a separate plugin. The plugin would then render previews of dropped images and adjusts these previews based on the crop information supplied by the image crop plugin.</p><p>Problem is, if you want to render a preview of a big JPEG encoded image, it’ll take the browser some time to decode the image and render the resulting Bitmap data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yPgqoTvUVwY9ATL05WuiSA.png" /><figcaption>Decoding time for a 6MB JPEG measured with 6x CPU slow down</figcaption></figure><p>While the browser is decoding, the main thread stalls and everything freezes. <strong>That’s bad news for any running animation.</strong></p><p>To work around this problem, FilePond internally has an option to queue CPU heavy operations. It’ll await running these operations till it’s in idle state.This allows timing of heavy operations and prevents accidental freezing of animations.</p><p>I imagined it would still be nice to show some sort of busy state once the browser goes in lockdown. As it turns out, I was in luck, <a href="http://www.phpied.com/css-animations-off-the-ui-thread/">WebKit based browsers can run CSS animations even while the main thread is frozen</a>. Before handling the heavy load queue FilePond will switch to a busy<strong> </strong>state and the file progress indicator will start its infinite spin animation (unfazed by the main thread that is about to be blocked).</p><p>Further speed improvements to the image preview plugin could be made by decoding images on a separate thread. We can do this with the recently added <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/createImageBitmap">createImageBitmap</a> API (at the time of this writing supported on Chrome and Firefox). The File object is sent to a Web Worker which uses createImageBitmap to decode the file and then returns a BitmapImage object which can be rendered to a &lt;canvas/&gt; using thedrawImage method.</p><h3>That’s it for now!</h3><p>Let me know if this was interesting! I’m happy to write some more about other challenges like rendering smooth gradients, making FilePond accessible for screen readers, handling and reading JPEG EXIF orientation and writing the adapter components for React, Vue, and Angular.</p><p>If you have any questions, <a href="https://twitter.com/rikschennink">find me on Twitter</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3073c934c77" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/filepond-frontend-trickery-a3073c934c77">5 interesting technical challenges I faced when building FilePond</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>