Skip to content

Commit

Permalink
Add zeitgeber.iteration, .passed
Browse files Browse the repository at this point in the history
- compose PercentElement into TickElement
- replace css with tailwind classes
  • Loading branch information
Phoscur committed May 4, 2024
1 parent b0e8905 commit 0cdec7f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 47 deletions.
1 change: 0 additions & 1 deletion src/app/app.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class AppElement extends HTMLElement {
<debug-ctx>
<app-clock></app-clock>
</debug-ctx>
<app-percent></app-percent>
</div>
<div class="container">
<div id="welcome" class="h-8 mb-16 mx-auto grid grid-flow-col gap-4 auto-cols-max">
Expand Down
38 changes: 32 additions & 6 deletions src/app/clock.element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { raw } from 'hono/html';
import { injectable, inject } from '@joist/di';
import './clock.element.css';
import './percent.element.css';
import './tick.element.css';
import { Debug } from './debug.element';
import { Zeitgeber } from './signals/zeitgeber';

Expand Down Expand Up @@ -46,25 +45,45 @@ export class ClockElement extends HTMLElement {

export const Percent = () => (
<>
<span class="percent"></span>
<span class="percent grid font-bold text-4xl p-4" style="transition: --percent 250ms"></span>
</>
);

export class PercentElement extends HTMLElement {
public static observedAttributes = [];
public static observedAttributes = ['value', 'speedms'];

connectedCallback() {
const html = Percent();
this.innerHTML = raw(html);

const value = this.attributes.getNamedItem('value')?.value || `${Math.random()}`;

const percent = this.getElementsByClassName('percent')[0] as HTMLElement;
percent.style.setProperty('--percent', value);
}

attributeChangedCallback(name: string, oldValue: string, newValue: string) {
const percent = this.getElementsByClassName('percent')[0] as HTMLElement;
percent.style.setProperty('--percent', `${Math.random()}`);
if (!percent) {
return;
}
if (name === 'value') {
percent.style.setProperty('--percent', newValue);
}
if (name === 'speedms') {
percent.style.setProperty('transition', `--percent ${newValue}ms`);
}
}
}

export const Tick = () => (
<>
<span class="tick"></span>
<div class="flex flex-row">
<span class="tick grid font-bold text-4xl p-4" style="transition: --tick 1s">
[-1]
</span>
<app-percent value="0.99" speedms="250"></app-percent>
</div>
</>
);

Expand All @@ -83,8 +102,15 @@ export class TickElement extends HTMLElement {

zeit.effect(() => {
const content = this.getElementsByClassName('tick')[0] as HTMLElement;
const percent = this.getElementsByTagName('app-percent')[0] as PercentElement;
const passed = zeit.passed;
//percent.setAttribute('value', `${passed <= 0.01 ? 0.1 : passed}`); // rather show 10 than 0
percent.setAttribute('value', `${passed}`);
percent.setAttribute('speedms', `${zeit.msPerIteration}`);
content.style.setProperty('--tick', `${zeit.tick}`);
logger.log('TICK', zeit.tick);
content.style.setProperty('transition', `--tick ${zeit.msPerIteration}ms`);
content.innerHTML = `[${zeit.tick}]`;
logger.log('TICK', zeit.tick, zeit.iteration, passed);
});

setTimeout(() => {
Expand Down
6 changes: 1 addition & 5 deletions src/app/percent.element.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@
}

.percent {
font: 800 40px monospace;
padding: 1ch;
display: grid;
transition: --percent 1s;
--temp: calc(var(--percent) * 100);
--v1: max(var(--temp) - 0.5, 0);
--v2: max((var(--temp) - var(--v1)) * 100 - 0.5, 0);
counter-reset: v1 var(--v1) v2 var(--v2);
}
.percent::before {
content: counter(v1) '.' counter(v2, decimal-leading-zero) '%';
content: counter(v1, decimal-leading-zero) '.' counter(v2, decimal-leading-zero) '%';
}

/*
Expand Down
13 changes: 12 additions & 1 deletion src/app/signals/zeitgeber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class Zeitgeber {
private zeitgeist: {
tick: Signal.State<number>;
time: Signal.State<number>;
iteration: Signal.State<number>;
};
private timeoutId?: number;

Expand All @@ -25,7 +26,7 @@ export class Zeitgeber {

constructor(
private currentTick = 0,
public readonly msPerTick = 1000,
public readonly msPerTick = 10000,
public readonly msPerIteration = 1000,
public readonly timeSource = () => Date.now(),
private currentTime = timeSource(),
Expand All @@ -35,6 +36,7 @@ export class Zeitgeber {
this.zeitgeist = {
tick: new Signal.State(currentTick),
time: new Signal.State(currentTime),
iteration: new Signal.State(currentTime),
};
}

Expand All @@ -46,6 +48,10 @@ export class Zeitgeber {
return this.zeitgeist.time.get();
}

get iteration(): number {
return this.zeitgeist.iteration.get();
}

get now() {
return this.currentTime;
}
Expand All @@ -54,12 +60,17 @@ export class Zeitgeber {
return this.time + this.msPerTick;
}

get passed(): number {
return (this.timeSource() - this.currentTime) / this.msPerTick;
}

get running() {
return !!this.timeoutId;
}

private timeloop() {
const now = this.timeSource();
this.zeitgeist.iteration.set(now);
const diff = now - this.currentTime;
const ticks = Math.floor(diff / this.msPerTick);
this.currentTime += ticks * this.msPerTick;
Expand Down
34 changes: 0 additions & 34 deletions src/app/tick.element.css

This file was deleted.

0 comments on commit 0cdec7f

Please sign in to comment.