Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add debug view events per sec #15

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/index-BkH9e1jg.css → docs/index-CkherYMV.css

Large diffs are not rendered by default.

35 changes: 21 additions & 14 deletions docs/index-B7U_mx4l.js → docs/index-NS4a8Agm.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<link rel="icon" type="image/svg+xml" href="/maplibre-gl-compass/compass.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>maplibre-gl-compass</title>
<script type="module" crossorigin src="/maplibre-gl-compass/index-B7U_mx4l.js"></script>
<link rel="stylesheet" crossorigin href="/maplibre-gl-compass/index-BkH9e1jg.css">
<script type="module" crossorigin src="/maplibre-gl-compass/index-NS4a8Agm.js"></script>
<link rel="stylesheet" crossorigin href="/maplibre-gl-compass/index-CkherYMV.css">
</head>
<body>
<div id="app">
Expand Down
16 changes: 14 additions & 2 deletions src/components/DebugView.css
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
.maplibregl-ctrl-compass-heading-debug {
width: 180px;
display: flex;
flex-wrap: wrap;
width: 200px;
margin: 0;
padding: 5px 10px;
list-style: none;
background-color: #fff;
border-radius: 4px;
box-shadow: 0 0 0 2px rgba(0,0,0,.1);

li {
dt {
margin: 0;
width: 50px;
font-weight: bold;
}

dd {
margin: 0;
flex-grow: 1;
width: calc(100% - 50px);
text-align: right;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
Expand Down
76 changes: 58 additions & 18 deletions src/components/DebugView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,72 @@ import './DebugView.css'

export class DebugView {
private element: HTMLElement
private eventTimestamps: number[] = []

constructor(parent: HTMLElement) {
this.element = document.createElement('div')
this.element.classList.add('maplibregl-ctrl')
this.element.innerHTML = `
<ul class="maplibregl-ctrl-compass-heading-debug">
<li><b>event</b>: <span class="event-name"></span></li>
<li><b>heading</b>: <span class="heading"></span></li>
<li><b>alpha</b>: <span class="alpha"></span></li>
<li><b>beta</b>: <span class="beta"></span></li>
<li><b>gamma</b>: <span class="gamma"></span></li>
</ul>
<dl class="maplibregl-ctrl-compass-heading-debug">
<dt>event</dt>
<dd class="event-type"></dd>
<dt>heading</dt>
<dd class="heading"></dd>
<dt>alpha</dt>
<dd class="alpha"></dd>
<dt>beta</dt>
<dd class="beta"></dd>
<dt>gamma</dt>
<dd class="gamma"></dd>
<dt>count</dt>
<dd class="count"></dd>
</dl>
`
parent.appendChild(this.element)
}

update(event?: CompassEvent) {
const eventName = this.element.querySelector('.event-name')
const headingSpan = this.element.querySelector('.heading')
const alphaSpan = this.element.querySelector('.alpha')
const betaSpan = this.element.querySelector('.beta')
const gammaSpan = this.element.querySelector('.gamma')
if (eventName) eventName.textContent = event?.originalEvent.type || ''
if (headingSpan) headingSpan.textContent = `${event?.heading || ''}`
if (alphaSpan) alphaSpan.textContent = `${event?.originalEvent.alpha || ''}`
if (betaSpan) betaSpan.textContent = `${event?.originalEvent.beta || ''}`
if (gammaSpan) gammaSpan.textContent = `${event?.originalEvent.gamma || ''}`
update(e?: CompassEvent) {
if (!e) {
this.clear()
return
}

const now = Date.now()
this.eventTimestamps.push(now)
this.eventTimestamps = this.eventTimestamps.filter((ts) => now - ts <= 1000)

const eventType = e.originalEvent.type
const heading = e.heading != null ? e.heading.toFixed(4) : ''
const alpha =
e.originalEvent.alpha != null ? e.originalEvent.alpha.toFixed(4) : ''
const beta =
e.originalEvent.beta != null ? e.originalEvent.beta.toFixed(4) : ''
const gamma =
e.originalEvent.gamma != null ? e.originalEvent.gamma.toFixed(4) : ''
const count = `${this.eventTimestamps.length} events/sec`

this.updateField('.event-type', eventType)
this.updateField('.heading', heading)
this.updateField('.alpha', alpha)
this.updateField('.beta', beta)
this.updateField('.gamma', gamma)
this.updateField('.count', count)
}

private clear() {
const fields = [
'.event-type',
'.heading',
'.alpha',
'.beta',
'.gamma',
'.count',
]
fields.forEach((selector) => this.updateField(selector, ''))
}

private updateField(selector: string, value: string) {
const element = this.element.querySelector(selector)
if (element) element.textContent = value
}
}
2 changes: 1 addition & 1 deletion tests/DebugView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('DebugView', () => {
} as WebkitDeviceOrientationEvent,
})
const headingSpan = parentElement.querySelector('.heading')
expect(headingSpan?.textContent).toBe('180.123')
expect(headingSpan?.textContent).toBe('180.1230')
})

it('should handle missing elements gracefully', () => {
Expand Down
Loading