heatspot 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,7 +5,6 @@
5
5
  [![TypeScript](https://img.shields.io/badge/TypeScript-5.9-%233178C6?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
6
6
  [![license](https://img.shields.io/github/license/Davidhanson90/heatspot.svg)](LICENSE)
7
7
  [![verify](https://github.com/Davidhanson90/heatspot/actions/workflows/verify-main.yml/badge.svg)](https://github.com/Davidhanson90/heatspot/actions/workflows/verify-main.yml)
8
- [![publish](https://github.com/Davidhanson90/heatspot/actions/workflows/publish-npm.yml/badge.svg)](https://github.com/Davidhanson90/heatspot/actions/workflows/publish-npm.yml)
9
8
 
10
9
  `heatspot` is an ESM TypeScript library for capturing pointer heat data and rendering an embeddable heatmap web component.
11
10
 
@@ -62,9 +61,45 @@ Example with hidden toolbar:
62
61
 
63
62
  ### Example
64
63
 
65
- ![example.jps](assets/example.jpg)
64
+ ![example.jpg](https://raw.githubusercontent.com/Davidhanson90/heatspot/main/assets/example.jpg)
66
65
 
67
- ### 3. Optional tracking API
66
+ ### 3. Read heatmap data from a `<heat-spot>` element
67
+
68
+ ```ts
69
+ const element = document.querySelector<HeatSpotElement>('heat-spot');
70
+
71
+ const snapshot = element.getHeatmapData();
72
+
73
+ console.log(snapshot);
74
+ ```
75
+
76
+ Example data shape:
77
+
78
+ ```json
79
+ {
80
+ "totalSamples": 42,
81
+ "trackedSince": 1741351200000,
82
+ "viewport": { "width": 960, "height": 540 },
83
+ "hotspots": [
84
+ {
85
+ "id": "hs-0",
86
+ "x": 418.2,
87
+ "y": 225.6,
88
+ "count": 18,
89
+ "intensity": 1
90
+ },
91
+ {
92
+ "id": "hs-1",
93
+ "x": 701.1,
94
+ "y": 392.8,
95
+ "count": 9,
96
+ "intensity": 0.5
97
+ }
98
+ ]
99
+ }
100
+ ```
101
+
102
+ ### 4. Optional global tracking API
68
103
 
69
104
  ```ts
70
105
  import {
@@ -85,6 +120,33 @@ stopMouseTracking();
85
120
  resetMouseHeatmap();
86
121
  ```
87
122
 
123
+ ### 5. Export the heatmap visualization as an image
124
+
125
+ ```ts
126
+ const element = document.querySelector<HeatSpotElement>("heat-spot");
127
+
128
+ if (element) {
129
+ const imageDataUrl = element.getHeatmapImage();
130
+
131
+ if (imageDataUrl) {
132
+ console.log(imageDataUrl);
133
+ // Example prefix:
134
+ // data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...
135
+
136
+ // Optional download example
137
+ const link = document.createElement("a");
138
+ link.href = imageDataUrl;
139
+ link.download = "heatmap.png";
140
+ link.click();
141
+ }
142
+ }
143
+ ```
144
+
145
+ `getHeatmapImage()` returns:
146
+
147
+ - A `data:` URL string when the component has measurable dimensions.
148
+ - `null` if the element has no measurable surface yet (for example, hidden or not laid out).
149
+
88
150
  ## Scripts
89
151
 
90
152
  - `npm run build` - compile library to `dist/`
@@ -1,4 +1,5 @@
1
1
  import { LitElement } from "lit";
2
+ import { type HeatmapSnapshot } from "./contracts/heatmap-contracts.js";
2
3
  export declare class HeatSpotElement extends LitElement {
3
4
  static properties: {
4
5
  heatmapVisible: {
@@ -15,6 +16,17 @@ export declare class HeatSpotElement extends LitElement {
15
16
  private readonly tracker;
16
17
  static styles: import("lit").CSSResult;
17
18
  constructor();
19
+ /**
20
+ * Returns the current tracked heatmap snapshot for this element instance.
21
+ */
22
+ getHeatmapData(): HeatmapSnapshot;
23
+ /**
24
+ * Renders the current heatmap visualization into an image data URL.
25
+ */
26
+ getHeatmapImage(options?: {
27
+ type?: string;
28
+ quality?: number;
29
+ }): string | null;
18
30
  /**
19
31
  * Stops frame rendering when element leaves the document.
20
32
  */
@@ -64,6 +64,36 @@ export class HeatSpotElement extends LitElement {
64
64
  this.heatmapVisible = false;
65
65
  this.toolbar = "simple";
66
66
  }
67
+ /**
68
+ * Returns the current tracked heatmap snapshot for this element instance.
69
+ */
70
+ getHeatmapData() {
71
+ return this.tracker.getSnapshot();
72
+ }
73
+ /**
74
+ * Renders the current heatmap visualization into an image data URL.
75
+ */
76
+ getHeatmapImage(options = {}) {
77
+ const surface = this.renderRoot.querySelector(".surface");
78
+ if (!surface) {
79
+ return null;
80
+ }
81
+ const width = Math.round(surface.clientWidth);
82
+ const height = Math.round(surface.clientHeight);
83
+ if (width <= 0 || height <= 0) {
84
+ return null;
85
+ }
86
+ const imageCanvas = document.createElement("canvas");
87
+ imageCanvas.width = width;
88
+ imageCanvas.height = height;
89
+ const context = imageCanvas.getContext("2d");
90
+ if (!context) {
91
+ return null;
92
+ }
93
+ const snapshot = this.tracker.getSnapshot();
94
+ renderHeatmapOverlay(context, width, height, snapshot.hotspots);
95
+ return imageCanvas.toDataURL(options.type ?? "image/png", options.quality);
96
+ }
67
97
  /**
68
98
  * Stops frame rendering when element leaves the document.
69
99
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "heatspot",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "A tiny ESM TypeScript library.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -37,6 +37,10 @@
37
37
  "typescript",
38
38
  "esm"
39
39
  ],
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "https://github.com/Davidhanson90/heatspot.git"
43
+ },
40
44
  "author": "",
41
45
  "license": "MIT",
42
46
  "engines": {