返回 html-ppt-skill
_util.js
根目录 / assets / animations / fx / _util.js
1 /* html-ppt fx :: shared helpers */
2 (function(){
3 window.HPX = window.HPX || {};
4 const U = window.HPX._u = {};
5
6 U.css = (el, name, fb) => {
7 const v = getComputedStyle(el).getPropertyValue(name).trim();
8 return v || fb;
9 };
10
11 U.accent = (el, fb) => U.css(el, '--accent', fb || '#7c5cff');
12 U.accent2 = (el, fb) => U.css(el, '--accent-2', fb || '#22d3ee');
13 U.text = (el, fb) => U.css(el, '--text-1', fb || '#eaeaf2');
14
15 U.palette = (el) => [
16 U.accent(el, '#7c5cff'),
17 U.accent2(el, '#22d3ee'),
18 U.css(el, '--ok', '#22c55e'),
19 U.css(el, '--warn', '#f59e0b'),
20 U.css(el, '--danger', '#ef4444'),
21 ];
22
23 U.canvas = (el) => {
24 if (getComputedStyle(el).position === 'static') el.style.position = 'relative';
25 const c = document.createElement('canvas');
26 c.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;pointer-events:none;display:block;';
27 el.appendChild(c);
28 const ctx = c.getContext('2d');
29 let w = 0, h = 0, dpr = Math.max(1, Math.min(2, window.devicePixelRatio||1));
30 const fit = () => {
31 const r = el.getBoundingClientRect();
32 w = Math.max(1, r.width|0);
33 h = Math.max(1, r.height|0);
34 c.width = (w*dpr)|0;
35 c.height = (h*dpr)|0;
36 ctx.setTransform(dpr,0,0,dpr,0,0);
37 };
38 fit();
39 const ro = new ResizeObserver(fit);
40 ro.observe(el);
41 return {
42 c, ctx,
43 get w(){return w;}, get h(){return h;}, get dpr(){return dpr;},
44 destroy(){
45 try{ro.disconnect();}catch(e){}
46 if (c.parentNode) c.parentNode.removeChild(c);
47 }
48 };
49 };
50
51 U.loop = (fn) => {
52 let raf = 0, stopped = false, t0 = performance.now();
53 const tick = (t) => {
54 if (stopped) return;
55 fn((t - t0)/1000);
56 raf = requestAnimationFrame(tick);
57 };
58 raf = requestAnimationFrame(tick);
59 return () => { stopped = true; cancelAnimationFrame(raf); };
60 };
61
62 U.rand = (a,b) => a + Math.random()*(b-a);
63 })();
64
64 lines JAVASCRIPT