ziko
Version:
A versatile JavaScript library offering a rich set of Hyperscript Based UI components, advanced mathematical utilities, interactivity ,animations, client side routing and more ...
84 lines (83 loc) • 2.17 kB
Plain Text
import { useTimeLoop } from "../../Time";
import {
map,
complex,
} from "../../Math";
class ZikoScreenObserver {
constructor(callback=e=>console.log({x:e.x,y:e.y})) {
this.previousX = globalThis?.screenX;
this.previousY = globalThis?.screenY;
this.view=[-1,-1,1,1];
this.start(callback);
}
get xMin(){
return this.view[0];
}
get yMin(){
return this.view[1];
}
get xMax(){
return this.view[2];
}
get yMax(){
return this.view[3];
}
get x(){
return globalThis?.screenX;
}
get y(){
return globalThis?.screenY;
}
get scx(){
return screen.availWidth/2;
}
get scy(){
return screen.availHeight/2;
}
get wcx(){
return screenX+globalThis?.innerWidth/2;
}
get wcx_v(){
return map(this.wcx,0,screen.availWidth,this.view[0],this.view[2]);
}
get wcy(){
return screenY+globalThis?.innerHeight/2;
}
get wcy_v(){
return -map(this.wcy,0,screen.availHeight,this.view[1],this.view[3]);
}
get dx(){
return map(this.x,0,screen.availWidth,this.xMin,this.xMax);
}
get dy(){
return map(this.y,0,screen.availHeight,this.yMin,this.yMax);
}
start(callback){
this.loop = useTimeLoop(()=>{
let currentX = globalThis?.screenX;
let currentY = globalThis?.screenY;
if (this.previousX !== currentX || this.previousY !== currentY) {
callback(this)
this.previousX = currentX;
this.previousY = currentY;
}
},1000/60,0,Infinity,true);
return this;
}
}
const watchScreen=(callback)=>new ZikoScreenObserver(callback);
// globalThis.watchScreen=watchScreen;
export{
watchScreen
}
/*
a=ul("x","y","dx","dy","wcx","wxy")
watchScreen(e=>{
a[0].setValue("x : "+e.x)
a[1].setValue("y : "+e.y)
a[2].setValue("dx : "+e.dx)
a[3].setValue("dy : "+e.dy)
a[4].setValue("wCx : "+e.wcx)
a[5].setValue("wCy : "+e.wcy)
})
*/