<?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 Koma Tebe on Medium]]></title>
        <description><![CDATA[Stories by Koma Tebe on Medium]]></description>
        <link>https://medium.com/@koma.tebe?source=rss-58ad6b99e966------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*e6xCIfPw7dwxGOcXtd6jjQ.jpeg</url>
            <title>Stories by Koma Tebe on Medium</title>
            <link>https://medium.com/@koma.tebe?source=rss-58ad6b99e966------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 05 Jun 2026 11:56:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@koma.tebe/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[Compressing the tiny code]]></title>
            <link>https://medium.com/@koma.tebe/compressing-the-tiny-code-fceba1b3eb56?source=rss-58ad6b99e966------2</link>
            <guid isPermaLink="false">https://medium.com/p/fceba1b3eb56</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[twitter]]></category>
            <category><![CDATA[つぶやきprocessing]]></category>
            <category><![CDATA[p5js]]></category>
            <dc:creator><![CDATA[Koma Tebe]]></dc:creator>
            <pubDate>Thu, 01 Sep 2022 10:42:10 GMT</pubDate>
            <atom:updated>2025-10-22T19:26:04.851Z</atom:updated>
            <content:encoded><![CDATA[<p>Making of #つぶやきProcessing sketch (part 2)</p><p>There are many ways to compress <a href="https://p5js.org"><strong>p5.js</strong></a> code created in <a href="https://medium.com/@koma.tebe/tiny-code-dbf26d84fe38"><strong>the first part of this tutorial</strong></a>. I’ll show you the easiest. But before we start, let me ask you to forget everything about the rules of good JS programming. Your code is about to become dirty and unreadable! Don’t worry about warnings as long as your sketch works.</p><h3>A̶n̶o̶t̶h̶e̶r̶ ̶g̶r̶e̶a̶t̶ ̶e̶d̶i̶t̶o̶r̶</h3><p>There is a great online tool called <a href="https://tsubuyaki-p5-editor.glitch.me/"><strong>つぶやきProcessing Editor</strong></a><strong>.</strong></p><p><strong>Unfortunately, this editor doesn’t exist anymore :-(</strong><br>You can use <a href="https://jscompress.com/">https://jscompress.com/</a> instead.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*r29-qPUFEs-pYzuT0Xvdwg@2x.png" /></figure><p>It is created by a guy named <a href="https://naotohieda.com/"><strong>Naoto Heida</strong></a>. He managed to create an excellent helper tool for <a href="https://twitter.com/hashtag/つぶやきProcessing"><strong>#つぶやきProcessing</strong></a> sketches. In essence, it is a code editor with options to compress JavaScript code, format the code, record animated gifs, and tweet directly from the editor. The <strong>minify &amp; run</strong> (JavaScript compression) is the only option we are going to use in this tutorial.</p><p>Let’s copy &amp; paste the following code into the <a href="https://tsubuyaki-p5-editor.glitch.me/"><strong>つぶやきProcessing Editor</strong></a><strong>.</strong></p><pre>function setup() {<br>  createCanvas(400, 400, WEBGL);<br>  noStroke();<br>}</pre><pre>function draw() {<br>  background(0);<br>  pointLight(255, 255, 255, 0, -400, 400);<br>  pointLight(200, 200, 200, 0, 0, 400);<br>  for (let i = 0; i &lt; TAU; i += PI / 256) {<br>    push();<br>    rotateZ(i);<br>    translate(100, 0, 0);<br>    rotateY(i + frameCount / 50);<br>    box(30, 2, 30);<br>    pop();<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*yOPfKrAAZLyWcSZPS8O28Q@2x.png" /></figure><p>Click <strong>minify &amp; run</strong> to compress the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CtrITRZJG_WKOxjNsW425w.png" /></figure><p>If everything goes as expected then you should get the following result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gjaH8N22atS8gFCmhrk3eg.png" /></figure><p>The compressed code is 283 characters long. It is not bad having in mind that the original code length is 354 characters!</p><p><strong><em>Please keep in mind that the resulting, compressed, code includes 17 characters long //#つぶやきProcessing hashtag too!</em></strong></p><h3>Compression</h3><p>The JavaScript compressor does a very good job. It compresses the code following its pre-defined rules like optimizing variable names or reformating code blocks. These rules are universal for all JS scripts. But our script always has unique lines and blocks of code which can be refactored and optimized for even better compression results. Using the compressor alone, we can reduce code up to one level. But it’s not intelligent. We must help him achieve better compression results.</p><p>Here is a list of changes we can do to our code to improve compression results:</p><h3>One variable to conquer the setup() function</h3><p>We don’t need <a href="https://p5js.org/reference/#/p5/setup"><strong>setup()</strong></a> function!</p><p>If we have a code block meant to execute only once, we can do it the other way with a little help from a variable. Let’s name it <strong>f.</strong></p><p><em>p5js based</em><strong><em> </em></strong><a href="https://twitter.com/hashtag/つぶやきProcessing"><strong><em>#つぶやきProcessing</em></strong></a><strong><em> </em></strong><em>variables are global in 99% of cases. Using </em><strong><em>var</em></strong><em>, </em><strong><em>let</em></strong><em> or </em><strong><em>const</em></strong><em> can increase the number of characters.</em></p><pre><strong>f=0;</strong><br>function setup() {<br>  createCanvas(400, 400, WEBGL);<br>  noStroke();<br>}</pre><p>Now let’s move the whole <a href="https://p5js.org/reference/#/p5/setup"><strong>setup()</strong></a><strong> </strong>code block grouped as <strong>(createCanvas(400, 400, WEBGL), noStroke()) </strong>into <a href="https://p5js.org/reference/#/p5/draw"><strong>draw()</strong></a> function like this:</p><pre>f=0;<br>function draw() {<br>  <strong>f++||(createCanvas(400, 400, WEBGL), noStroke());</strong><br>  background(0);<br>  pointLight(255, 255, 255, 0, -400, 400);<br>  pointLight(200, 200, 200, 0, 0, 400);<br>  for (let i = 0; i &lt; TAU; i += PI / 256) {<br>    push();<br>    rotateZ(i);<br>    translate(100, 0, 0);<br>    rotateY(i + frameCount / 50);<br>    box(30, 2, 30);<br>    pop();<br>  }<br>}</pre><p><strong>(createCanvas(400, 400, WEBGL), noStroke())</strong> is a comma operator expression wrapped in grouping parentheses. In our case, it executes every function separated by a comma (from left to right) and that is all you have to know about it. It is great for grouping functions in one line which is important for our script.</p><p>We are using logical <strong>OR</strong> (<strong>||</strong>) to execute our grouped code only once. If the variable <strong>f</strong> on the left can be converted to false, then it executes code on the right; else, it returns the value of <strong>f</strong>.</p><p>In short, at the beginning <strong>f</strong> is equal to <strong>zero</strong> which executes the code. With every draw cycle it increases by 1 (1,2,3,…) and the code on the right never gets executed again.</p><p><strong><em>(f = 0</em></strong><em> is treated as false. </em><strong><em>f &gt; 0</em></strong><em> is treated as not false. </em><strong><em>f++</em></strong><em> is the same as </em><strong><em>f=f+1)</em></strong></p><p>Let’s copy the code into the <a href="https://tsubuyaki-p5-editor.glitch.me/"><strong>つぶやきProcessing Editor</strong></a> and compress it again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CtrITRZJG_WKOxjNsW425w.png" /></figure><p>It is 277 characters! Great! But this is just the beginning.</p><h3>No frameCount<strong> </strong>property</h3><p>Because the <strong>f</strong> variable gets incremented every frame we can use it to replace the <a href="https://p5js.org/reference/#/p5/frameCount"><strong>frameCount</strong></a> property too:</p><pre>f=0;<br>function draw() {<br>  f++||(createCanvas(400, 400, WEBGL), noStroke());<br>  background(0);<br>  pointLight(255, 255, 255, 0, -400, 400);<br>  pointLight(200, 200, 200, 0, 0, 400);<br>  for (let i = 0; i &lt; TAU; i += PI / 256) {<br>    push();<br>    rotateZ(i);<br>    translate(100, 0, 0);<br>    <strong>rotateY(i + f / 50);</strong><br>    box(30, 2, 30);<br>    pop();<br>  }<br>}</pre><p>268 Characters!</p><h3>Compress pointLight() parameters</h3><p>If we have grayscale lights (all three <strong>r</strong><em>ed</em><strong>g</strong><em>reen</em><strong>b</strong><em>lue</em> values are the same) we can write rgb values as array with one value: pointLight(<strong>255, 255, 255</strong>, 0, -400, 400) as pointLight(<strong>[255]</strong>, 0, -400, 400).</p><pre>f=0;<br>function draw() {<br>  f++||(createCanvas(400, 400, WEBGL), noStroke());<br>  background(0);<br>  <strong>pointLight([255], 0, -400, 400);<br>  pointLight([200], 0, 0, 400);</strong><br>  for (let i = 0; i &lt; TAU; i += PI / 256) {<br>    push();<br>    rotateZ(i);<br>    translate(100, 0, 0);<br>    rotateY(i + f / 50);<br>    box(30, 2, 30);<br>    pop();<br>  }<br>}</pre><p>Copy the code into the <a href="https://tsubuyaki-p5-editor.glitch.me/"><strong>つぶやきProcessing Editor</strong></a> and compress it… 256 characters!!!</p><h3>No noStroke()</h3><p>This one is cool. Our <a href="https://p5js.org/reference/#/p5/box"><strong>box(30,3,30)</strong></a> has three parameters for width, height, and depth. But we can add the fourth parameter which defines the number of triangle subdivisions in x-dimension. <em>(we can also add the fifth parameter for subdivisions in y-dimension but it is not necessary)</em></p><p>Funny thing is — if we pass a number bigger than 4 as a fourth parameter We’ll get the following warning “Cannot draw stroke on box objects with more than 4 detailX or 4 detailY” but the object will still be rendered — without outlines! Which gives the same effect as the <a href="https://p5js.org/reference/#/p5/noStroke"><strong>noStroke()</strong></a> function.</p><p>So, let’s delete <a href="https://p5js.org/reference/#/p5/noStroke"><strong>noStroke()</strong></a> function and add the fourth parameter to our <a href="https://p5js.org/reference/#/p5/box"><strong>box(30,3,30)</strong></a><strong>. </strong>Another outcome of this action is that we don’t need parentheses for <strong>createCanvas(400, 400, WEBGL)</strong> because it is the only function to be executed in the case when <strong>f=0.</strong></p><pre>f=0;<br>function draw() {<br>  f++||<strong>createCanvas(400, 400, WEBGL);</strong><br>  background(0);<br>  pointLight([255], 0, -400, 400);<br>  pointLight([200], 0, 0, 400);<br>  for (let i = 0; i &lt; TAU; i += PI / 256) {<br>    push();<br>    rotateZ(i);<br>    translate(100, 0, 0);<br>    rotateY(i + f / 50);<br>    <strong>box(30, 2, 30, 5);</strong><br>    pop();<br>  }<br>}</pre><p>If you copy &amp; paste in <a href="https://tsubuyaki-p5-editor.glitch.me/"><strong>つぶやきProcessing Editor</strong></a> you’ll see that the compressed code is 245 characters long now!</p><h3><strong>No background()</strong></h3><p>Instead of using <a href="https://p5js.org/reference/#/p5/background"><strong>background()</strong></a>,<strong> </strong>we can use a big <a href="https://p5js.org/reference/#/p5/box"><strong>box()</strong></a><strong> </strong>that can do the same job. It will cover the remains of the previous <a href="https://p5js.org/reference/#/p5/draw"><strong>draw()</strong></a> cycle and fill the scene with black color because it is big and light can not light it properly.</p><p>Values bigger than 1000 will be represented by <a href="http://www.mas.ncl.ac.uk/~nwhf1/notation.html"><strong>e-notation</strong></a>. <br>Example: 1000 = 1e3 (1 character gained).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*snHre0rqPCpyam2Ir3ZTrQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*rph_tCYT_jNoRy1pNorTIA.png" /><figcaption>The scene enclosed with the box on the left. The same scene zoomed out of the enclosing box on the right.</figcaption></figure><pre>f=0;<br>function draw() {<br>  f++||createCanvas(400, 400, WEBGL);<br>  pointLight([255], 0, -400, 400);<br>  pointLight([200], 0, 0, 400);<br>  for (let i = 0; i &lt; TAU; i += PI / 256) {<br>    push();<br>    rotateZ(i);<br>    translate(100, 0, 0);<br>    rotateY(i + f / 50);<br>    box(30, 2, 30, 5);<br>    pop();<br>  }<br>  <strong>box(1000);</strong><br>}</pre><p>240 characters!!!</p><h3>Put the same values into a variable</h3><p>Value 400 appears in 5 places in the code! Let’s move it into variable <strong>W</strong>.</p><pre>f=0;<br>function draw() {<br>  f++||createCanvas(<strong>W=400</strong>, <strong>W</strong>, WEBGL);<br>  pointLight([255], 0, <strong>-W</strong>, <strong>W</strong>);<br>  pointLight([200], 0, 0, <strong>W</strong>);<br>  for (let i = 0; i &lt; TAU; i += PI / 256) {<br>    push();<br>    rotateZ(i);<br>    translate(100, 0, 0);<br>    rotateY(i + f / 50);<br>    box(30, 2, 30, 5);<br>    pop();<br>  }<br>  box(1000);<br>}</pre><p>234 characters!!!</p><h3>One character here… one character there…</h3><ul><li><a href="https://p5js.org/reference/#/p5/rotateZ"><strong>rotateZ()</strong></a> is the same as <a href="https://p5js.org/reference/#/p5/rotate"><strong>rotate()</strong></a></li><li>value 100 in the <a href="https://p5js.org/reference/#/p5/translate"><strong>translate()</strong></a> function can be changed into 99 without any visible changes</li><li>if the last <a href="https://p5js.org/reference/#/p5/translate"><strong>translate(x,y,z=0)</strong></a> parameter is 0 you can omit it: <a href="https://p5js.org/reference/#/p5/translate"><strong>translate(x,y</strong></a><strong>)</strong></li><li>put <a href="https://p5js.org/reference/#/p5/box"><strong>box()</strong></a> into <a href="https://p5js.org/reference/#/p5/pop"><strong>pop()</strong></a> as a parameter — it’ll execute before <a href="https://p5js.org/reference/#/p5/pop"><strong>pop()</strong></a><strong>.</strong> <br><em>(the same goes for any function passed as a </em><a href="https://p5js.org/reference/#/p5/pop"><strong><em>pop()</em></strong></a><strong><em> </em></strong><em>parameter)</em></li><li>remove <strong>let</strong> i=0 in for loop and keep <strong>i=0</strong> only</li></ul><pre>f=0;<br>function draw() {<br>  f++||createCanvas(W=400, W, WEBGL);<br>  pointLight([255], 0, -W, W);<br>  pointLight([200], 0, 0, W);<br>  for (<strong>i = 0</strong>; i &lt; TAU; i += PI / 256) {<br>    push();<br>    <strong>rotate(i);</strong><br>    <strong>translate(99, 0);</strong><br>    rotateY(i + f / 50);<br>    <strong>pop(box(30, 2, 30, 5));</strong><br>  }<br>  box(1000);<br>}</pre><p>Copy &amp; paste the code in <a href="https://tsubuyaki-p5-editor.glitch.me/"><strong>つぶやきProcessing Editor</strong></a><strong> </strong>and compress it. It is 225 characters long now!</p><h3>Arrow function</h3><p>Finally, we can use a new arrow function instead of the standard one. It has a shorter syntax which is great for code compression.</p><p>Instead of <strong>function draw()</strong> we can use <strong>draw=_=&gt;</strong>.</p><pre>f=0;<br><strong>draw =_=&gt;</strong> {<br>  f++||createCanvas(W=400, W, WEBGL);<br>  pointLight([255], 0, -W, W);<br>  pointLight([200], 0, 0, W);<br>  for (i = 0; i &lt; TAU; i += PI / 256) {<br>    push();<br>    rotate(i);<br>    translate(99, 0);<br>    rotateY(i + f / 50);<br>    pop(box(30, 2, 30, 5));<br>  }<br>  box(1000);<br>}</pre><p>This reduces the code to 219 characters!</p><h3>Compromise</h3><p>Our lights are set using the following values:</p><pre>pointLight([255], 0, -W, W);<br>pointLight([200], 0, 0, W);</pre><p>These values give the following result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*oEiS-S42PRRUj5SRuSyUdA.gif" /></figure><p>Looks like we can use the same light two times with only one different value:</p><pre>pointLight([255], 0, <strong>-W</strong>, W);<br>pointLight([255], 0, <strong>W</strong>, W);</pre><p>And get the slightly brighter result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*8lRFdVmWShORCnHt2A1NPw.gif" /><figcaption>Banding is the result of gif compression</figcaption></figure><p>If the resulting animation looks good then we can do another optimization using the array map function. It iterates an array and passes the iterated value to our function. Like this:</p><pre>pointLight([255], 0, <strong>-W</strong>, W);<br>pointLight([255], 0, <strong>W</strong>, W);</pre><p>… changes into:</p><pre>[<strong>-1,1</strong>].map(<strong>i</strong>=&gt;pointLight([255], 0, <strong>W * i</strong>, W));</pre><p>Each array value <strong>[-1,1]</strong> is passed as <strong>i</strong> to <a href="https://p5js.org/reference/#/p5/pointLight"><strong>pointLight()</strong></a> function. And then used to position the light on the y-axis <strong>W * i</strong>.</p><h3>Finish line</h3><p>Our final, optimized, code looks like this:</p><pre>f=0;<br>draw =_=&gt; {<br>  f++||createCanvas(W=400, W, WEBGL);<br>  [-1,1].map(i=&gt;pointLight([255], 0, W * i, W));<br>  for (i = 0; i &lt; TAU; i += PI / 256) {<br>    push();<br>    rotate(i);<br>    translate(99, 0);<br>    rotateY(i + f / 50);<br>    pop(box(30, 2, 30, 5));<br>  }<br>  box(1000);<br>}</pre><p>Compressed code looks like this:</p><pre>f=0,draw=a=&gt;{for(f++||createCanvas(W=400,W,WEBGL),[-1,1].map(a=&gt;pointLight([255],0,W*a,W)),i=0;i&lt;TAU;i+=PI/256)push(),rotate(i),translate(99,0),rotateY(i+f/50),pop(box(30,2,30,5));box(1e3)};//#つぶやきProcessing</pre><p>The final code is 211 characters long (<a href="https://twitter.com/hashtag/つぶやきProcessing"><strong>#つぶやきProcessing</strong></a><strong> </strong>hashtag included!) which means that you have a whole 69 characters left to add your tweaks and changes to the code.</p><p>Be creative! Post your results on Twitter! And don’t forget to keep the <a href="https://twitter.com/hashtag/つぶやきProcessing"><strong>#つぶやきProcessing</strong></a><strong> </strong>hashtag in the compressed code. This will help people find you.</p><p>Cheers,<br>KT</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HhjLSsuJNNYMQdOeImiahg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fceba1b3eb56" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tiny code]]></title>
            <link>https://medium.com/@koma.tebe/tiny-code-dbf26d84fe38?source=rss-58ad6b99e966------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbf26d84fe38</guid>
            <category><![CDATA[つぶやきprocessing]]></category>
            <category><![CDATA[p5js]]></category>
            <category><![CDATA[twitter]]></category>
            <dc:creator><![CDATA[Koma Tebe]]></dc:creator>
            <pubDate>Wed, 31 Aug 2022 10:45:30 GMT</pubDate>
            <atom:updated>2022-09-01T11:38:09.148Z</atom:updated>
            <content:encoded><![CDATA[<p>Making of #つぶやきProcessing sketch (part 1)</p><p>The <a href="https://twitter.com/hashtag/つぶやきProcessing"><strong>#つぶやきProcessing</strong></a> is Twitter hashtag used for indexing <a href="https://processing.org"><strong>Processing</strong></a> (<a href="https://p5js.org"><strong>p5.js</strong></a> mostly) sketch code posted in a form of a tweet. The idea is to write code 280 characters long which creates a static or animated visual piece and to attach the result as an image or video respectively.</p><p>The <a href="https://twitter.com/hashtag/つぶやきProcessing"><strong>#つぶやきProcessing</strong></a> topic requires a whole new article and I will probably cover it in one of the future posts. But for the time being, I suggest you check the hashtag and see all these beautiful pieces created by many talented artists and programmers.</p><h3>What are we going to create?</h3><p>In this tutorial, we’ll create a <a href="https://p5js.org"><strong>p5.js</strong></a> sketch, create a few visual variants, compress it, make 280 characters (or less) long code, and then leave it to you for further enhancements.</p><p>The only prerequisites you need are a <strong>text editor</strong>, a little <strong>JS knowledge,</strong> and a will to create a <a href="https://twitter.com/hashtag/つぶやきProcessing"><strong>#つぶやきProcessing</strong></a><strong> </strong>sketch.</p><h3>Text editor</h3><p>The fastest way to start coding <a href="https://twitter.com/hashtag/つぶやきProcessing"><strong>#つぶやきProcessing</strong></a><strong> </strong>sketch is to use <a href="https://editor.p5js.org/"><strong>p5js editor</strong></a>.</p><p>The editor is designed especially for <a href="https://p5js.org"><strong>p5.js</strong></a> sketches. It has integrated preview, syntax coloring, formatting tools, and many other useful options. It is small, fast and it costs nothing. The only thing you have to do is to register to use it.</p><p>For this tutorial it is enough to know these options:</p><ol><li><strong>Play</strong> (run the code)</li><li><strong>Stop</strong> (stop playback of the code)</li></ol><p>There is also the option to <strong>rename</strong> the sketch (3).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qSM54_4zgnQoHI-lvW_lfw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*MQMaik-P8Kt5yqaJ4c75bQ.png" /><figcaption>Editor in dark mode and the basic commands</figcaption></figure><h3>Let’s start</h3><p>Select <strong>File &gt; New</strong> in the editor menu, and you’ll get a basic sketch template:</p><pre>function setup() {<br>  createCanvas(400, 400);<br>}</pre><pre>function draw() {<br>  background(220);<br>}</pre><p>Click <strong>Run</strong> to test it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z8E_TrfTQfyJL16ux-cXxw.png" /><figcaption>The expected result: gray rectangular sketch</figcaption></figure><p>Beautiful!</p><h3>The sketch</h3><p>This is the result we are going to achieve:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*LEPDN5VBS_ATfjSISL5_NA.gif" /></figure><h3>The code</h3><p>Most of the time simple and clean ideas result in simple code.</p><p>If you want to make a <a href="https://twitter.com/hashtag/つぶやきProcessing"><strong>#つぶやきProcessing</strong></a> sketch, you must simplify your idea as much as possible. Always ask yourself what is the essence of the scene you want to make. Are all these objects necessary? Static or animated? Color or monochrome? Getting the balance between simplicity and visual richness is tough.</p><p>Code length limitation often kills enthusiasm and joy. Don’t worry. Limits boost creativity! You’ll return with a better solution later, tomorrow… or next month — it doesn’t matter when. And the feeling of accomplishment will be great!</p><p>This is the code that outputs the animation above:</p><pre>function setup() {<br>  createCanvas(400, 400, WEBGL);<br>  noStroke();<br>}</pre><pre>function draw() {<br>  background(0);<br>  pointLight(255, 255, 255, 0, -400, 400);<br>  pointLight(200, 200, 200, 0, 0, 400);<br>  for (let i = 0; i &lt; TAU; i += PI / 256) {<br>    push();<br>    rotateZ(i);<br>    translate(100, 0, 0);<br>    rotateY(i + frameCount / 50);<br>    box(30, 2, 30);<br>    pop();<br>  }<br>}</pre><p>Copy &amp; paste it into the <a href="https://editor.p5js.org/"><strong>p5.js editor</strong></a> and test.</p><p>The resulting scene has rich shading and relatively complex object. And yet the code is very simple.</p><h3>Setup function</h3><p>Our sketch is 400 pixels wide, and 400 pixels tall and it uses <a href="https://p5js.org/reference/#/p5/WEBGL"><strong>WEBGL</strong></a> renderer. These properties are set using <a href="https://p5js.org/reference/#/p5/createCanvas"><strong>createCanvas()</strong></a> function. They must be defined only once because creating canvas every frame is very slow and compute-intensive so we are putting the following code into the <a href="https://p5js.org/reference/#/p5/setup"><strong>setup()</strong></a> function of the sketch. The<strong> </strong><a href="https://p5js.org/reference/#/p5/setup"><strong>setup()</strong></a> function executes only once — after a sketch starts.</p><pre>function setup() {<br>  createCanvas(400, 400, WEBGL);<br>  noStroke();<br>}</pre><p>By default, the edges of all objects are outlined with black lines. We don’t want these outlines. <a href="https://p5js.org/reference/#/p5/noStroke"><strong>noStroke()</strong></a> disables drawing of them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*jrWm9llhpPqLzUiuwqq5Qw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*DY4ezhiBgXL1fsAQRd0CNg.png" /><figcaption>The box with default black lines on the right and with the noStroke() function used on the right</figcaption></figure><h3>Draw function</h3><p>Animation code goes into the <a href="https://p5js.org/reference/#/p5/draw"><strong>draw()</strong></a> function. The <a href="https://p5js.org/reference/#/p5/draw"><strong>draw()</strong></a> function executes 60 times each second which is perfect for animations. <em>(the number of frames to be displayed every second is controlled with the </em><a href="https://p5js.org/reference/#/p5/frameRate"><strong><em>frameRate()</em></strong></a><em> function)</em></p><p>Let’s deconstruct our <a href="https://p5js.org/reference/#/p5/draw"><strong>draw()</strong></a> function.</p><pre>function draw() {<br>  background(0);<br>  pointLight(255, 255, 255, 0, -400, 400);<br>  pointLight(200, 200, 200, 0, 0, 400);<br>  for (let i = 0; i &lt; TAU; i += PI / 256) {<br>    push();<br>    rotateZ(i);<br>    translate(100, 0, 0);<br>    rotateY(i + frameCount / 50);<br>    box(30, 2, 30);<br>    pop();<br>  }<br>}</pre><p>Our sketch has a black <a href="https://p5js.org/reference/#/p5/background"><strong>background()</strong></a><strong>. </strong>This function refreshes the screen, clears everything from the previous draw cycle, and fills it with the desired color.</p><pre>background(0);</pre><p><em>Try to use the code without</em><strong><em> </em></strong><a href="https://p5js.org/reference/#/p5/background"><strong><em>background()</em></strong></a><strong><em> </em></strong><em>and you’ll get a picture of what it does.</em></p><p>There are two <a href="https://p5js.org/reference/#/p5/pointLight"><strong>pointLight()</strong></a> lights positioned at coordinates which give good surface shading of our object.</p><pre>pointLight(255, 255, 255, 0, -400, 400);<br>pointLight(200, 200, 200, 0, 0, 400);</pre><p><em>The first three numbers are </em><strong><em>rgb</em></strong><em> color values: <br>pointLight(</em><strong><em>255, 255, 255</em></strong><em>, 0, -400, 400).</em></p><p><em>The last three numbers are xyz position values: <br>pointLight(255, 255, 255, </em><strong><em>0, -400, 400</em></strong><em>).</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*mUyHdx0ZQq0_ssHDSvJBbg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*35-KTHx0Lahq4G2TjMwOWw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*0Vp3Wd0gBum1tcYpXhVXBQ.png" /><figcaption>Top light only, bottom light only and both lights combined</figcaption></figure><h3>Modeling by duplications</h3><p>The twisted torus in this sketch doesn’t exist as a p5.js object. Custom geometry is also not an option because the final code can be huge. But we can go in the opposite direction and use the simplest p5.js 3d primitive!</p><p>A squashed <a href="https://p5js.org/reference/#/p5/box"><strong>box()</strong></a> object duplicated enough times to create an impression of a torus (and twisted for the final touch) can be represented through the following steps:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*qtcY7SZfRKkWQgQfE5BmuA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*6SdiiY0EQr1RYd_P8CN9Xg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*JNtU1FHer9oBB5A__f_krg.png" /><figcaption>The squashed box positioned right from the center and duplicated 4 times</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*GejJAs6fv4gAXwrnVhvAhg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*QVCyk4kJrCO7sRnGdJFEGw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*Zwh3WwtnPikGIiZ2asArtg.png" /><figcaption>The same box duplicated 64 and 512 times. On the right we can see a twisting effect of rotation .</figcaption></figure><pre>for (let i = 0; i &lt; TAU; i += PI / 256) { // 4, 32, 256, ...</pre><pre>// For 4 boxes use 2 instead of 256<br>  // For 64 boxes use 32 instead of 256<br>  // For 512 boxes leave 256 as it is now<br>  // We are using TAU (2*PI) hence the number of duplicates / 2</pre><pre>push();<br>  rotateZ(i);<br>  translate(100, 0, 0);<br>  rotateY(i + frameCount / 50);<br>  box(30, 2, 30);<br>  pop();<br>}</pre><p>As you can see, we used <a href="https://p5js.org/reference/#/p5/box"><strong>box()</strong></a> and turned it into a complex object in a few steps:</p><ol><li><a href="https://p5js.org/reference/#/p5/for"><strong>for</strong></a> loop duplicates the box desired number of times.</li><li>Each loop step starts by saving current global transformation values using the <a href="https://p5js.org/reference/#/p5/push"><strong>push()</strong></a> command. Every transformation from this point is local.</li><li><a href="https://p5js.org/reference/#/p5/rotateZ"><strong>rotateZ()</strong></a> rotate the box around the Z axis by step defined in <a href="https://p5js.org/reference/#/p5/for"><strong>for</strong></a> loop.</li><li><a href="https://p5js.org/reference/#/p5/translate"><strong>translate()</strong></a> the box 100 units to the right (3d space placement is not represented in pixels. The unit value is relative).</li><li><a href="https://p5js.org/reference/#/p5/rotateY"><strong>rotateY()</strong></a> the box around the Y-axis using the current for loop step with a fraction of the current frame value (<a href="https://p5js.org/reference/#/p5/frameCount"><strong>frameCount</strong></a>). This part animates the twisting of the torus.</li><li>Draw the <a href="https://p5js.org/reference/#/p5/box"><strong>box()</strong></a> on the screen. 30 units wide, 2 units tall, and 30 units deep.</li><li>Finally, use <a href="https://p5js.org/reference/#/p5/pop"><strong>pop()</strong></a> to restore the saved global transformation values for the next <a href="https://p5js.org/reference/#/p5/for"><strong>for</strong></a> step.</li></ol><h3>Success!</h3><p>If you run the code, you should get the following result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*LEPDN5VBS_ATfjSISL5_NA.gif" /></figure><h3>Your turn</h3><p>Now, let’s add some tweaks to the code. This is the part where you can include your visual improvements. The following variants are just examples of where you can go from here.</p><p>If you change the <strong>box(30, 2, 30)</strong> part of the code with one of the following variants you can get some interesting visual results:</p><pre>box(s=abs(sin(i*6))*30,2,s,30)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*izdKNZhdNanZE6AuteLTjA.gif" /></figure><pre>box(s=abs(sin(i*2))*30,2,s,30)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*1rMI3Eela5GubznHQpQx7g.gif" /></figure><pre>box(s=abs(atan(PI-i))*30,2,s,30)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*d7ZMhUVo5KGMWXGO6h8NIw.gif" /></figure><p>Every line of code between <a href="https://p5js.org/reference/#/p5/push"><strong>push()</strong></a> and <a href="https://p5js.org/reference/#/p5/pop"><strong>pop()</strong></a> can be tweaked. I encourage you to play and experiment.</p><h3>#つぶやきProcessing sketch</h3><p>We have a sketch. But do we have a <a href="https://twitter.com/hashtag/つぶやきProcessing"><strong>#つぶやきProcessing</strong></a> sketch? <br>Let’s count characters:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x4EdKiusRJw8vB6csOTtWQ.png" /></figure><p>354 characters are way more than 280, which is the current Twitter limit. This code must be compressed. <br>I’ll show you how…</p><p>…<a href="https://medium.com/@koma.tebe/compressing-the-tiny-code-fceba1b3eb56"><strong>in the next part of this tutorial</strong></a> 😁</p><p>Cheers,<br>KT</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HoH6jAp2PnatIsgiMdgSbQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbf26d84fe38" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>