<?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: Q'inti</title>
    <description>The latest articles on DEV Community by Q'inti (@qinti).</description>
    <link>https://dev.to/qinti</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%2F741301%2F5114bee1-710e-4908-ab8c-bdd07993131c.jpeg</url>
      <title>DEV Community: Q'inti</title>
      <link>https://dev.to/qinti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/qinti"/>
    <language>en</language>
    <item>
      <title>nazca - Introduction tutorial</title>
      <dc:creator>Q'inti</dc:creator>
      <pubDate>Wed, 03 Nov 2021 08:05:41 +0000</pubDate>
      <link>https://dev.to/qinti/nazca-introduction-tutorial-51hj</link>
      <guid>https://dev.to/qinti/nazca-introduction-tutorial-51hj</guid>
      <description>&lt;p&gt;Nazca is a new GUI programming language. It is based on CSS. If you describe the style (CSS) that has functions (JavaScript) and the ability to define page structure (HTML), you'll get &lt;a href="https://github.com/Qinti/nazca"&gt;nazca&lt;/a&gt;. Another way to imagine it - the syntactic sugar that converts a single UI code into CSS, JavaScript, and HTML.&lt;/p&gt;

&lt;p&gt;Let's see how easily we can create web applications with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install and init first nazca project
&lt;/h2&gt;

&lt;p&gt;It is better to install it globally&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -g nazca
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you have a tool installed that can initialize the project for you, compile it and analyze if there are any errors.&lt;br&gt;
Let's create a new project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir firstProject
cd firstProject
nazca init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a new folder, called "firstProject", it adds the default .nazca configuration to it and some folder structure for you. It also creates the default &lt;code&gt;*.nazca&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;You can easily compile it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nazca
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nazca does not serve files. It is up to you what you want to use, will it be the nodeJS app, or simple nginx serving static files - it does not matter. Nazca is just a front-end compiler that takes the sources and converts them to &lt;code&gt;*.css&lt;/code&gt;, &lt;code&gt;*.js&lt;/code&gt;, and &lt;code&gt;.html&lt;/code&gt; for your browser to understand.&lt;br&gt;
For this tutorial, we will use a simple HTTP nodeJS server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -g http-server
cd www
http-server -c-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We installed &lt;code&gt;http-server&lt;/code&gt; package, which will serve the files, then changed to the output directory (check &lt;code&gt;.nazca&lt;/code&gt; settings file) and ran it with the cache living for 1 second.&lt;br&gt;
Open your browser and go to &lt;code&gt;http://localhost:8080&lt;/code&gt;, you'll see the compiled default nazca code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Import existing JS code
&lt;/h2&gt;

&lt;p&gt;The easiest way to do something is to import the module intended to be used on the browser side. I have one useful module just for the sake of the tutorial, but you can import any existing modules you would like. During the compile nazca searches the module you specified in the &lt;code&gt;node_modules&lt;/code&gt; directory, puts the script inside the &lt;code&gt;modules&lt;/code&gt; directory, and imports it with &lt;code&gt;import&lt;/code&gt; of the ES6.&lt;/p&gt;

&lt;p&gt;Let's install it first&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i nazca-logo-3d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should install it locally for nazca to be able to search &lt;code&gt;node_modules&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's modify &lt;code&gt;nazca/index.nazca&lt;/code&gt; we created previously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*import: Logo = nazca-logo-3d;

.html {
    .head {
        .title {
            text: Nazca 3D logo;
        };
    };

    .body {
        constructor: () {
            new Logo(native);
        };
    };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have a few elements without the names, but with the defined classes. Nazca has pre-defined classes for all html elements. If you define &lt;code&gt;.head&lt;/code&gt; it is compiled as an html element &lt;code&gt;&amp;lt;head&amp;gt;&amp;lt;/head&amp;gt;&lt;/code&gt;.&lt;br&gt;
The body element has &lt;code&gt;constructor()&lt;/code&gt; method. This method is executed, when all the elements are loaded (&lt;code&gt;DOMContentLoaded&lt;/code&gt; event).&lt;br&gt;
At the beginning of the file, we used &lt;code&gt;import directive&lt;/code&gt;. It defines a new global variable, called "Logo", which is imported from the module we installed. In the constructor, the new instance of the &lt;code&gt;Logo&lt;/code&gt; is created. Because the code is inside the constructor of the body, to access its properties you don't have to specify &lt;code&gt;this&lt;/code&gt;, but use the property instead. All nazca elements have default &lt;code&gt;native&lt;/code&gt; property. It allows you to access the native element. It is useful in this case to pass it to the constructor of the module.&lt;/p&gt;

&lt;p&gt;Compile it and refresh it in the browser.&lt;/p&gt;
&lt;h2&gt;
  
  
  Adding the loader
&lt;/h2&gt;

&lt;p&gt;Your program uses the &lt;code&gt;nazca-logo-3d&lt;/code&gt; module that loads the models from the web and renders them. What if you have a slow connection and can't load the models quickly? Let's create a loader for it.&lt;br&gt;
Let's imitate the loading process by adding 5s timeout before adding the &lt;code&gt;Logo&lt;/code&gt; to the page&lt;/p&gt;

&lt;p&gt;Replace contents of &lt;code&gt;nazca/index.nazca&lt;/code&gt; with this code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*import: Logo = nazca-logo-3d;

.html {
    .head {
        .title {
            text: Nazca 3D logo;
        };
    };

    .body {
        constructor: () {
            setTimeout(() =&amp;gt; {
                new Logo(native);
            }, 5000);
        };

        .div {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            z-index: -1;

            .div {
                -maxCounter: 45;
                -counter: 0;
                -increment: 1;

                constructor: () {
                    animate();
                };

                -animate: () {
                    counter += increment;

                    if (counter &amp;gt;= maxCounter) {
                        increment = -1;
                    } else if (counter &amp;lt; 0) {
                        increment = 1;
                    }

                    point.left = `${counter}px`;
                    requestAnimationFrame(animate);
                };

                point.div {
                    background-color: #80e780;
                    position: absolute;
                    left: 0;
                    top: 0;
                    width: 1px;
                    height: 1px;
                    box-shadow: 1px 0 10px 3px #2c9d2c;
                };
            };

            .div {
                text: Loading;
                text-transform: uppercase;
                font-family: sans-serif;
                font-size: 10px;
                padding-top: 15px;
            };
        };
    };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nazca has 3 types of accessors. Private properties and methods are accessible only by the instance of the class, where they are declared. Protected can be accessible by the instance of any class extended from that one and the public, accessible by anyone. In nazca they are specified by a special prefix. &lt;code&gt;-&lt;/code&gt; means private methods and properties. If no properties are specified, it is public. See &lt;a href="https://github.com/Qinti/nazca/blob/master/README.md"&gt;full nazca README&lt;/a&gt; for more details.&lt;/p&gt;

&lt;p&gt;Here we added a new element, called &lt;code&gt;point&lt;/code&gt;, so later we can access it in the parent element in the hierarchy. The code in &lt;code&gt;animate()&lt;/code&gt; moves it by one pixel every frame, making running animation. All public properties that are named as CSS parameters are treated by the compiler as CSS properties.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this tutorial I covered a small portion of what nazca can do. Check the project's &lt;a href="https://github.com/Qinti/nazca/blob/master/README.md"&gt;GitHub page and see the full README&lt;/a&gt; to learn more features.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>css</category>
      <category>html</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
