magic-glyphs 0.7.0 → 0.8.0
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.
- package/LICENSE.md +1 -1
- package/README.md +126 -89
- package/cli/index.js +32 -13
- package/cli/video.js +36 -0
- package/data/icons/apple.json +1 -0
- package/data/icons/arrow-down.json +1 -0
- package/data/icons/arrow-left.json +1 -0
- package/data/icons/arrow-right.json +1 -0
- package/data/icons/arrow-up.json +1 -0
- package/data/icons/bag.json +1 -0
- package/data/icons/battery.json +1 -0
- package/data/icons/bell.json +1 -0
- package/data/icons/bike.json +1 -0
- package/data/icons/bird.json +1 -0
- package/data/icons/bolt.json +1 -0
- package/data/icons/book.json +1 -0
- package/data/icons/bookmark.json +1 -0
- package/data/icons/briefcase.json +1 -0
- package/data/icons/bug.json +1 -0
- package/data/icons/bus.json +1 -0
- package/data/icons/cake.json +1 -0
- package/data/icons/calendar.json +1 -0
- package/data/icons/camera.json +1 -0
- package/data/icons/car.json +1 -0
- package/data/icons/card.json +1 -0
- package/data/icons/cart.json +1 -0
- package/data/icons/cat.json +1 -0
- package/data/icons/chat.json +1 -0
- package/data/icons/check.json +1 -0
- package/data/icons/chevron-down.json +1 -0
- package/data/icons/chevron-left.json +1 -0
- package/data/icons/chevron-right.json +1 -0
- package/data/icons/chevron-up.json +1 -0
- package/data/icons/circle-alert.json +1 -0
- package/data/icons/circle-check.json +1 -0
- package/data/icons/circle-minus.json +1 -0
- package/data/icons/circle-plus.json +1 -0
- package/data/icons/circle-x.json +1 -0
- package/data/icons/clipboard.json +1 -0
- package/data/icons/clock.json +1 -0
- package/data/icons/cloud.json +1 -0
- package/data/icons/code.json +1 -0
- package/data/icons/coffee.json +1 -0
- package/data/icons/coin.json +1 -0
- package/data/icons/copy.json +1 -0
- package/data/icons/database.json +1 -0
- package/data/icons/dog.json +1 -0
- package/data/icons/download.json +1 -0
- package/data/icons/drop.json +1 -0
- package/data/icons/edit.json +1 -0
- package/data/icons/external-link.json +1 -0
- package/data/icons/eye-off.json +1 -0
- package/data/icons/eye.json +1 -0
- package/data/icons/file.json +1 -0
- package/data/icons/filter.json +1 -0
- package/data/icons/fish.json +1 -0
- package/data/icons/flag.json +1 -0
- package/data/icons/flame.json +1 -0
- package/data/icons/folder.json +1 -0
- package/data/icons/frown.json +1 -0
- package/data/icons/gift.json +1 -0
- package/data/icons/globe.json +1 -0
- package/data/icons/hand.json +1 -0
- package/data/icons/heart.json +1 -0
- package/data/icons/help.json +1 -0
- package/data/icons/home.json +1 -0
- package/data/icons/image.json +1 -0
- package/data/icons/info.json +1 -0
- package/data/icons/key.json +1 -0
- package/data/icons/laptop.json +1 -0
- package/data/icons/laugh.json +1 -0
- package/data/icons/leaf.json +1 -0
- package/data/icons/link.json +1 -0
- package/data/icons/lock.json +1 -0
- package/data/icons/mail.json +1 -0
- package/data/icons/map.json +1 -0
- package/data/icons/menu.json +1 -0
- package/data/icons/mic.json +1 -0
- package/data/icons/minus.json +1 -0
- package/data/icons/monitor.json +1 -0
- package/data/icons/moon.json +1 -0
- package/data/icons/music.json +1 -0
- package/data/icons/package.json +1 -0
- package/data/icons/pause.json +1 -0
- package/data/icons/pencil.json +1 -0
- package/data/icons/phone.json +1 -0
- package/data/icons/pin.json +1 -0
- package/data/icons/pizza.json +1 -0
- package/data/icons/plane.json +1 -0
- package/data/icons/play.json +1 -0
- package/data/icons/plus.json +1 -0
- package/data/icons/printer.json +1 -0
- package/data/icons/receipt.json +1 -0
- package/data/icons/refresh.json +1 -0
- package/data/icons/rocket.json +1 -0
- package/data/icons/save.json +1 -0
- package/data/icons/search.json +1 -0
- package/data/icons/server.json +1 -0
- package/data/icons/settings.json +1 -0
- package/data/icons/share.json +1 -0
- package/data/icons/shield.json +1 -0
- package/data/icons/smile.json +1 -0
- package/data/icons/star.json +1 -0
- package/data/icons/stop.json +1 -0
- package/data/icons/store.json +1 -0
- package/data/icons/sun.json +1 -0
- package/data/icons/tablet.json +1 -0
- package/data/icons/tag.json +1 -0
- package/data/icons/terminal.json +1 -0
- package/data/icons/thumbs-down.json +1 -0
- package/data/icons/thumbs-up.json +1 -0
- package/data/icons/train.json +1 -0
- package/data/icons/trash.json +1 -0
- package/data/icons/tree.json +1 -0
- package/data/icons/truck.json +1 -0
- package/data/icons/upload.json +1 -0
- package/data/icons/user.json +1 -0
- package/data/icons/users.json +1 -0
- package/data/icons/video.json +1 -0
- package/data/icons/volume.json +1 -0
- package/data/icons/wallet.json +1 -0
- package/data/icons/wave.json +1 -0
- package/data/icons/wifi.json +1 -0
- package/data/icons/wink.json +1 -0
- package/data/index.json +1 -1
- package/docs/avalonia.md +15 -0
- package/docs/cli.md +1 -1
- package/docs/figma.md +12 -0
- package/docs/flutter.md +18 -0
- package/docs/godot.md +13 -0
- package/docs/images-video.md +42 -0
- package/docs/jetpack-compose.md +39 -0
- package/docs/maker.md +64 -0
- package/docs/maui.md +17 -0
- package/docs/more-frameworks.md +49 -0
- package/docs/native-lottie.md +5 -0
- package/docs/qt.md +26 -0
- package/docs/react-native.md +34 -0
- package/docs/react-vue.md +12 -1
- package/docs/rust.md +43 -0
- package/docs/site-builders.md +27 -0
- package/docs/sound.md +2 -2
- package/docs/swiftui.md +22 -0
- package/docs/tauri-rust.md +15 -0
- package/docs/unity.md +15 -0
- package/docs/windows.md +23 -0
- package/lib.js +9 -1
- package/mcp/server.js +1 -1
- package/package.json +48 -11
- package/shopify/magic-glyph.liquid +8 -6
- package/tools/apng.js +36 -0
- package/tools/baked.js +30 -0
- package/tools/gif.js +60 -0
- package/tools/motion.js +31 -0
- package/tools/native.js +208 -0
- package/tools/native2.js +432 -0
- package/tools/raster.js +139 -0
- package/web/magic-glyphs.all.js +4 -3
- package/web/runtime.js +4 -3
- package/wordpress/magic-glyphs/magic-glyphs.all.js +4 -3
- package/wordpress/magic-glyphs/magic-glyphs.php +19 -6
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "magic-glyphs",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Animated
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "Animated icon library with sound: spring-animated SVG icons for React, Next.js, Vue, Nuxt, Svelte, Astro, shadcn, HTML, Tailwind, Lottie (iOS, Android, Flutter, Windows) and a CLI and MCP server. 120 icons free.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
6
|
-
"author":
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Magic Glyphs",
|
|
8
|
+
"url": "https://magicglyphs.com"
|
|
9
|
+
},
|
|
7
10
|
"main": "lib.js",
|
|
8
11
|
"bin": {
|
|
9
12
|
"magic-glyphs": "cli/index.js"
|
|
@@ -23,8 +26,12 @@
|
|
|
23
26
|
"default": "./vue/index.js"
|
|
24
27
|
},
|
|
25
28
|
"./lottie": "./tools/lottie.js",
|
|
29
|
+
"./gif": "./tools/gif.js",
|
|
30
|
+
"./apng": "./tools/apng.js",
|
|
31
|
+
"./raster": "./tools/raster.js",
|
|
26
32
|
"./node": "./lib.js",
|
|
27
33
|
"./icons.json": "./data/icons.json",
|
|
34
|
+
"./icons/*": "./data/icons/*.json",
|
|
28
35
|
"./package.json": "./package.json",
|
|
29
36
|
"./licence": "./licence.js",
|
|
30
37
|
"./mcp": "./mcp/server.js"
|
|
@@ -66,17 +73,47 @@
|
|
|
66
73
|
"keywords": [
|
|
67
74
|
"icons",
|
|
68
75
|
"animated icons",
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"react",
|
|
72
|
-
"vue",
|
|
76
|
+
"icon library",
|
|
77
|
+
"svg icons",
|
|
78
|
+
"react icons",
|
|
79
|
+
"vue icons",
|
|
80
|
+
"svelte icons",
|
|
81
|
+
"nextjs",
|
|
82
|
+
"nuxt",
|
|
83
|
+
"astro",
|
|
84
|
+
"shadcn",
|
|
85
|
+
"shadcn icons",
|
|
86
|
+
"tailwind",
|
|
73
87
|
"web component",
|
|
74
|
-
"
|
|
88
|
+
"custom element",
|
|
89
|
+
"lottie",
|
|
90
|
+
"lottie icons",
|
|
91
|
+
"react native",
|
|
92
|
+
"swiftui",
|
|
93
|
+
"jetpack compose",
|
|
94
|
+
"flutter",
|
|
95
|
+
"micro-interactions",
|
|
96
|
+
"ui sound",
|
|
97
|
+
"sound effects",
|
|
98
|
+
"icon animation",
|
|
99
|
+
"motion",
|
|
100
|
+
"mcp",
|
|
101
|
+
"mcp server",
|
|
102
|
+
"cli",
|
|
103
|
+
"icon maker"
|
|
75
104
|
],
|
|
76
105
|
"magicGlyphs": {
|
|
77
|
-
"api": "https://
|
|
106
|
+
"api": "https://api.magicglyphs.com",
|
|
78
107
|
"buy": "https://magicscraper.gumroad.com/l/vjaxbp"
|
|
79
108
|
},
|
|
80
|
-
"homepage": "https://
|
|
81
|
-
"types": "./web/runtime.d.ts"
|
|
109
|
+
"homepage": "https://magicglyphs.com",
|
|
110
|
+
"types": "./web/runtime.d.ts",
|
|
111
|
+
"repository": {
|
|
112
|
+
"type": "git",
|
|
113
|
+
"url": "git+https://github.com/lewisjohnvillamor/magic-glyphs.git"
|
|
114
|
+
},
|
|
115
|
+
"bugs": {
|
|
116
|
+
"url": "https://github.com/lewisjohnvillamor/magic-glyphs/issues",
|
|
117
|
+
"email": "support@magicglyphs.com"
|
|
118
|
+
}
|
|
82
119
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{%- comment -%}
|
|
2
2
|
Magic Glyphs snippet. Upload magic-glyphs.all.js to the theme's assets folder, save this file as snippets/magic-glyph.liquid, then:
|
|
3
3
|
{% render 'magic-glyph', name: 'cart', variant: 'duotone', size: 28, trigger: 'hover', label: 'Cart' %}
|
|
4
|
-
|
|
4
|
+
Snippets cannot share variables in Shopify, so the script loads itself once per page however many icons are rendered.
|
|
5
|
+
Sound: add sound: true. A mute switch for shoppers: {% render 'magic-glyph', toggle: true %}
|
|
5
6
|
{%- endcomment -%}
|
|
6
|
-
{
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
{%-
|
|
10
|
-
<magic-glyph name="{{ name | escape }}" variant="{{ variant | default: 'outline' | escape }}" size="{{ size | default: 24 | escape }}" trigger="{{ trigger | default: 'hover' | escape }}"{% if label %} label="{{ label | escape }}"{% endif %}></magic-glyph>
|
|
7
|
+
<script>(function(){if(window.__magicGlyphsLoading)return;window.__magicGlyphsLoading=1;var s=document.createElement('script');s.src={{ 'magic-glyphs.all.js' | asset_url | json }};s.defer=true;document.head.appendChild(s)})()</script>
|
|
8
|
+
{%- if toggle -%}
|
|
9
|
+
<magic-glyphs-sound-toggle default="{% if default_on == false %}off{% else %}on{% endif %}"></magic-glyphs-sound-toggle>
|
|
10
|
+
{%- else -%}
|
|
11
|
+
<magic-glyph name="{{ name | escape }}" variant="{{ variant | default: 'outline' | escape }}" size="{{ size | default: 24 | escape }}" trigger="{{ trigger | default: 'hover' | escape }}"{% if label %} label="{{ label | escape }}"{% endif %}{% if sound %} sound{% endif %}></magic-glyph>
|
|
12
|
+
{%- endif -%}
|
package/tools/apng.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// A small animated PNG (APNG) encoder: full alpha, exact timing, no dependencies.
|
|
2
|
+
// encode({width, height, frames:[RGBA], delays:[seconds], loop}, deflate) -> Promise<Uint8Array>
|
|
3
|
+
// `deflate(bytes)` must return zlib-format data (Node: zlib.deflateSync; browsers: CompressionStream('deflate')).
|
|
4
|
+
// Frames that repeat the previous one are merged into a longer delay.
|
|
5
|
+
(function(root,factory){if(typeof module==='object'&&module.exports)module.exports=factory();else root.MGApng=factory()})(this,function(){
|
|
6
|
+
const CRC=new Uint32Array(256);for(let n=0;n<256;n++){let c=n;for(let k=0;k<8;k++)c=c&1?0xEDB88320^(c>>>1):c>>>1;CRC[n]=c>>>0}
|
|
7
|
+
const crc=(b)=>{let c=0xFFFFFFFF;for(let i=0;i<b.length;i++)c=CRC[(c^b[i])&255]^(c>>>8);return(c^0xFFFFFFFF)>>>0};
|
|
8
|
+
function chunk(type,data){const out=new Uint8Array(12+data.length),v=new DataView(out.buffer);v.setUint32(0,data.length);
|
|
9
|
+
for(let i=0;i<4;i++)out[4+i]=type.charCodeAt(i);out.set(data,8);v.setUint32(8+data.length,crc(out.subarray(4,8+data.length)));return out}
|
|
10
|
+
const u32=(v,a,o)=>new DataView(a.buffer).setUint32(o,v),u16=(v,a,o)=>new DataView(a.buffer).setUint16(o,v);
|
|
11
|
+
function same(a,b){if(a.length!==b.length)return false;for(let i=0;i<a.length;i++)if(a[i]!==b[i])return false;return true}
|
|
12
|
+
// PNG rows with the "sub" filter (good for flat icon art)
|
|
13
|
+
function rows(f,W,H){const out=new Uint8Array(H*(W*4+1));for(let y=0;y<H;y++){const o=y*(W*4+1),s=y*W*4;out[o]=1;
|
|
14
|
+
for(let x=0;x<W*4;x++)out[o+1+x]=(f[s+x]-(x>=4?f[s+x-4]:0))&255}return out}
|
|
15
|
+
async function encode(o,deflate){
|
|
16
|
+
const W=o.width,H=o.height,fr=[],dl=[];
|
|
17
|
+
o.frames.forEach((f,i)=>{if(fr.length&&same(f,fr[fr.length-1]))dl[dl.length-1]+=o.delays[i];else{fr.push(f);dl.push(o.delays[i])}});
|
|
18
|
+
const parts=[new Uint8Array([137,80,78,71,13,10,26,10])];
|
|
19
|
+
const ih=new Uint8Array(13);u32(W,ih,0);u32(H,ih,4);ih[8]=8;ih[9]=6;parts.push(chunk('IHDR',ih));
|
|
20
|
+
const ac=new Uint8Array(8);u32(fr.length,ac,0);u32(o.loop===undefined?0:o.loop,ac,4);parts.push(chunk('acTL',ac));
|
|
21
|
+
let seq=0;
|
|
22
|
+
for(let i=0;i<fr.length;i++){
|
|
23
|
+
// after the first frame only the rectangle that changed is stored; it replaces those pixels (blend "source")
|
|
24
|
+
let x0=0,y0=0,x1=W,y1=H;
|
|
25
|
+
if(i>0){const a=fr[i],b=fr[i-1];x0=W;y0=H;x1=0;y1=0;for(let y=0;y<H;y++)for(let x=0;x<W;x++){const j=(y*W+x)*4;if(a[j]!==b[j]||a[j+1]!==b[j+1]||a[j+2]!==b[j+2]||a[j+3]!==b[j+3]){if(x<x0)x0=x;if(x>=x1)x1=x+1;if(y<y0)y0=y;if(y>=y1)y1=y+1}}}
|
|
26
|
+
const cw=x1-x0,chh=y1-y0,sub=new Uint8Array(cw*chh*4);for(let y=0;y<chh;y++)sub.set(fr[i].subarray(((y0+y)*W+x0)*4,((y0+y)*W+x1)*4),y*cw*4);
|
|
27
|
+
const fc=new Uint8Array(26);u32(seq++,fc,0);u32(cw,fc,4);u32(chh,fc,8);u32(x0,fc,12);u32(y0,fc,16);
|
|
28
|
+
// delay as a fraction of a second in milliseconds
|
|
29
|
+
u16(Math.max(1,Math.round(dl[i]*1000)),fc,20);u16(1000,fc,22);fc[24]=0;fc[25]=0;parts.push(chunk('fcTL',fc));
|
|
30
|
+
const z=await deflate(rows(sub,cw,chh));
|
|
31
|
+
if(i===0)parts.push(chunk('IDAT',z));else{const d=new Uint8Array(4+z.length);u32(seq++,d,0);d.set(z,4);parts.push(chunk('fdAT',d))}}
|
|
32
|
+
parts.push(chunk('IEND',new Uint8Array(0)));
|
|
33
|
+
let n=0;for(const p of parts)n+=p.length;const out=new Uint8Array(n);let k=0;for(const p of parts){out.set(p,k);k+=p.length}return out;
|
|
34
|
+
}
|
|
35
|
+
return{encode};
|
|
36
|
+
});
|
package/tools/baked.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// The motion of an icon pre-computed for platforms without a spring engine or a Lottie player:
|
|
2
|
+
// per layer, a 2D matrix [a,b,c,d,e,f] per frame (x' = a·x + c·y + e, y' = b·x + d·y + f), sampled at `fps`,
|
|
3
|
+
// plus what each path paints. Players step through the frames and interpolate between them.
|
|
4
|
+
const R=require('./raster.js');
|
|
5
|
+
const r3=v=>Math.round(v*1000)/1000;
|
|
6
|
+
function matrix(v,oR,oS){
|
|
7
|
+
if(!v)return[1,0,0,1,0,0];
|
|
8
|
+
const kx=1+v.s+v.sx,ky=1+v.s+v.sy,t=v.r*Math.PI/180,c=Math.cos(t),s=Math.sin(t);
|
|
9
|
+
// S about oS, then R about oR, then translate
|
|
10
|
+
const S=[kx,0,0,ky,oS[0]*(1-kx),oS[1]*(1-ky)],Rm=[c,s,-s,c,oR[0]-c*oR[0]+s*oR[1],oR[1]-s*oR[0]-c*oR[1]];
|
|
11
|
+
const mul=(A,B)=>[A[0]*B[0]+A[2]*B[1],A[1]*B[0]+A[3]*B[1],A[0]*B[2]+A[2]*B[3],A[1]*B[2]+A[3]*B[3],A[0]*B[4]+A[2]*B[5]+A[4],A[1]*B[4]+A[3]*B[5]+A[5]];
|
|
12
|
+
const M=mul(Rm,S);M[4]+=v.tx;M[5]+=v.ty;return M.map(r3);
|
|
13
|
+
}
|
|
14
|
+
// paint per layer kind and style, as {fill, stroke, fillOpacity} with colour roles: 'fg' | 'bg' | 'sec' | null
|
|
15
|
+
function paint(style,k){
|
|
16
|
+
if(k==='f')return{fill:'fg'};if(k==='d')return{fill:style==='solid'?'bg':'fg'};
|
|
17
|
+
if(k==='o')return{stroke:'fg'};if(k==='i')return{stroke:style==='solid'?'bg':'fg'};
|
|
18
|
+
if(style==='solid')return{fill:'fg',stroke:'fg'};if(style==='duotone')return{fill:'sec',fillOpacity:.18,stroke:'fg'};
|
|
19
|
+
return{stroke:'fg'};
|
|
20
|
+
}
|
|
21
|
+
function bake(MG,ic,o){
|
|
22
|
+
o=o||{};const fps=o.fps||30,style=o.style||'outline',org=MG.origins(ic),samples=R.sample(MG,ic,fps);
|
|
23
|
+
const layers=ic.L.map(l=>{
|
|
24
|
+
const oR=org[l.i+'|r']||org[l.i+'|s']||[12,12],oS=org[l.i+'|s']||org[l.i+'|r']||[12,12];
|
|
25
|
+
const m=samples.map(f=>matrix(f[l.i],oR,oS)),still=m.every(x=>x.join()===m[0].join())&&m[0].join()==='1,0,0,1,0,0';
|
|
26
|
+
return{id:l.i,paint:paint(style,l.k),paths:l.p.map(p=>({d:p.d,r:p.r||null,fill:p.f||null,polys:o.polys?R.flatten(p.d,p.r,.05).map(q=>({pts:q.pts.map(r3),closed:q.closed})):undefined})),m:still?null:m};
|
|
27
|
+
});
|
|
28
|
+
return{name:ic.n,fps,frames:samples.length,layers,sound:ic.s||null};
|
|
29
|
+
}
|
|
30
|
+
module.exports={bake,matrix,paint};
|
package/tools/gif.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// A small animated GIF encoder (no dependencies; runs in Node and in browsers).
|
|
2
|
+
// encode({width, height, frames:[RGBA], delays:[seconds], loop:0 forever | n, background:[r,g,b] 0..255 or null for transparent})
|
|
3
|
+
// GIF has one bit of transparency, so transparent exports blend soft edges onto `matte` (default white) and keep
|
|
4
|
+
// pixels at least half covered. With a background, unchanged areas between frames are skipped to keep files small.
|
|
5
|
+
(function(root,factory){if(typeof module==='object'&&module.exports)module.exports=factory();else root.MGGif=factory()})(this,function(){
|
|
6
|
+
// median cut on a 5-bit-per-channel histogram, at most `max` colours
|
|
7
|
+
function palette(hist,max){
|
|
8
|
+
let boxes=[{c:[]}];for(let i=0;i<hist.length;i++)if(hist[i])boxes[0].c.push(i);
|
|
9
|
+
if(!boxes[0].c.length)return[[0,0,0]];
|
|
10
|
+
const ch=(i,k)=>k===0?i>>10:k===1?(i>>5)&31:i&31;
|
|
11
|
+
const range=b=>{let best=-1,bk=0;for(let k=0;k<3;k++){let lo=99,hi=-1;for(const i of b.c){const v=ch(i,k);if(v<lo)lo=v;if(v>hi)hi=v}if(hi-lo>best){best=hi-lo;bk=k}}return[best,bk]};
|
|
12
|
+
while(boxes.length<max){let bi=-1,bv=0;boxes.forEach((b,i)=>{if(b.c.length<2)return;const r=range(b),w=r[0]*Math.log2(1+b.c.reduce((s,i)=>s+hist[i],0));if(r[0]>0&&w>bv){bv=w;bi=i}});
|
|
13
|
+
if(bi<0)break;const b=boxes[bi],k=range(b)[1];b.c.sort((x,y)=>ch(x,k)-ch(y,k));
|
|
14
|
+
const tot=b.c.reduce((s,i)=>s+hist[i],0);let acc=0,cut=1;for(let j=0;j<b.c.length-1;j++){acc+=hist[b.c[j]];if(acc>=tot/2){cut=j+1;break}}
|
|
15
|
+
boxes.splice(bi,1,{c:b.c.slice(0,cut)},{c:b.c.slice(cut)})}
|
|
16
|
+
return boxes.map(b=>{let n=0,r=0,g=0,bl=0;for(const i of b.c){const w=hist[i];n+=w;r+=(i>>10)*w;g+=((i>>5)&31)*w;bl+=(i&31)*w}return[r/n*255/31+.5|0,g/n*255/31+.5|0,bl/n*255/31+.5|0]});
|
|
17
|
+
}
|
|
18
|
+
function lzw(idx,minBits){
|
|
19
|
+
const out=[];let cur=0,bits=0;const put=(code,size)=>{cur|=code<<bits;bits+=size;while(bits>=8){out.push(cur&255);cur>>=8;bits-=8}};
|
|
20
|
+
const clear=1<<minBits,eoi=clear+1;let size=minBits+1,next=eoi+1,dict=new Map();
|
|
21
|
+
put(clear,size);let w=idx[0];
|
|
22
|
+
for(let i=1;i<idx.length;i++){const k=idx[i],key=w*4096+k,hit=dict.get(key);
|
|
23
|
+
if(hit!==undefined){w=hit;continue}
|
|
24
|
+
put(w,size);
|
|
25
|
+
if(next<4096){dict.set(key,next++);if(next>(1<<size)&&size<12)size++}
|
|
26
|
+
else{put(clear,size);dict=new Map();size=minBits+1;next=eoi+1}
|
|
27
|
+
w=k}
|
|
28
|
+
put(w,size);put(eoi,size);if(bits>0)out.push(cur&255);
|
|
29
|
+
const blocks=[];for(let i=0;i<out.length;i+=255){const n=Math.min(255,out.length-i);blocks.push(n);for(let j=0;j<n;j++)blocks.push(out[i+j])}blocks.push(0);return blocks;
|
|
30
|
+
}
|
|
31
|
+
function encode(o){
|
|
32
|
+
const W=o.width,H=o.height,bg=o.background||null,matte=o.matte||[255,255,255],N=W*H;
|
|
33
|
+
// flatten every frame to opaque colours (or "transparent") first
|
|
34
|
+
const flat=o.frames.map(f=>{const c=new Int32Array(N);for(let i=0,j=0;i<N;i++,j+=4){const a=f[j+3]/255;
|
|
35
|
+
if(bg){c[i]=((f[j]*a+bg[0]*(1-a))>>3<<10)|((f[j+1]*a+bg[1]*(1-a))>>3<<5)|((f[j+2]*a+bg[2]*(1-a))>>3)}
|
|
36
|
+
else if(a<.5)c[i]=-1;else c[i]=((f[j]*a+matte[0]*(1-a))>>3<<10)|((f[j+1]*a+matte[1]*(1-a))>>3<<5)|((f[j+2]*a+matte[2]*(1-a))>>3)}return c});
|
|
37
|
+
const hist=new Uint32Array(32768);for(const c of flat)for(let i=0;i<N;i++)if(c[i]>=0)hist[c[i]]++;
|
|
38
|
+
const pal=palette(hist,bg?256:255),T=bg?-1:pal.length,cache=new Int16Array(32768).fill(-1);
|
|
39
|
+
const near=k=>{if(cache[k]>=0)return cache[k];const r=(k>>10)*255/31,g=((k>>5)&31)*255/31,b=(k&31)*255/31;let bi=0,bd=1e9;
|
|
40
|
+
pal.forEach((p,i)=>{const d=(p[0]-r)**2*.3+(p[1]-g)**2*.59+(p[2]-b)**2*.11;if(d<bd){bd=d;bi=i}});return cache[k]=bi};
|
|
41
|
+
let n=pal.length+(bg?0:1),bitsz=1;while((1<<bitsz)<n)bitsz++;const PS=1<<bitsz;
|
|
42
|
+
const B=[];const s=t=>{for(let i=0;i<t.length;i++)B.push(t.charCodeAt(i))},u16=v=>B.push(v&255,v>>8);
|
|
43
|
+
s('GIF89a');u16(W);u16(H);B.push(0xF0|(bitsz-1),0,0);
|
|
44
|
+
for(let i=0;i<PS;i++){const p=pal[i]||[0,0,0];B.push(p[0],p[1],p[2])}
|
|
45
|
+
const loop=o.loop===undefined?0:o.loop;if(loop!==1){B.push(0x21,0xFF,11);s('NETSCAPE2.0');B.push(3,1);u16(loop===0?0:loop-1);B.push(0)}
|
|
46
|
+
let prev=null;
|
|
47
|
+
flat.forEach((c,fi)=>{
|
|
48
|
+
let x0=0,y0=0,x1=W,y1=H;
|
|
49
|
+
// with a solid background only the changed rectangle is written
|
|
50
|
+
if(bg&&prev){x0=W;y0=H;x1=0;y1=0;for(let y=0;y<H;y++)for(let x=0;x<W;x++){const i=y*W+x;if(c[i]!==prev[i]){if(x<x0)x0=x;if(x>=x1)x1=x+1;if(y<y0)y0=y;if(y>=y1)y1=y+1}}if(x1<=x0){x0=0;y0=0;x1=1;y1=1}}
|
|
51
|
+
const cs=Math.max(2,Math.round((o.delays[fi]||.02)*100));
|
|
52
|
+
B.push(0x21,0xF9,4,(bg?1:2)<<2|(bg?0:1));u16(cs);B.push(bg?0:T,0);
|
|
53
|
+
B.push(0x2C);u16(x0);u16(y0);u16(x1-x0);u16(y1-y0);B.push(0);
|
|
54
|
+
const idx=new Uint8Array((x1-x0)*(y1-y0));let k=0;for(let y=y0;y<y1;y++)for(let x=x0;x<x1;x++){const v=c[y*W+x];idx[k++]=v<0?T:near(v)}
|
|
55
|
+
const mb=Math.max(2,bitsz);B.push(mb);const data=lzw(idx,mb);for(let i=0;i<data.length;i++)B.push(data[i]);
|
|
56
|
+
prev=c});
|
|
57
|
+
B.push(0x3B);return new Uint8Array(B);
|
|
58
|
+
}
|
|
59
|
+
return{encode};
|
|
60
|
+
});
|
package/tools/motion.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Animated image and video exports for places that cannot run code: email, slides, Canva, Notion, chat emoji, social posts.
|
|
2
|
+
// gif works everywhere, one bit of transparency (soft edges are blended onto --matte or the background)
|
|
3
|
+
// apng full transparency and exact timing (Slack, Discord, browsers, Keynote)
|
|
4
|
+
// webm VP9 video with transparency and the sound baked in } need ffmpeg on this computer
|
|
5
|
+
// mp4 H.264 video with the sound baked in (solid background) } (or export them from the icon maker)
|
|
6
|
+
const R=require('./raster.js');
|
|
7
|
+
const DEF={gif:{size:256,fps:50},apng:{size:128,fps:60},webm:{size:512,fps:60},mp4:{size:512,fps:60}};
|
|
8
|
+
|
|
9
|
+
function frames(L,name,format,o){
|
|
10
|
+
const ic=L.byName.get(name),d=DEF[format];
|
|
11
|
+
if((o.style||'outline')==='glass'&&format==='gif'&&!(o.background&&o.background!=='transparent'))throw new Error('GIF cannot hold the glass style\'s see-through gradient on a transparent background. Add --background, or use --format apng or webm.');
|
|
12
|
+
const bg=o.background||(format==='mp4'?'#ffffff':'transparent');
|
|
13
|
+
return R.render(L.MG,ic,{size:o.size||d.size,style:o.style,color:o.color||'#000000',secondary:o.secondary,background:bg,fps:d.fps,hold:o.hold,lead:o.lead});
|
|
14
|
+
}
|
|
15
|
+
const rgb=c=>{const h=R.hex(c,null);return h&&h.map(v=>Math.round(v*255))};
|
|
16
|
+
|
|
17
|
+
async function gif(L,name,o){const r=frames(L,name,'gif',o);
|
|
18
|
+
return require('./gif.js').encode({width:r.width,height:r.height,frames:r.frames,delays:r.delays,loop:o.loop===undefined?0:+o.loop,background:o.background&&o.background!=='transparent'?rgb(o.background):null,matte:rgb(o.matte||'#ffffff')})}
|
|
19
|
+
async function apng(L,name,o){const r=frames(L,name,'apng',o),z=require('zlib');
|
|
20
|
+
return require('./apng.js').encode({width:r.width,height:r.height,frames:r.frames,delays:r.delays,loop:o.loop===undefined?0:+o.loop},b=>z.deflateSync(b,{level:9}))}
|
|
21
|
+
|
|
22
|
+
// the icon's sound, placed where the motion starts, as 16-bit stereo PCM covering the whole clip
|
|
23
|
+
async function soundtrack(L,name,seconds,lead){
|
|
24
|
+
const ic=L.byName.get(name);if(!ic||!ic.s)return null;
|
|
25
|
+
const A=require('./audio.js'),buf=await L.MG.sound.render(name,{},A.OfflineContext),sr=buf.sampleRate,n=Math.ceil(seconds*sr),off=Math.round(lead*sr);
|
|
26
|
+
const Lc=buf.getChannelData(0),Rc=buf.getChannelData(1),pcm=Buffer.alloc(n*4);
|
|
27
|
+
for(let i=0;i<Lc.length&&i+off<n;i++){pcm.writeInt16LE(Math.max(-1,Math.min(1,Lc[i]))*32767|0,(i+off)*4);pcm.writeInt16LE(Math.max(-1,Math.min(1,Rc[i]))*32767|0,(i+off)*4+2)}
|
|
28
|
+
return{pcm,sr};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports={DEF,frames,gif,apng,soundtrack,FORMATS:['gif','apng','webm','mp4']};
|
package/tools/native.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// One-file components for native apps. Each embeds the icon's Lottie animation, so there is no asset to copy;
|
|
2
|
+
// if a <name>.wav from `--sound` is bundled with the app, the component plays it with the animation.
|
|
3
|
+
function lottieJson(ic, o) { const { toLottie } = require('./lottie.js'); return JSON.stringify(toLottie(ic, Object.assign({}, o, { color: o.color || '#000000' }))); }
|
|
4
|
+
const pascal = (n) => n.split(/[^a-z0-9]+/i).filter(Boolean).map((w) => w[0].toUpperCase() + w.slice(1)).join('').replace(/^(\d)/, 'Glyph$1');
|
|
5
|
+
const camel = (n) => { const p = pascal(n); return p[0].toLowerCase() + p.slice(1); };
|
|
6
|
+
|
|
7
|
+
function reactNative(ic, o) {
|
|
8
|
+
const P = pascal(ic.n), j = lottieJson(ic, o);
|
|
9
|
+
return { file: P + 'Glyph.tsx', text: `// Animated "${ic.n}" icon from Magic Glyphs for React Native.
|
|
10
|
+
// Needs: npm install lottie-react-native (sound, optional: npx expo install expo-audio, and bundle ${ic.n}.wav)
|
|
11
|
+
import React, { useRef } from 'react';
|
|
12
|
+
import { Pressable, type StyleProp, type ViewStyle } from 'react-native';
|
|
13
|
+
import LottieView from 'lottie-react-native';
|
|
14
|
+
|
|
15
|
+
const animation = ${j};
|
|
16
|
+
|
|
17
|
+
export type ${P}GlyphProps = {
|
|
18
|
+
size?: number;
|
|
19
|
+
/** Play when pressed (default true). */
|
|
20
|
+
playOnPress?: boolean;
|
|
21
|
+
/** Called when it plays, for example to play its sound with expo-audio. */
|
|
22
|
+
onPlay?: () => void;
|
|
23
|
+
accessibilityLabel?: string;
|
|
24
|
+
style?: StyleProp<ViewStyle>;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default function ${P}Glyph({ size = 24, playOnPress = true, onPlay, accessibilityLabel, style }: ${P}GlyphProps) {
|
|
28
|
+
const ref = useRef<LottieView>(null);
|
|
29
|
+
const play = () => { ref.current?.reset(); ref.current?.play(); onPlay?.(); };
|
|
30
|
+
return (
|
|
31
|
+
<Pressable onPress={playOnPress ? play : undefined} accessibilityRole={accessibilityLabel ? 'button' : undefined}
|
|
32
|
+
accessibilityLabel={accessibilityLabel} accessibilityElementsHidden={!accessibilityLabel} style={style}>
|
|
33
|
+
<LottieView ref={ref} source={animation} autoPlay={false} loop={false} style={{ width: size, height: size }} />
|
|
34
|
+
</Pressable>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
` };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function swiftui(ic, o) {
|
|
41
|
+
const P = pascal(ic.n), j = lottieJson(ic, o);
|
|
42
|
+
return { file: P + 'Glyph.swift', text: `// Animated "${ic.n}" icon from Magic Glyphs for SwiftUI (iOS 15+, macOS 12+).
|
|
43
|
+
// Needs the Lottie package: https://github.com/airbnb/lottie-ios (4.4 or later).
|
|
44
|
+
// Sound, optional: add ${ic.n}.wav to the app bundle and it plays with the animation.
|
|
45
|
+
import SwiftUI
|
|
46
|
+
import Lottie
|
|
47
|
+
import AVFoundation
|
|
48
|
+
|
|
49
|
+
public struct ${P}Glyph: View {
|
|
50
|
+
private static let animation = LottieAnimation.from(data: Data(#"${j}"#.utf8))
|
|
51
|
+
private static var player: AVAudioPlayer? = Bundle.main.url(forResource: "${ic.n}", withExtension: "wav").flatMap { try? AVAudioPlayer(contentsOf: $0) }
|
|
52
|
+
@State private var playback: LottiePlaybackMode = .paused(at: .progress(0))
|
|
53
|
+
var size: CGFloat
|
|
54
|
+
var sound: Bool
|
|
55
|
+
var label: String?
|
|
56
|
+
|
|
57
|
+
public init(size: CGFloat = 24, sound: Bool = true, label: String? = nil) { self.size = size; self.sound = sound; self.label = label }
|
|
58
|
+
|
|
59
|
+
public var body: some View {
|
|
60
|
+
LottieView(animation: Self.animation)
|
|
61
|
+
.playbackMode(playback)
|
|
62
|
+
.animationDidFinish { _ in playback = .paused(at: .progress(0)) } // ready to play again on the next tap
|
|
63
|
+
.frame(width: size, height: size)
|
|
64
|
+
.contentShape(Rectangle())
|
|
65
|
+
.onTapGesture {
|
|
66
|
+
playback = .playing(.fromProgress(0, toProgress: 1, loopMode: .playOnce))
|
|
67
|
+
if sound { Self.player?.currentTime = 0; Self.player?.play() }
|
|
68
|
+
}
|
|
69
|
+
.accessibilityLabel(label ?? "")
|
|
70
|
+
.accessibilityHidden(label == nil)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
` };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function compose(ic, o) {
|
|
77
|
+
const P = pascal(ic.n), j = lottieJson(ic, o).replace(/\$/g, "${'$'}");
|
|
78
|
+
return { file: P + 'Glyph.kt', text: `// Animated "${ic.n}" icon from Magic Glyphs for Jetpack Compose.
|
|
79
|
+
// Needs: implementation("com.airbnb.android:lottie-compose:6.6.0")
|
|
80
|
+
// Sound, optional: put ${ic.n}.wav in res/raw as ${ic.n.replace(/-/g, '_')}.wav and pass sound = true.
|
|
81
|
+
package magicglyphs
|
|
82
|
+
|
|
83
|
+
import android.media.MediaPlayer
|
|
84
|
+
import androidx.compose.foundation.clickable
|
|
85
|
+
import androidx.compose.foundation.layout.size
|
|
86
|
+
import androidx.compose.runtime.*
|
|
87
|
+
import androidx.compose.ui.Modifier
|
|
88
|
+
import androidx.compose.ui.platform.LocalContext
|
|
89
|
+
import androidx.compose.ui.semantics.contentDescription
|
|
90
|
+
import androidx.compose.ui.semantics.semantics
|
|
91
|
+
import androidx.compose.ui.unit.Dp
|
|
92
|
+
import androidx.compose.ui.unit.dp
|
|
93
|
+
import com.airbnb.lottie.compose.*
|
|
94
|
+
|
|
95
|
+
private const val ${camel(ic.n)}Json = """${j}"""
|
|
96
|
+
|
|
97
|
+
@Composable
|
|
98
|
+
fun ${P}Glyph(size: Dp = 24.dp, sound: Boolean = false, label: String? = null, modifier: Modifier = Modifier) {
|
|
99
|
+
val composition by rememberLottieComposition(LottieCompositionSpec.JsonString(${camel(ic.n)}Json))
|
|
100
|
+
var plays by remember { mutableIntStateOf(0) }
|
|
101
|
+
val progress by animateLottieCompositionAsState(composition, isPlaying = plays > 0, restartOnPlay = true, iterations = 1)
|
|
102
|
+
val context = LocalContext.current
|
|
103
|
+
val soundId = remember { context.resources.getIdentifier("${ic.n.replace(/-/g, '_')}", "raw", context.packageName) }
|
|
104
|
+
LottieAnimation(
|
|
105
|
+
composition = composition,
|
|
106
|
+
progress = { if (plays == 0) 0f else progress },
|
|
107
|
+
modifier = modifier.size(size)
|
|
108
|
+
.semantics { if (label != null) contentDescription = label }
|
|
109
|
+
.clickable {
|
|
110
|
+
plays++
|
|
111
|
+
if (sound && soundId != 0) MediaPlayer.create(context, soundId)?.apply { setOnCompletionListener { it.release() }; start() }
|
|
112
|
+
}
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
` };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function flutter(ic, o) {
|
|
119
|
+
const P = pascal(ic.n), j = lottieJson(ic, o);
|
|
120
|
+
return { file: ic.n.replace(/-/g, '_') + '_glyph.dart', text: `// Animated "${ic.n}" icon from Magic Glyphs for Flutter.
|
|
121
|
+
// Needs: flutter pub add lottie (sound, optional: flutter pub add audioplayers, and add assets/${ic.n}.wav to pubspec.yaml)
|
|
122
|
+
import 'dart:convert';
|
|
123
|
+
import 'package:flutter/material.dart';
|
|
124
|
+
import 'package:lottie/lottie.dart';
|
|
125
|
+
import 'package:audioplayers/audioplayers.dart';
|
|
126
|
+
|
|
127
|
+
const String _${camel(ic.n)}Json = r'''${j}''';
|
|
128
|
+
|
|
129
|
+
class ${P}Glyph extends StatefulWidget {
|
|
130
|
+
const ${P}Glyph({super.key, this.size = 24, this.sound = false, this.label});
|
|
131
|
+
final double size;
|
|
132
|
+
final bool sound;
|
|
133
|
+
final String? label;
|
|
134
|
+
@override
|
|
135
|
+
State<${P}Glyph> createState() => _${P}GlyphState();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
class _${P}GlyphState extends State<${P}Glyph> with SingleTickerProviderStateMixin {
|
|
139
|
+
late final AnimationController _controller = AnimationController(vsync: this);
|
|
140
|
+
final AudioPlayer _audio = AudioPlayer();
|
|
141
|
+
|
|
142
|
+
void play() {
|
|
143
|
+
_controller.forward(from: 0);
|
|
144
|
+
if (widget.sound) _audio.play(AssetSource('${ic.n}.wav'));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
@override
|
|
148
|
+
void dispose() { _controller.dispose(); _audio.dispose(); super.dispose(); }
|
|
149
|
+
|
|
150
|
+
@override
|
|
151
|
+
Widget build(BuildContext context) {
|
|
152
|
+
return Semantics(
|
|
153
|
+
label: widget.label,
|
|
154
|
+
excludeSemantics: widget.label == null,
|
|
155
|
+
button: widget.label != null,
|
|
156
|
+
child: GestureDetector(
|
|
157
|
+
onTap: play,
|
|
158
|
+
child: Lottie.memory(
|
|
159
|
+
utf8.encode(_${camel(ic.n)}Json),
|
|
160
|
+
controller: _controller,
|
|
161
|
+
width: widget.size,
|
|
162
|
+
height: widget.size,
|
|
163
|
+
onLoaded: (c) => _controller.duration = c.duration,
|
|
164
|
+
),
|
|
165
|
+
),
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
` };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function winui(ic, o) {
|
|
173
|
+
const P = pascal(ic.n), j = lottieJson(ic, o).replace(/"/g, '""');
|
|
174
|
+
return { file: P + 'Glyph.cs', text: `// Animated "${ic.n}" icon from Magic Glyphs for WinUI 3 (Windows App SDK).
|
|
175
|
+
// Needs: dotnet add package CommunityToolkit.WinUI.Lottie
|
|
176
|
+
// Sound, optional: add ${ic.n}.wav to the project as Content and set Sound = true.
|
|
177
|
+
using System;
|
|
178
|
+
using CommunityToolkit.WinUI.Lottie;
|
|
179
|
+
using Microsoft.UI.Xaml.Controls;
|
|
180
|
+
using Windows.Media.Core;
|
|
181
|
+
using Windows.Media.Playback;
|
|
182
|
+
|
|
183
|
+
namespace MagicGlyphs;
|
|
184
|
+
|
|
185
|
+
public sealed class ${P}Glyph : UserControl
|
|
186
|
+
{
|
|
187
|
+
private const string Json = @"${j}";
|
|
188
|
+
private readonly AnimatedVisualPlayer _player = new() { AutoPlay = false };
|
|
189
|
+
private readonly MediaPlayer _audio = new();
|
|
190
|
+
public bool Sound { get; set; }
|
|
191
|
+
|
|
192
|
+
public ${P}Glyph(double size = 24)
|
|
193
|
+
{
|
|
194
|
+
Width = Height = size;
|
|
195
|
+
_player.Source = LottieVisualSource.CreateFromString(Json);
|
|
196
|
+
Content = _player;
|
|
197
|
+
Tapped += (_, _) => Play();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
public async void Play()
|
|
201
|
+
{
|
|
202
|
+
if (Sound) { _audio.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///${ic.n}.wav")); _audio.Play(); }
|
|
203
|
+
await _player.PlayAsync(0, 1, false);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
` };
|
|
207
|
+
}
|
|
208
|
+
module.exports = { 'react-native': reactNative, swiftui, compose, flutter, winui, lottieJson };
|