<?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: Jack Joseph</title>
    <description>The latest articles on DEV Community by Jack Joseph (@jack____joseph).</description>
    <link>https://dev.to/jack____joseph</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F461913%2F361ba057-78ea-40e5-925a-5ccedda07917.jpeg</url>
      <title>DEV Community: Jack Joseph</title>
      <link>https://dev.to/jack____joseph</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jack____joseph"/>
    <language>en</language>
    <item>
      <title>Make Your First Instagram Filter With JavaScript </title>
      <dc:creator>Jack Joseph</dc:creator>
      <pubDate>Wed, 21 Oct 2020 12:13:18 +0000</pubDate>
      <link>https://dev.to/jack____joseph/make-your-first-instagram-filter-with-javascript-4d7b</link>
      <guid>https://dev.to/jack____joseph/make-your-first-instagram-filter-with-javascript-4d7b</guid>
      <description>&lt;p&gt;If you use Instagram, there's a certain filter I'm sure you've seen. &lt;/p&gt;

&lt;p&gt;A question hovers above your head, something like &lt;em&gt;What Friends Character Are You?&lt;/em&gt; or &lt;em&gt;What Kind of Dog Are You?&lt;/em&gt;. You press, and a series of pictures are shuffled through. Eventually, it stops and you have your result. &lt;/p&gt;

&lt;p&gt;In this tutorial, we'll cover how to make this kind of filter with JavaScript and Spark AR, an augmented reality studio created by Facebook. &lt;strong&gt;My version of the project is on GitHub &lt;a href="https://github.com/dunleavyjack/Spark-AR-Instagram-Filter" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/strong&gt; That said, let's get to it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsmp7rmqmr6z59hl1ogdl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsmp7rmqmr6z59hl1ogdl.png" alt="title"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Part One: Setup in Spark AR&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Adding Textures
&lt;/h4&gt;

&lt;p&gt;After opening a blank project in Spark AR, the first thing we’ll need to do is add our pictures. You will need one picture with your “Which ____ are you?” question, and then however many results you want to include.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F93i9mwloh0cfobfpxxrj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F93i9mwloh0cfobfpxxrj.png" alt="spark1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the bottom left, you’ll see a section called &lt;strong&gt;Assets&lt;/strong&gt;. In that section, select &lt;strong&gt;Import: From Computer&lt;/strong&gt; and then choose your image files.&lt;br&gt;
These pictures will be added to your project in a folder called &lt;strong&gt;Textures&lt;/strong&gt;. In Spark AR, you can think of textures like a top-coat of paint. Later on, we will use them to cover an AR material in the Spark AR studio.&lt;/p&gt;
&lt;h4&gt;
  
  
  Adding a Face Tracker
&lt;/h4&gt;

&lt;p&gt;Above &lt;strong&gt;Assets&lt;/strong&gt; is another menu called &lt;strong&gt;Scene&lt;/strong&gt;. At the bottom right of this menu, select &lt;strong&gt;Add Objects&lt;/strong&gt;, and choose &lt;strong&gt;Face Tracker&lt;/strong&gt; from the popup menu. Our project can now follow the movements of the user’s face.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx0593yymqy2zwqiimc0p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx0593yymqy2zwqiimc0p.png" alt="spark2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, Face Tracker isn’t a ‘physical’ object so we need to right-click &lt;strong&gt;faceTracker0&lt;/strong&gt; from our Scene hierarchy, select &lt;strong&gt;Add&lt;/strong&gt;, and then choose &lt;strong&gt;Add Plane&lt;/strong&gt;. This will add a checkerboard patterned square in front of the person in the simulator and a &lt;strong&gt;plane0&lt;/strong&gt; underneath Face Tracker in our Scene hierarchy.&lt;/p&gt;

&lt;p&gt;You don’t need to change the name of the plane, but select it and look at the menu on the far right. You will see an option to add a &lt;strong&gt;Material&lt;/strong&gt;, which when selected will add a blank &lt;strong&gt;material0&lt;/strong&gt; to our Assets hierarchy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fve2c68psr0ilhq0qwmbv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fve2c68psr0ilhq0qwmbv.png" alt="spark3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, rename &lt;strong&gt;material0&lt;/strong&gt; to display. We will reference this later in our script, so it’s important that the names match. Then, under &lt;strong&gt;Shader Type&lt;/strong&gt; select &lt;strong&gt;Flat&lt;/strong&gt;, and under &lt;strong&gt;Texture&lt;/strong&gt; select the “Which _____ are you?” image file you added before.&lt;/p&gt;

