JavaScript

Web APIs

The examples below build on the basic examples covered in the JavaScript: core concepts of the craft notes to create dynamic HTML/CSS content. Web browsers have built in "APIs" (application programming interfaces) which are collections of variables and functions that we can use to do all sorts of things. There are browser APIs for generating sounds, for getting data from other servers (referred to as 3rd-party "REST APIs"), for creating 2D graphics and 3D graphics, for creating Virtual and Augmented reality and so much more.

The most fundamental of these browser APIs is likely the DOM (document object model) API. When the browser renders our HTML page, behind the scenes it also creates a JavaScript Object named "document" which represents our entire HTML page, with all it's HTML and CSS. This special "document" Object has all sorts of built in properties (internal variables) and methods (internal functions) that enable us to interact with the HTML/CSS content on our page. The examples below demonstrate some of what's possible with this API, but you can learn more at Mozilla's developer docs on the DOM.





the DOM API

The DOM (document object model) API is the most fundamental browser API, it's how we manipulate our page's HTML/CSS through JavaScript and forms the foundation of any web application.

the DOM basics
removing/replacing elements
creating/adding elements

DOM Events

"event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions, sensor outputs, or message passing from other programs or threads", the DOM provides us with an API for doing just that in the browser.

click event (basic)
other events
the event object
keyboard events
multiple click events

DOM Animations

There are many ways to create animations beyond in the browser besides JavaScript, like SVG animations and CSS animations, but JavaScript is likely the most flexible and expressive ways to approach generative/interactive animations in the browser. The following examples demonstrate some of the core concepts.

animation loop (recursive function)
DVD logo bounce
circular motion
spirallling motion

Fetch API

When we visit a website our browser makes an HTTP request for that HTML as well as an subsequent HTTP requests for loading any assets (fonts, images, videos, etc). The difference between a web "page" and a web "app" is the ability to dynamically change the content of our site through user interaction and other events, which often requires the ability to make our own HTTP requests.

XMLHttpRequest (old way)
Fetch API: then(callback) (newer way)
Fetch API: async/await (newest way)