Category: JavaScript

With JavaScript you can create dynamic solutions on the web. From microanimations to web apps that will fetch content via an API.

  • How to use jQuery in a Page or Post

    How to use jQuery in a Page or Post

    In order to use jQuery you must get the library. Here we’ll use a CDN. You can import the jQuery CDN in a custom HTML block, like this:

    <script
      src="https://code.jquery.com/jquery-3.6.3.min.js"
      integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU="
      crossorigin="anonymous"></script>

    That’s it. Now you can use jQuery.

    Just for the Hell of it, let’s try and create a button that will hide/show all <p> tags. Select the button, and give it an id. You can do this in advanced settings. Here the id is removeP.

    Here is the code that should remove the <p>-elements. Add the script tag to a custom HTML block:

    <script>
    $('#removeP').click(function(){
       $('p').toggle(777);
    })
    </script>
    

    The code above will detect a click event on the button. We gave the button the id removeP. When a click is detected a new selector will mark all <p> elements and toggle them. A toggle is a bit like a normal on/off switch on the wall. If the elements are visible they will hide away. Or vice versa. The number in the parethesis (777) is the number of microseconds that the toggle animation will last.

    Now try to click the button. Does it work as intended?

  • WordPress: wrap columns so that the right one gets on top on mobile devices

    WordPress: wrap columns so that the right one gets on top on mobile devices

    Normally WP will wrap the columns after a manner where the right one goes to the bottom om mobile devices. Here is a recipe:

    Add a class to your column block

    In additional classes add your class. In this case I added: petjReverse as my class.

    style.css

    Then add the this to your style.css:

    /* Mobile, Tablet */
    @media only screen and (max-width: 783px) {
    
       /* will stack reversed */
       .petjReverse {
    	   flex-wrap: wrap-reverse !important;
       }
    	
    }

    Save and update. If need be clear the cache in WordPress and your browser. That’s it.

  • Adobe Animate and the OpenWeatherMap API

    Adobe Animate and the OpenWeatherMap API

    Part One: Adobe Animate and API

    Do you want to fetch data from an API and use the data in your Adobe Animate CC creative work? Animate CC productions are made by JavaScript and HTML canvas. In theory getting data should work.

    Let’s try!

    OpenWeatherMap

    The data from OpenWeatherMap is only available when you have a token. The first step is to create a user profile, and then to create a token. Then you can create the URL to fetch the weathere data.

    Above you see the API call. That’s the information we need in order to get data from the website. The data will be returned as JSON, but we can fetch the content and display it in Adobe Animate. You can create the string along these lines:

    var weather = 'https://api.openweathermap.org/data/2.5/weather?q=YOUR-CITY-HERE&appid=ADD-YOUR-TOKEN-HERE';

    The url will fetch data from the API in the form of JSON. The URL for the weather data is formed in the variable weather.

    A JSON object from OpenWeatherMap.

    The fetch() function will use the variable above, and that’s how we get the data for the JavaScript into the document:

    fetch( weather ).then( ... etc ... );

    Dynamic Texts

    Now we have the data. Then we want to use the data in the design. Here we will work with Dynamic Text Fields.

    Dynamic Text Field.
    Here the Dynamic Text Field is selected. In Propterties to the left it is named theCity. Now we can change the content dynamicly with JavaScript.

    On the stage you see two Dynamic Textfields called theDescription and theCity. If you want to change the text of these fields you could do it like this:

    _this.theCity.text = “Hello World”;

    But we need the text from the API. If you check out the content of the weather data in the console, you can see, that the name of the city is:

    data.name

    The city name is added to the Dynamic Text Fiels like this:

    _this.theCity.text = data.name;

    If you use the Inspect Tool in the browsesr you will be able to create Dynamic text fields for all the data in the object from OpenWeatherMap.

    The JavaScript

    In the actions layer you can add your Vanilla Javascript. Here is the script I used:

    /**
     * OpenWeatherMap API Demo
     * IMPORTANT
     * Don't use the code beautifier. It will ruin the => in the Js.
     **/
    
    // get this as a global var
    var _this = this;
    
    // Openweather API string
    var weather = 'https://api.openweathermap.org/data/2.5/weather?q=Aarhus&appid=ADD-YOUR-TOKEN-HERE';
    
    // get the weather data via query URI
    fetch(weather).then( response=>{
    	
    	return response.json();
    
    }).then(data => {
    
    	// JSON data to the console for inspection
    	console.log(data);
    
    	// add the weadther description,texts or images to theDescription
    	_this.theDescription.text = data.weather[0].description;
    	_this.theCity.text = data.name;
    
    }).
    catch (err => {
    	// Do something for an error here
    	console.log('There was an error.');
    });

    Now you can test the production. Use the inspection tool in order to use the data from the JSON object in Dynamic Text Fields.

    So that’s what it takes if you want to create an Adobe Animate production that can display data from OpenWeatherMap’s API. Of course you can work in a similar manner with other API’s and JSON objects.

    Display the Weather Forecast in WordPress

    In order to show your work in WordPress, you can use an iframe. Upload your production to a folder on your server. Then add am iframe along these lines:

    <iframe src="https://yoursite.net/yourFolder/weather.html" height="480" frameborder="0" style="overflow=hidden;"></iframe>

    When you upload your work you’ll need the HTML, JavaScript file and the images/ folder – of course with the images in the folder. You don’t need the .fla file, since it’s for production only, but I would recommend to put it there anyway. If you want to change anything, you’ll know where it is.

    Resources

  • Jsconf in Reykjavik

    I’m at the JsConf in Reykjavik. The venue is the fantastic Harpa, with wonderful artwork by Olafur Eliasson. We live in the hotel with the Sky Lounge just across the street.


    View Larger Map

    Today art and code mixed into an amazing creative cocktail. I cannot wait to experiment with the creative libraries from these wizards of code.

    Augmented Reality

    There were several talks on JavaScriot as a creative tool. Raisa Cuevas introduced to augmented reality, and she introduced several creative tools, such as:

    Google ARCore Brings Augmented Reality to Android

    https://threejs.org/

    https://poly.google.com/

    https://github.com/google-ar/three.ar.js/tree/master

    https://github.com/jeromeetienne/AR.js/blob/master/README.md

     

    Paint Splatter: The making of an interactive artwork

    Halldór Eldjárn & Þórður Hans Baldursson introduced the code behind the interactive artwork decorating Harpa this week. On the website http://paint.jsconf.is/ you can choose the colors on the building. They told about the work, and how they wrote the creative code.

     

    Creating generative art with Javascript

    Then Kate Compton told about her work as an artist / researcher. She used JavaScript for art. Even her dress was made by JavaScript. One of her bon mots was:

    “make things that makes things”

    You can follow Kate’s work here: http://galaxykate.com/

    Among the tools in her toolbox was https://threejs.org/ 

     

    The keynote

    The first keynote speaker was a nodejs talk, along the lines of this: “version 1.0.4 of XX is not there yet. Please google”. My mind drifted away, but perhaps there were hidden gems for the Nodejs adepts. But at the end of the day JsConf gave many ideas for creative code.

    Code is poetry.

  • Radio Buttons and DOM

    Gist: How to manipulate the DOM via event listeners.

     

  • Workshop: JavaScript Assignment

    Here are some ideas, you might use:

  • Jquery first fiddle

    Here is a very, very short Jquery introduction on Jsfiddle.



    After a while the document ready function becomes boring. However there is a shorthand version:

    $(function() {
        // ... your code ...
    });
  • The image registration point

    On at JavaScript canvas the registration point is the top left corner.

    So in hit detection you have to calculate the x-position, the image width and height. In layout you should be able to figure out where to plage the images, and illustrations.

    • Y = “up / down”
    • X = “left / right”

    ( I tend to forget this … )

    The image registration point.
    The image registration point.
  • Canvas Animation

    With a canvas you can animate your page. You can use images or draw geometric figures on the canvas. Here are some tutorials for canvas animation.

    Parallax Effect ( Multilayer animation )

    The following presentation is not strictly JavaScript but a sum up of a bunch of animation methods for the web:

Enable Notifications OK No thanks

We use cookies - more information

Multimusen.dk will set a few cookies from Doubleclick, Google and the Social Media plugins they ay set some cookies. Some of my pages use APIs - such as YouTube, LinkedIn, Google Fonts, Google Maps, Mapbox, Spotify, Jetpack, Twitter, Facebook &c.. Such plugins may set the odd cookie.

Close