&lt;p&gt;You will now see your image displayed in front of the face in the simulator. You can now go back to &lt;strong&gt;plane0&lt;/strong&gt; and play around with the position and scale until it looks right.&lt;/p&gt;
&lt;h4&gt;
  
  
  Connecting to the Script
&lt;/h4&gt;

&lt;p&gt;Going back to our &lt;strong&gt;Assets&lt;/strong&gt; menu, select &lt;strong&gt;Add Asset&lt;/strong&gt;, and choose &lt;strong&gt;Script&lt;/strong&gt;. This will create a script directory including a blank script in the hierarchy called &lt;strong&gt;script.js&lt;/strong&gt;. We need to create two patches; an event listener and a reference to our script.&lt;/p&gt;

&lt;p&gt;Under &lt;strong&gt;View&lt;/strong&gt; on the very top of the page select &lt;strong&gt;Show/Hide Patch Editor&lt;/strong&gt;. It will be blank for now, but this way we can see things as we add them in a second.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fowsa2s2c9idyog6av8zl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fowsa2s2c9idyog6av8zl.png" alt="spark4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, right-click anywhere in the patch editor and select &lt;strong&gt;Screen Tap&lt;/strong&gt; from the popup menu. This will create a patch that will listen for a user’s touch.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuqbf9zefe768wdg413m2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuqbf9zefe768wdg413m2.png" alt="spark5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lastly, select &lt;strong&gt;script.js&lt;/strong&gt; and choose the &lt;strong&gt;To Script&lt;/strong&gt; option from the menu on the right. Select Pulse from the dropdown and change its name to tap. When you press the yellow arrow, it will appear as a yellow rectangle in our patch editor. Draw a line between &lt;strong&gt;Screen Tap&lt;/strong&gt; and &lt;strong&gt;tap&lt;/strong&gt; to connect the two patches. &lt;/p&gt;

&lt;p&gt;Now our script can communicate with Spark AR and we can get started writing the program.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Part Two: Scripting&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Double click on the script.js file and your default code-editor will open (I used VS Code for this project). First, you need to reference the different Spark AR APIs that we’ll need later, and you can do so in a giant chunk like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Time = require('Time');
const Patches = require('Patches');
const Instruction = require('Instruction');
const Diagnostics = require('Diagnostics');
const Materials = require('Materials');
const Textures = require('Textures');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we need to reference our &lt;strong&gt;display&lt;/strong&gt; material (which we labeled before) and create an array to hold all our result pictures, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const display = Materials.get('display');
const pics = ['pic1', 'pic2', 'pic3', 'pic4'];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we just need to establish a few more things before we can really get started.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let randInterval = null;
let status = 'ready';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’ll use &lt;code&gt;randInterval&lt;/code&gt; later to shuffle through the different pictures in our &lt;code&gt;pics&lt;/code&gt; object and we’ll use &lt;code&gt;status&lt;/code&gt; to keep track of the progress of the program along the way.&lt;/p&gt;

&lt;p&gt;Finally, we need to set up an initial instruction for the user to see when they start the filter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Instruction.bind(true, 'tap_to_start');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instructions appear in white text at the bottom of the screen. They can be toggled on and off with the booleans &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; and the display text is set using what Spark AR calls a &lt;strong&gt;token&lt;/strong&gt;, which has a string value. The first token we’ll use is &lt;code&gt;'tap_to_start'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are a few different instruction tokens, and you can find them in the main Spark AR studio by selecting &lt;strong&gt;Project → Capabilities → Instructions → Custom Instructions&lt;/strong&gt;. You also need to declare each instruction that you will use within Spark AR by selecting &lt;strong&gt;Add Instruction&lt;/strong&gt; and choosing the complimentary token from the dropdown menu.&lt;/p&gt;

