Skip to content

Commit 2bf2826

Browse files
committed
feat!: api change and perf improvements
1 parent db3932e commit 2bf2826

File tree

3 files changed

+218
-227
lines changed

3 files changed

+218
-227
lines changed

src/example.vue

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
<script setup lang="ts">
1010
import { range } from 'lodash-es'
11-
import { onMounted, ref } from 'vue'
12-
import { snaproll, SnaprollActionType, type SnaprollSubscription } from './index'
13-
import { Pane } from 'tweakpane'
1411
import { mean } from 'simple-statistics'
12+
import { Pane } from 'tweakpane'
13+
import { onMounted, ref } from 'vue'
14+
import { Snaproll, SnaprollActionType, type SnaprollSubscription } from './index'
1515
1616
// class Estimation {
1717
// private currentCount = 0
@@ -88,7 +88,7 @@ class Estimation {
8888
}
8989
}
9090
91-
const loop = snaproll({ fps: 30 })
91+
const loop = new Snaproll({ fps: 30 })
9292
const boxRefs = ref<HTMLElement[]>([])
9393
9494
function lerp(v0: number, v1: number, t: number) {
@@ -110,10 +110,17 @@ const boxes = range(50).map((_, index) => {
110110
velocity,
111111
}
112112
113+
// 4 seconds
114+
const updateStepsMax = Math.round(4000 / loop.timestep)
115+
let updateSteps = 0
116+
113117
const subscription: SnaprollSubscription = (action) => {
114118
const element = boxRefs.value[box.index]
115119
116120
switch (action.type) {
121+
case SnaprollActionType.Begin:
122+
updateSteps = 0
123+
break
117124
case SnaprollActionType.Update:
118125
box.lastPosition = box.position
119126
box.position += box.velocity * action.timestep
@@ -125,10 +132,9 @@ const boxes = range(50).map((_, index) => {
125132
box.lastPosition,
126133
box.position,
127134
action.delta / action.timestep,
128-
// 1 - Math.exp(-lambda * action.delta),
129135
)}vw`
130136
131-
if (action.panic) {
137+
if (++updateSteps >= updateStepsMax) {
132138
loop.resetFrameDelta()
133139
}
134140
@@ -169,7 +175,6 @@ const observer = new PerformanceObserver((list) => {
169175
if (entry.entryType === 'measure') {
170176
durations.push(entry.duration)
171177
172-
173178
if (durations.length > 60) {
174179
durations.shift()
175180
}
@@ -192,7 +197,7 @@ const read = {
192197
get draw() {
193198
return estimationDraw.getFrequency()
194199
},
195-
get mean() {
200+
get perf() {
196201
try {
197202
return mean(durations) * 100
198203
} catch {
@@ -246,7 +251,7 @@ Object.keys(read).forEach((key) => {
246251
readonly: true,
247252
view: 'graph',
248253
min: 0,
249-
max: 200,
254+
max: 100,
250255
bufferSize: 100,
251256
})
252257
})

src/index.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { deferred } from '@escapace/sequentialize'
22
import { max, mean, median, min, standardDeviation } from 'simple-statistics'
33
import { assert, describe, it, vi } from 'vitest'
44
import {
5-
snaproll,
5+
Snaproll,
66
SnaprollActionType,
77
type SnaprollActionBegin,
88
type SnaprollActionDraw,
@@ -56,7 +56,7 @@ const createScenario = async () => {
5656

5757
const timestep = 1000 / 60
5858
const fps = 60
59-
const loop = snaproll({ fps, timestep })
59+
const loop = new Snaproll({ fps, timestep })
6060
const begin = vi.fn<[SnaprollActionBegin, number]>()
6161
const update = vi.fn<[SnaprollActionUpdate, number]>()
6262
const draw = vi.fn<[SnaprollActionDraw, number]>()

0 commit comments

Comments
 (0)