ziko 0.39.0 → 0.40.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,87 +1,103 @@
1
- class ZikoTimeLoop {
2
- constructor(callback, step = 1000/30, startTime=0, endTime = Infinity, started = true) {
1
+ export class TimeLoop {
2
+ constructor(callback, { step = 1000, t0 = 0, t1 = Infinity, autoplay = true } = {}) {
3
3
  this.callback = callback;
4
4
  this.cache = {
5
5
  isRunning: false,
6
- AnimationId : null,
7
- t0 : null,
6
+ id: null,
7
+ last_tick: null,
8
8
  step,
9
- // fps,
10
- startTime,
11
- endTime,
12
- started
9
+ t0,
10
+ t1,
11
+ autoplay,
12
+ pauseTime: null,
13
+ frame : 0,
13
14
  };
14
- this.init();
15
- this.i=0;
16
- }
17
- init(){
18
- // if(this.cache.step && this.cache.fps){
19
- // console.warn(`Fps will be adjusted from ${this.cache.fps} to ${1000/this.cache.step} to ensure a smoother animation`);
20
- // this.cache.fps=1000/this.cache.step;
21
- // }
22
- if(this.cache.started){
23
- this.cache.startTime?this.startAfter(this.cache.startTime):this.start();
24
- if(this.cache.endTime&&this.cache.endTime!==Infinity)this.stopAfter(this.cache.endTime);
15
+
16
+ if (autoplay) {
17
+ t0 ? this.startAfter(t0) : this.start();
18
+ if (t1 !== Infinity) this.stopAfter(t1);
25
19
  }
26
- return this;
27
20
  }
28
- // get TIME_STEP() {
29
- // // return this.cache.step?this.cache.step:1000 / this.cache.fps;
30
- // return this.cache.step;
31
- // }
21
+
22
+ get frame(){
23
+ return this.cache.frame;
24
+ }
25
+ get elapsed(){
26
+ return this.cache.elapsed;
27
+ }
28
+
32
29
  start() {
33
30
  if (!this.cache.isRunning) {
34
- this.i=0;
31
+ this.cache.frame = 0;
35
32
  this.cache.isRunning = true;
36
- this.cache.t0 = Date.now();
33
+ this.cache.last_tick = Date.now();
37
34
  this.animate();
38
35
  }
39
36
  return this;
40
37
  }
38
+
41
39
  pause() {
42
40
  if (this.cache.isRunning) {
43
- clearTimeout(this.cache.AnimationId);
41
+ clearTimeout(this.cache.id);
44
42
  this.cache.isRunning = false;
43
+ this.cache.pauseTime = Date.now();
45
44
  }
46
45
  return this;
47
46
  }
48
- stop(){
49
- this.pause();
50
- this.i=0;
47
+
48
+ resume() {
49
+ if (!this.cache.isRunning) {
50
+ this.cache.isRunning = true;
51
+ if (this.cache.pauseTime) {
52
+ // adjust start time so delta stays consistent
53
+ const pausedDuration = Date.now() - this.cache.pauseTime;
54
+ this.cache.last_tick += pausedDuration;
55
+ }
56
+ this.animate();
57
+ }
51
58
  return this;
52
59
  }
53
- resume(){
54
- this.cache.isRunning=true;
55
- this.animate();
60
+
61
+ stop() {
62
+ this.pause();
63
+ this.cache.frame = 0;
56
64
  return this;
57
65
  }
58
- startAfter(t=1000){
59
- setTimeout(this.start.bind(this),t);
66
+
67
+ startAfter(t = 1000) {
68
+ setTimeout(() => this.start(), t);
60
69
  return this;
61
70
  }
62
- stopAfter(t=1000){
63
- setTimeout(this.stop.bind(this),t);
64
- return this;
71
+
72
+ stopAfter(t = 1000) {
73
+ setTimeout(() => this.stop(), t);
74
+ return this;
65
75
  }
76
+
66
77
  animate = () => {
67
78
  if (this.cache.isRunning) {
68
79
  const now = Date.now();
69
- const delta = now - this.cache.t0;
70
- if (delta > this.cache.step) {
80
+ const delta = now - this.cache.last_tick;
81
+
82
+ if (delta >= this.cache.step) {
83
+ this.cache.elapsed = now - (this.cache.t0 || 0);
71
84
  this.callback(this);
72
- this.i++;
73
- this.cache.t0 = now - (delta % this.cache.step);
85
+ this.cache.frame++;
86
+ this.cache.last_tick = now - (delta % this.cache.step);
74
87
  }
75
- this.cache.AnimationId = setTimeout(this.animate, 0);
76
- };
88
+
89
+ this.cache.id = setTimeout(this.animate, 0);
90
+ }
77
91
  }
78
92
  }
79
- const useFps = fps => 1000/fps;
80
- const useTimeLoop = (callback, step, startTime, endTime, started) => new ZikoTimeLoop(callback, step, startTime, endTime, started);
81
93
 
94
+ export const loop = (callback, options = {}) => new TimeLoop(callback, options);
95
+
96
+
97
+ // Helpers
98
+ // const useFps = (fps) => 1000 / fps;
99
+
100
+ // const _loop = loop( e => {
101
+ // console.log("Frame:", e.frame, " Elapsed: ", e.elapsed);
102
+ // });
82
103
 
83
- //const a = useTimeLoop((e) => console.log(e.i), {step:100,fps:30,t:[500,4001],start:true});
84
- export {
85
- useTimeLoop,
86
- useFps
87
- }
@@ -1,4 +1,4 @@
1
- import Ease from "./ease.js";
1
+ // import Ease from "./ease.js";
2
2
  const wait=(delayInMS)=>{
3
3
  return new Promise((resolve) => setTimeout(resolve, delayInMS));
4
4
  }
@@ -11,7 +11,7 @@ const timeTaken = callback => {
11
11
  export{
12
12
  wait,
13
13
  timeTaken,
14
- Ease
14
+ // Ease
15
15
  }
16
16
  export * from "./performance.js";
17
17
  export * from "./ui.js"
@@ -1,5 +1,8 @@
1
- import { Str } from "../../data";
2
1
  import { isStateGetter } from "../../hooks/use-state.js";
2
+ import {
3
+ is_camelcase,
4
+ camel2hyphencase
5
+ } from '../../data/string/index.js'
3
6
 
4
7
  // To Do add getter, watchAttr
5
8
  export const AttrsMethods = {
@@ -22,7 +25,7 @@ export const AttrsMethods = {
22
25
  return this;
23
26
  },
24
27
  getAttr(name){
25
- name = Str.isCamelCase(name) ? Str.camel2hyphencase(name) : name;
28
+ name = is_camelcase(name) ? camel2hyphencase(name) : name;
26
29
  return this.element.attributes[name].value;
27
30
  },
28
31
  setContentEditable(bool = true) {
@@ -32,7 +35,7 @@ export const AttrsMethods = {
32
35
  };
33
36
 
34
37
  function _set_attrs_(name, value){
35
- if(this.element?.tagName !== "svg") name = Str.isCamelCase(name) ? Str.camel2hyphencase(name) : name;
38
+ if(this.element?.tagName !== "svg") name = is_camelcase(name) ? camel2hyphencase(name) : name;
36
39
  if(this?.attr[name] && this?.attr[name]===value) return;
37
40
  if(isStateGetter(value)){
38
41
  const getter = value()
@@ -68,7 +68,7 @@ export const DomMethods = {
68
68
 
69
69
  };
70
70
 
71
- function __addItem__(adder, pusher, ...ele) {
71
+ async function __addItem__(adder, pusher, ...ele) {
72
72
  if (this.cache.isFrozzen) {
73
73
  console.warn("You can't append new item to frozzen element");
74
74
  return this;
@@ -94,6 +94,13 @@ function __addItem__(adder, pusher, ...ele) {
94
94
  ele[i].target = this.element;
95
95
  this.items[pusher](ele[i]);
96
96
  }
97
+ else if(ele[i] instanceof Promise){
98
+ const UIEle = await ele[i]
99
+ UIEle.cache.parent = this;
100
+ this.element?.[adder](UIEle.element);
101
+ UIEle.target = this.element;
102
+ this.items[pusher](UIEle)
103
+ }
97
104
  else if (ele[i] instanceof Object) {
98
105
  if (ele[i]?.style) this.style(ele[i]?.style);
99
106
  if (ele[i]?.attr) {
@@ -1,14 +1,34 @@
1
- // Process width and height
1
+ import { isStateGetter } from '../../hooks/use-state.js'
2
2
  export const StyleMethods = {
3
3
  style(styles){
4
- Object.assign(this.element.style, styles)
4
+ for(let key in styles){
5
+ const value = styles[key];
6
+ if(isStateGetter(value)){
7
+ const getter = value()
8
+ Object.assign(this.element.style, {[key] : getter.value})
9
+ getter._subscribe(
10
+ (newValue) => {
11
+ console.log({newValue})
12
+ Object.assign(this.element.style, {[key] : newValue})
13
+ },
14
+ // this
15
+ );
16
+ }
17
+ else Object.assign(this.element.style, {[key] : value})
18
+ }
5
19
  return this;
6
20
  },
7
21
  size(width, height){
8
22
  return this.style({width, height})
23
+ },
24
+ hide(){
25
+
26
+ },
27
+ show(){
28
+
9
29
  },
10
30
  animate(keyframe, {duration=1000, iterations=1, easing="ease"}={}){
11
31
  this.element?.animate(keyframe,{duration, iterations, easing});
12
32
  return this;
13
- }
33
+ }
14
34
  }
@@ -1,6 +1,7 @@
1
1
  import { addSuffixeToNumber } from "../../__utils__/index.js";
2
2
  import { Matrix,cos,sin} from "../../../math/index.js";
3
3
  import { Str } from "../../../data/index.js";
4
+ import { camel2hyphencase } from '../../../data/string/converters.js'
4
5
  class ZikoUIElementStyle{
5
6
  constructor(defaultStyle={}){
6
7
  this.target=null;
@@ -25,7 +26,7 @@ class ZikoUIElementStyle{
25
26
  for(const [key, value] of Object.entries(styles)){
26
27
  if(Str.isCamelCase(key)){
27
28
  delete styles[key];
28
- Object.assign(styles,{[Str.camel2hyphencase(key)]:value})
29
+ Object.assign(styles,{[camel2hyphencase(key)]:value})
29
30
  }
30
31
  }
31
32
  if(this?.target?.element?.style)Object.assign(this?.target?.element?.style, styles);
@@ -19,18 +19,14 @@ const tags = new Proxy({}, {
19
19
  let type ;
20
20
  if(HTMLTags.includes(tag)) type = 'html'
21
21
  if(SVGTags.includes(tag)) type = 'svg'
22
- if(HTMLTags.includes(tag)) return (...args)=>{
22
+ return (...args)=>{
23
23
  console.log(isStateGetter(args[0]))
24
- // if(typeof args[0] === 'function') {
25
- // console.log(args[0], args[0]?.() instanceof StateGetter)
26
- // globalThis.a = args[0]
27
- // console.log({t : a.constructor})
28
- // }
29
24
  if(
30
25
  ['string', 'number'].includes(typeof args[0])
31
26
  || args[0] instanceof UIElement
32
27
  || (typeof args[0] === 'function' && args[0]().isStateGetter())
33
- ) return new UIElement({element :tag, name : tag, type}).append(...args);
28
+ ) return new UIElement({element : tag, name : tag, type}).append(...args);
29
+ // console.log(args[0])
34
30
  return new UIElement({element : tag}).setAttr(args.shift()).append(...args)
35
31
  }
36
32
  // if(SVGTags.includes(tag)) return (...args) => new UIElement(tag,"",{el_type : "svg"}).append(...args);
@@ -1,144 +0,0 @@
1
- const Ease={
2
- Linear:function(t){
3
- return t;
4
- },
5
- InSin(t){
6
- return 1 - Math.cos((t * Math.PI) / 2);
7
- },
8
- OutSin(t){
9
- return Math.sin((t * Math.PI) / 2);
10
- },
11
- InOutSin(t){
12
- return -(Math.cos(Math.PI * t) - 1) / 2;
13
- },
14
- InQuad(t){
15
- return t**2;
16
- },
17
- OutQuad(t){
18
- return 1 - Math.pow((1 - t),2)
19
- },
20
- InOutQuad(t){
21
- return t < 0.5 ? 2 * Math.pow(t,2) : 1 - Math.pow(-2 * t + 2, 2) / 2;
22
- },
23
- InCubic(t){
24
- return t**3;
25
- },
26
- OutCubic(t){
27
- return 1 - Math.pow((1 - t),3)
28
- },
29
- InOutCubic(t){
30
- return t < 0.5 ? 4 * Math.pow(t,3) : 1 - Math.pow(-2 * t + 2, 3) / 2;
31
- },
32
- InQuart(t){
33
- return t**4;
34
- },
35
- OutQuart(t){
36
- return 1 - Math.pow((1 - t),4);
37
- },
38
- InOutQuart(t){
39
- return t < 0.5 ? 8 * Math.pow(t,4) : 1 - Math.pow(-2 * t + 2, 4) / 2;
40
- },
41
- InQuint(t){
42
- return t**5;
43
- },
44
- OutQuint(t){
45
- return 1 - Math.pow((1 - t),5);
46
- },
47
- InOutQuint(t){
48
- return t < 0.5 ? 16 * Math.pow(t,5) : 1 - Math.pow(-2 * t + 2, 5) / 2;
49
- },
50
- InExpo(t){
51
- return t === 0 ? 0 : Math.pow(2, 10 * t - 10);
52
- },
53
- OutExpo(t){
54
- return t === 1 ? 1 : 1 - Math.pow(2, -10 * t);
55
- },
56
- InOutExpo(t){
57
- return t === 0? 0: t === 1? 1: t < 0.5 ? Math.pow(2, 20 * t - 10) / 2: (2 - Math.pow(2, -20 * t + 10)) / 2;
58
- },
59
- InCirc(t){
60
- return 1 - Math.sqrt(1 - Math.pow(t, 2));
61
- },
62
- OutCirc(t){
63
- return Math.sqrt(1 - Math.pow(t - 1, 2));
64
- },
65
- InOutCic(t){
66
- return t < 0.5? (1 - Math.sqrt(1 - Math.pow(2 * t, 2))) / 2: (Math.sqrt(1 - Math.pow(-2 * t + 2, 2)) + 1) / 2;
67
- },
68
- Arc(t){
69
- return 1 - Math.sin(Math.acos(t));
70
- },
71
- Back(t){
72
- // To Be Changed
73
- let x=1
74
- return Math.pow(t, 2) * ((x + 1) * t - x);
75
- },
76
- Elastic(t){
77
- return -2*Math.pow(2, 10 * (t - 1)) * Math.cos(20 * Math.PI * t / 3 * t);
78
- },
79
- InBack(t){
80
- const c1 = 1.70158;
81
- const c3 = c1 + 1;
82
- return c3 *Math.pow(t,3)- c1 * (t**2);
83
- },
84
- OutBack(t){
85
- const c1 = 1.70158;
86
- const c3 = c1 + 1;
87
- return 1 + c3 * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2);
88
- },
89
- InOutBack(t){
90
- const c1 = 1.70158;
91
- const c2 = c1 * 1.525;
92
- return t < 0.5
93
- ? (Math.pow(2 * t, 2) * ((c2 + 1) * 2 * t - c2)) / 2
94
- : (Math.pow(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2;
95
- },
96
- InElastic(t){
97
- const c4 = (2 * Math.PI) / 3;return t === 0
98
- ? 0
99
- : t === 1
100
- ? 1
101
- : -Math.pow(2, 10 * t - 10) * Math.sin((t * 10 - 10.75) * c4);
102
- },
103
- OutElastic(t){
104
- const c4 = (2 * Math.PI) / 3;
105
- return t === 0
106
- ? 0
107
- : t === 1
108
- ? 1
109
- : Math.pow(2, -10 * t) * Math.sin((t * 10 - 0.75) * c4) + 1;
110
- },
111
- InOutElastic(t){
112
- const c5 = (2 * Math.PI) / 4.5;
113
- return t === 0
114
- ? 0
115
- : t === 1
116
- ? 1
117
- : t < 0.5
118
- ? -(Math.pow(2, 20 * t - 10) * Math.sin((20 * t - 11.125) * c5)) / 2
119
- : (Math.pow(2, -20 * t + 10) * Math.sin((20 * t - 11.125) * c5)) / 2 + 1;
120
- },
121
- InBounce(t){
122
- return 1 - Ease.OutBounce(1-t);
123
- },
124
- OutBounce(t){
125
- const n1 = 7.5625;
126
- const d1 = 2.75;
127
- if (t < 1 / d1) {
128
- return n1 * t * t;
129
- } else if (t < 2 / d1) {
130
- return n1 * (t -= 1.5 / d1) * t + 0.75;
131
- } else if (t < 2.5 / d1) {
132
- return n1 * (t -= 2.25 / d1) * t + 0.9375;
133
- } else {
134
- return n1 * (t -= 2.625 / d1) * t + 0.984375;
135
- }
136
-
137
- },
138
- InOutBounce(t){
139
- return t < 0.5
140
- ? (1 - Ease.OutBounce(1 - 2 * t)) / 2
141
- : (1 + Ease.OutBounce(2 * t - 1)) / 2;
142
- }
143
- }
144
- export default Ease
File without changes
File without changes