&lt;p&gt;Now, we can get to the heart of our program, which looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Patches.getPulseValue('tap').subscribe(function (e) {
    Instruction.bind(false, 'tap_to_start')
    if (status === 'ready') {
        start();
    }
    else if (status === 'running'){
        return;
    }
    else if (status === 'finished'){
        reset();
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first line listens for a screen &lt;code&gt;tap&lt;/code&gt; using the tap patch we made earlier. When it “hears” a tap, it turns off the &lt;code&gt;'tap_to_start'&lt;/code&gt; instruction and begins an if-loop to shuffle our pictures.&lt;/p&gt;

&lt;p&gt;Since we already set the status to &lt;code&gt;ready&lt;/code&gt;, the first step of this loop is to call the function &lt;code&gt;start()&lt;/code&gt;. We write this function ourselves, and it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function start(){
    status == 'running';

    randInterval = Time.setInterval(function(){
        randomImage();
    }, 100);
    beginCountDown();
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, our status is changed to &lt;code&gt;'running'&lt;/code&gt;. If you check back to the main if-loop, you’ll see a solitary &lt;code&gt;return&lt;/code&gt; keyword under the &lt;code&gt;else if&lt;/code&gt; statement for this status. Essentially, this ignores any additional taps while the pictures are being shuffled.&lt;/p&gt;

&lt;p&gt;Next,&lt;code&gt;randInterval&lt;/code&gt; is changed from &lt;code&gt;null&lt;/code&gt; to an interval which calls a function every set number of milliseconds. In this case, the function we are using is called &lt;code&gt;randomImage()&lt;/code&gt;, which we also need to write, and it will randomly choose a picture from our pics object every 100 milliseconds.&lt;/p&gt;

&lt;p&gt;That function looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function randomImage(){
    let randomNumber = randomlyChoose(0, pics.length);
    let pickedImage = pics[randomNumber]
    display.diffuse = Textures.get(pickedImage);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the first line, a number is randomly chosen between 0 and the length of your pics array. Writing a javascript function to choose a number randomly within a range can be found with a simple Google search. (I called mine &lt;code&gt;randomlyChoose&lt;/code&gt;, but you can call yours whatever).&lt;/p&gt;

&lt;p&gt;In the second line, that number is then used to index the array, choosing a random image. Lastly, &lt;code&gt;display.diffuse&lt;/code&gt; is used to change the texture of &lt;strong&gt;display&lt;/strong&gt; to that image.&lt;/p&gt;

&lt;p&gt;Next, continuing on with &lt;code&gt;start()&lt;/code&gt;, a timer is set on the first interval using &lt;code&gt;beginCountDown()&lt;/code&gt;. Here’s that function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function beginCountDown(){
    Time.setTimeout(function(){
        stop();
    }, 3000);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works similarly to setting an interval. The above function calls &lt;code&gt;stop()&lt;/code&gt; once after 3 seconds (3000 milliseconds), giving the user a result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function stop(){
    Time.clearInterval(randInterval);
    Instruction.bind(true, 'tap_to_reply')
    status = 'finished';
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This also adds an instruction for the user to restart (using the token &lt;code&gt;'tap_to_reply'&lt;/code&gt;), and changes the program status to 'finished'. This calls the final function in our program, &lt;code&gt;restart()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function reset(){
    Instruction.bind(false, 'tap_to_reply')
    Instruction.bind(true, 'tap_to_start')
    display.diffuse = Textures.get('which');
    status = 'ready';
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The instructions are reset (&lt;code&gt;tap_to_reply&lt;/code&gt; is turned off and &lt;code&gt;tap_to_start&lt;/code&gt; is turned on) and the &lt;strong&gt;display&lt;/strong&gt; material is returned to the initial “Which ____ are you?” question picture. The user can tap to use the filter again and again.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Next Steps&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Before you submit your filter for review to Instagram and Facebook, there are a lot of additional ways you can play around with it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flpn90s7o4fhngpveigce.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flpn90s7o4fhngpveigce.png" alt="spark6"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In my project, I added a &lt;strong&gt;particle emitter&lt;/strong&gt;, which allows small images to fly upwards from the &lt;strong&gt;display&lt;/strong&gt;, creating a kind of glittery effect. Also, I changed the opacity of the &lt;strong&gt;display&lt;/strong&gt; material to give it a slightly translucent look. &lt;/p&gt;

&lt;p&gt;There is a lot you can do! Good luck :)&lt;/p&gt;

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