<?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 Efraim Dahl on Medium]]></title>
        <description><![CDATA[Stories by Efraim Dahl on Medium]]></description>
        <link>https://medium.com/@efraimcdahl?source=rss-47b730509b01------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*pfj1ktPFAybZu4lr</url>
            <title>Stories by Efraim Dahl on Medium</title>
            <link>https://medium.com/@efraimcdahl?source=rss-47b730509b01------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 12:23:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@efraimcdahl/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[Guide to p5.js + shaders with Vue.js (Nuxt).]]></title>
            <link>https://medium.com/@efraimcdahl/guide-to-p5-js-shaders-with-vue-js-nuxt-d4085a962222?source=rss-47b730509b01------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4085a962222</guid>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[p5js]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[shaders]]></category>
            <dc:creator><![CDATA[Efraim Dahl]]></dc:creator>
            <pubDate>Sun, 07 Aug 2022 06:39:01 GMT</pubDate>
            <atom:updated>2022-08-07T06:39:01.434Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*seOeDs9N4JfZKwHtsQ8LnQ.jpeg" /><figcaption><a href="https://www.shadertoy.com/view/4lfSzS">Awesome Star</a>, by <a href="http://www.panteleymonov.ru/projects_eng.htm">Panteleymonov </a>on Shadertoy.</figcaption></figure><p>The creative coding library <a href="https://p5js.org/">P5.js</a> is one of my favorite libraries out there. It can be used to create interactive and beautiful graphical web applications and so much more. Add to that the power of a Java-script framework such as Vue.js for speed and integration of external data sources, the sky is the limit for data-driven web-applications rich in interactive graphics. For complicated graphics and displays however, you might want to consider shaders, they are way more performant than just using p5, and fortunately we can combine the two.</p><p>This git repository contains the code I will walk through in this guide: <a href="https://github.com/efraimdahl/VueP5AndShaders">https://github.com/efraimdahl/VueP5AndShaders</a></p><p>The setup, however, is not as straightforward as one might think. In this guide, I&#39;ll create a nuxt project from scratch, with a p5 component with a shader. It will cover integrating p5 into nuxt, and then how to make shaders work on P5 objects.</p><h3>Setting up Nuxt</h3><p>If you have an existing nuxt setup you are integrating to, or know how to do this, go ahead and skip this step. We are going to use the default installation and dependencies. If you are having problems following these instructions, or want to do it another way, reference the official <a href="https://nuxtjs.org/docs/get-started/installation/">installation guide.</a></p><p>Using your prefferred package manager (yarn, npm, npx) run</p><pre>npm init nuxt-app &lt;project-name&gt;</pre><p>I use following setup configuration. Make sure to add the libraries and integrations you want to use.</p><p>Edit: For this tutorial I am not using ESLint after all, its a great tool, just not right for this little demo-project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*-cS5_sT-f-ljJ-lyhFyBYA.png" /></figure><p>Once the installation is complete, navigate to your project folder and run</p><pre>npm run dev</pre><p>Once everything compiled, let visit https:localhost:3000 to make sure everything has set up properly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*HNHl4xBzPYxYmoveEY-Ufw.png" /><figcaption>If this is what you are seeing you are set!</figcaption></figure><p>Lets remove the default components, and add a new custom component called Hero.vue</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/247/1*hOZmCiOlY-SByzPI_U06NA.png" /></figure><p>Then modify index.vue to contain the following:</p><pre>&lt;template&gt;</pre><pre>&lt;Hero /&gt;</pre><pre>&lt;/template&gt;</pre><pre>&lt;script&gt;</pre><pre>&lt;/script&gt;</pre><p>Note that nuxt takes care of our components so you dont need to import or define export statements.</p><p>Now add the following content to the Hero component:</p><pre>&lt;template&gt;</pre><pre>&lt;div&gt;</pre><pre>&lt;h1&gt;Our brand new component&lt;/h1&gt;</pre><pre>&lt;/div&gt;</pre><pre>&lt;/template&gt;</pre><pre>&lt;style&gt;</pre><pre>&lt;/style&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*fxjkbmTEUb7dS7MqpmVqVQ.png" /><figcaption>This is how it should display on your browser under localhost:3000</figcaption></figure><h3>Adding P5</h3><pre>npm install p5 </pre><p>Lets go ahead and create the file where our p5 will live. (I am putting it into the components folder for now). Then we can start writing our p5 file. I will make the canvas as large as the view window, and make sure it stays that way using window. resize, I will also paint the background black. Note: this code is partially inspired by <a href="https://medium.com/js-dojo/experiment-with-p5-js-on-vue-7ebc05030d33?source=---------0-----------------------">Mitsuya Watanabe</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71ae6bb1a1bcfdd6177365954c35d3f5/href">https://medium.com/media/71ae6bb1a1bcfdd6177365954c35d3f5/href</a></iframe><p>To load this script into Vue, we will do the following inside the Hero.vue component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d1d4d9d413e827d43b57c11d592e25f/href">https://medium.com/media/2d1d4d9d413e827d43b57c11d592e25f/href</a></iframe><p>When opening the development server, you should be able to see a black box, as large as the screen. Notice when you resize it, it adjusts to the size.</p><h3>Adding Shaders</h3><p>P5.js provides us with a handy function to compile shaders called <a href="https://p5js.org/reference/#/p5/createShader">createShader()</a></p><p>It can take a shader file as a string, and compiles it on page load. In order to in cooperate modules as strings, we will need to add a new file loader called <a href="https://webpack.js.org/loaders/raw-loader/">raw-loader</a> to our nuxt configuration.</p><p>First we install the loader:</p><pre>npm install raw-loader --save-dev</pre><p>Then we <a href="https://nuxtjs.org/docs/features/configuration/">add the loader to the nuxt config file</a> which is located in the root directory of the project. You can use whatever file-types you want for your shaders, I am using .vert and .frag but other file-types are also valid and should work since we are loading them as raw strings. Just make sure to change the config settings if you do, otherwise Nuxt will treat your shader-files as regular files and might create issues.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9cbdd57b8bae2f2af67cb308865488f5/href">https://medium.com/media/9cbdd57b8bae2f2af67cb308865488f5/href</a></iframe><p>Now its time to create our shaders. I’ll add them to the assets/shaders folder. The code I am using for the shaders is the <a href="https://en.wikipedia.org/wiki/Mandelbrot_set">mandelbrot fractoral</a> used in the p5 <a href="https://p5js.org/reference/#/p5/createShader">createShader </a>reference.</p><p>I won’t go into to much detail on how shaders work in this tutorial, but we will need to create both a vertex and a fragment shader. (<a href="https://www.cs.cornell.edu/courses/cs4620/2014fa/lectures/glsl1.pdf">Read more about shaders</a>)</p><p>First lets make the vertex shader, i’ll call the file shader.vert</p><pre>Source: <a href="https://p5js.org/reference/#/p5/createShader">https://p5js.org/reference/#/p5/createShader</a></pre><pre>precision highp float; <br>varying vec2 vPos;</pre><pre>attribute vec3 aPosition;<br>void main() { vPos = (gl_Position = vec4(aPosition,1.0)).xy; }</pre><p>Then we add the fragment shader: shader.frag</p><pre>precision highp float;</pre><pre>varying vec2 vPos;</pre><pre>uniform vec2 p;</pre><pre>uniform float r;</pre><pre>const int I = 500;</pre><pre>void main() {</pre><pre>vec2 c = p + vPos * r, z = c;</pre><pre>float n = 0.0;</pre><pre>for (int i = I; i &gt; 0; i --) {</pre><pre>if(z.x*z.x+z.y*z.y &gt; 4.0) {</pre><pre>n = float(i)/float(I);</pre><pre>break;</pre><pre>}</pre><pre>z = vec2(z.x*z.x-z.y*z.y, 2.0*z.x*z.y) + c;</pre><pre>}</pre><pre>gl_FragColor = vec4(0.5-cos(n*17.0)/2.0,0.5-cos(n*13.0)/2.0,0.5-cos(n*23.0)/2.0,1.0);</pre><pre>}</pre><p>If you are using visual studio you should be able to get some syntax highlighting which is a game changer for more complex shaders.</p><p>Next we have to actually load the shader into our p5 file.</p><p>For this we do the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e681cd5a4fbb4efb29672cabe687280/href">https://medium.com/media/1e681cd5a4fbb4efb29672cabe687280/href</a></iframe><p>This should be all. In a future article I might explore how to build more complex shaders in Vue, adding input channels (maybe from a live data-source) etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mh2pmCY25zim25MQT9CfGA.png" /><figcaption>A snapshot from the shader in action</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4085a962222" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>