Ticker

6/recent/ticker-posts

📍 Geolocation API & 🎨 Canvas API – Two Browser APIs Devs Underrate

📍 Geolocation API & 🎨 Canvas API – Two Browser APIs Devs Underrate

You know those features that make apps feel next-level?

👉 “Show my current location”
👉 “Draw something dynamic on screen”

No heavy libraries. No backend magic.

Just built-in browser APIs: Geolocation and Canvas.

Most devs either ignore them… or overcomplicate them.

Let’s keep it real and see how they’re actually used.

📍 Geolocation API – Getting User Location (The Right Way)

⚡ What it actually does

The Geolocation API lets you access the user’s:

  • Latitude
  • Longitude
  • Accuracy

But here’s the catch:

👉 It requires user permission (and HTTPS in production)

🔎 Basic Usage

navigator.geolocation.getCurrentPosition(
  position => {
    const { latitude, longitude } = position.coords;
    console.log(latitude, longitude);
  },
  error => {
    console.error("Location error:", error);
  }
);

Simple… but don’t stop here.

🔥 Real Use Case: Location-Based Feature

async function getLocation() {
  if (!navigator.geolocation) {
    console.log("Geolocation not supported");
    return;
  }

  navigator.geolocation.getCurrentPosition(
    ({ coords }) => {
      console.log(`Lat: ${coords.latitude}, Lng: ${coords.longitude}`);
    },
    err => {
      console.error("Permission denied or error:", err.message);
    }
  );
}

💡 Real Developer Insight

This API looks easy… but in real apps:

👉 Users deny permission. A lot.

So always design fallback UX:

  • Manual location input
  • Default city
  • Graceful error handling

Also:

👉 Don’t call it repeatedly unless needed — it drains battery.

If you need continuous tracking:

navigator.geolocation.watchPosition(callback);

But be careful… this runs constantly.

⚠️ Common Geolocation Mistakes

1️⃣ Assuming permission will be granted
Nope. Always handle rejection.

2️⃣ Using it on HTTP
Won’t work in production — requires HTTPS.

3️⃣ Calling it too often
Kills performance and battery.

4️⃣ Not handling accuracy

coords.accuracy

Sometimes location isn’t precise — especially on desktops.

🎨 Canvas API – Drawing Anything in the Browser

⚡ What it actually does

Canvas lets you draw graphics using JavaScript.

Not HTML elements. Not CSS.

👉 Pure pixel-level control.

Used for:

  • Charts
  • Games
  • Animations
  • Image editing

🔎 Basic Setup

<canvas id="canvas" width="300" height="200"></canvas>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

ctx.fillStyle = "blue";
ctx.fillRect(50, 50, 100, 100);

Boom. You just drew a rectangle.

🔥 Real Use Case: Simple Drawing

const canvas = document.querySelector("#canvas");
const ctx = canvas.getContext("2d");

ctx.beginPath();
ctx.arc(150, 100, 50, 0, Math.PI * 2);
ctx.fillStyle = "orange";
ctx.fill();

💡 Real Developer Insight

Canvas is powerful… but here’s the reality:

👉 It’s imperative, not declarative.

Meaning:

  • You control every draw call
  • No automatic updates
  • No DOM diffing like React

So for complex apps:

👉 You’ll end up managing state manually or using libraries

Also:

👉 Once drawn, elements aren’t “objects” anymore — just pixels.

You can’t click a rectangle unless you track it yourself.

⚠️ Common Canvas Mistakes

1️⃣ Redrawing everything unnecessarily

Leads to performance issues.

2️⃣ Not clearing canvas

ctx.clearRect(0, 0, canvas.width, canvas.height);

Without this → overlapping drawings.

3️⃣ Mixing DOM mindset with Canvas

Canvas is not HTML. No elements. Just pixels.

4️⃣ Ignoring device pixel ratio

Leads to blurry graphics on high-res screens.

🚀 Best Practice Summary

✅ Always handle user permission and fallback in Geolocation API
✅ Use watchPosition only when continuous tracking is needed
✅ Don’t rely on high accuracy without checking coords.accuracy
✅ Use Canvas for dynamic graphics, not regular UI elements
✅ Always clear and optimize redraws in Canvas

Reactions

Post a Comment

0 Comments