JavaScript Tutorial

Canvas & Web APIs

Canvas 2D, Geolocation, Intersection Observer, Web Workers

What are Web APIs?

Beyond the standard JavaScript language, modern browsers provide a rich suite of built-in Web APIs that allow your code to interact with the device's hardware, graphics engine, and location services. These APIs are the foundation for building high-performance, native-like web applications.

1. Canvas API (Graphics)

The Canvas API provides a means for drawing graphics via JavaScript and the HTML <canvas> element. It's used for everything from simple graphs and diagrams to complex, 2D/3D games.
javascript
const canvas = document.querySelector("#myCanvas");
const ctx = canvas.getContext("2d");

// Create a blue rectangle!
ctx.fillStyle = "blue";
ctx.fillRect(10, 10, 150, 100);

// Draw text!
ctx.font = "30px Arial";
ctx.fillText("Canvas Rocks!", 10, 150);

2. Geolocation API (Location)

This API allows you to ask for the user's current geographic location (with their permission!).
javascript
navigator.geolocation.getCurrentPosition((pos) => {
  const { latitude, longitude } = pos.coords;
  console.log(`Location: ${latitude}, ${longitude}`);
}, (err) => {
  console.error("Location Denied!", err);
});

3. Intersection Observer API (Performance)

This is a modern, high-performance way to detect when an element enters or leaves the browser's "viewport" (the visible area). It's the standard way to implement Lazy Loading images or Infinite Scrolling.
javascript
const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      console.log("Element is visible!");
      // Stop observing after it's found!
      observer.unobserve(entry.target);
    }
  });
});

observer.observe(document.querySelector("#myElement"));

4. Web Workers (Parallel Logic)

JavaScript is single-threaded, but Web Workers allow you to run a script in a background thread, entirely separate from the main execution thread of your web application. Useful for heavy math!
javascript
// main.js
const worker = new Worker("worker.js");

// Send data to background!
worker.postMessage("Start heavy task!");

// Receive result!
worker.onmessage = (e) => {
    console.log(`Result from background: ${e.data}`);
};

Summary

FeatureDescription
CanvasHigh-performance 2D/3D graphics
GeolocationDetect user's physical coordinates
ObserverDetect visibility for lazy loading
WorkerBackground thread for parallel logic
Best ForGames, visualization, heavy processing, UX
StorageUse localStorage or IndexedDB for persistence
Historyhistory.pushState() for modern single-page routing
Clipboardnavigator.clipboard.writeText() to copy to CLIPBOARD!
Notificationnew Notification("Hi!") (Needs permission!)
Devicenavigator object provides device properties
Bluetoothnavigator.bluetooth to interact with hardware
SensorsAccelerometer and Gyroscope APIs
Screenwindow.screen for display properties
EncodingTextEncoder and TextDecoder for text processing
Cryptocrypto for high-quality random numbers and hashing
Medianavigator.mediaDevices.getUserMedia (Access Webcam/Mic!)
Wake Locknavigator.wakeLock to prevent screen sleep
IndexedDBTransactional, structured database in browser
WebGLUse Canvas context "webgl" for 3D graphics
Fileinput.files for reading user files safely
PaymentPaymentRequest for modern checkout integration