import { timedResult } from "@thi.ng/bench/timed" ; import { KdTreeMap } from "@thi.ng/geom-accel/kd-tree-map" ; import { canvas } from "@thi.ng/hdom-canvas" ; import { gestureStream } from "@thi.ng/rstream-gestures" ; import { CloseMode } from "@thi.ng/rstream/api" ; import { StreamSync , sync } from "@thi.ng/rstream/sync" ; import { trigger } from "@thi.ng/rstream/trigger" ; import { updateDOM } from "@thi.ng/transducers-hdom" ; import { map } from "@thi.ng/transducers/map" ; import { mapcat } from "@thi.ng/transducers/mapcat" ; import type { Vec } from "@thi.ng/vectors" ; const app = ( main ) => { const _canvas = { ... canvas , init : ( el ) => main . add ( gestureStream ( el ) . transform ( map ( ( g ) => g . pos ) ) , "mpos" ) , } ; const width = window . innerWidth ; const height = window . innerHeight ; let tree = new KdTreeMap < Vec , number > ( 2 ) ; return ( { mpos } ) => { if ( ! ( tree . size % 500 ) ) { tree = tree . copy ( ) ; } mpos = mpos || [ width / 2 , height / 2 ] ; tree . set ( mpos , 1.5 + Math . random ( ) * 5 ) ; let [ selected , t1 ] = timedResult ( ( ) => tree . query ( mpos , width / 4 , 200 ) ) ; let [ neighbors , t2 ] = timedResult ( ( ) => [ ... mapcat ( ( p ) => tree . queryKeys ( p [ 0 ] , width / 4 , 8 ) . map ( ( q ) => [ "line" , { } , p [ 0 ] , q ] ) , selected ) , ] ) ; return [ "div.overflow-hidden.sans-serif.f7" , [ "div" , `Points: ${ tree . size }, Sel: ${ selected . length }, ` , `Neighbors: ${ neighbors . length }, Q1: ${ t1 }ms, Q2: ${ t2 }ms, ` , `Height: ${ tree . height }, Ratio: ${ tree . ratio . toFixed ( 2 ) }` , ] , [ _canvas , { width , height , __diff : false , __normalize : false } , [ "points" , { fill : "black" , size : 2 } , tree . keys ( ) ] , [ "g" , { fill : "rgba(0,192,255,0.5)" } , ... selected . map ( ( p ) => [ "circle" , { } , p [ 0 ] , p [ 1 ] ] ) , ] , [ "g" , { stroke : "rgba(0,0,0,0.25)" } , ... neighbors ] , ] , ] ; } ; } ; const main = sync < any , any > ( { src : { trigger : trigger ( ) } , closeIn : CloseMode . NEVER , } ) ; main . transform ( map ( app ( main ) ) , updateDOM ( ) ) ;