asciify-engine 1.0.30 → 1.0.32

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/dist/index.cjs CHANGED
@@ -2425,454 +2425,6 @@ async function recordAndDownload(canvas, durationMs, options = {}) {
2425
2425
  a.click();
2426
2426
  }
2427
2427
 
2428
- // src/webgl-engine.ts
2429
- var VERT_SRC = (
2430
- /* glsl */
2431
- `#version 300 es
2432
- precision highp float;
2433
- precision highp int;
2434
-
2435
- // Unit quad corners: (-0.5,-0.5) to (0.5,0.5) - TRIANGLE_STRIP, 4 verts
2436
- in vec2 a_corner;
2437
-
2438
- // Layout
2439
- uniform vec2 u_canvasSize;
2440
- uniform vec2 u_gridSize;
2441
- uniform vec2 u_cellSize;
2442
-
2443
- // Animation
2444
- uniform float u_time;
2445
- uniform float u_animSpeed;
2446
- uniform int u_animStyle;
2447
-
2448
- // Color
2449
- uniform int u_colorMode;
2450
- uniform vec3 u_accentColor;
2451
- uniform int u_invert;
2452
-
2453
- // Hover
2454
- uniform float u_hoverStrength;
2455
- uniform float u_hoverIntensity;
2456
- uniform vec2 u_hoverPos;
2457
- uniform float u_hoverRadius;
2458
- uniform int u_hoverEffect;
2459
- uniform vec3 u_hoverColor;
2460
-
2461
- // Render mode: 0=rect 1=dots
2462
- uniform int u_renderMode;
2463
- uniform float u_dotSizeRatio;
2464
-
2465
- // Cell data: RGBA8 texture (cols x rows)
2466
- // rgb = cell colour, a = source alpha
2467
- uniform sampler2D u_cellTex;
2468
-
2469
- out vec4 v_color;
2470
- out float v_alpha;
2471
- out vec2 v_localUv;
2472
-
2473
- #define PI 3.141592653589793
2474
-
2475
- float ss(float t) { return t * t * (3.0 - 2.0 * t); }
2476
-
2477
- float getAnim(float nx, float ny, float cx, float cy, float t) {
2478
- if (u_animStyle == 0) return 1.0;
2479
- if (u_animStyle == 1) { // wave
2480
- return 0.3 + 0.7 * (sin(nx * PI * 4.0 + t * 3.0) * 0.3 + 0.5
2481
- + sin(ny * PI * 3.0 + t * 2.0) * 0.2);
2482
- }
2483
- if (u_animStyle == 2) { // pulse
2484
- float d = length(vec2(nx - 0.5, ny - 0.5)) * 1.41421;
2485
- return 0.2 + 0.8 * (sin(d * PI * 6.0 - t * 4.0) * 0.5 + 0.5);
2486
- }
2487
- if (u_animStyle == 3) { // rain
2488
- float drop = sin(ny * PI * 8.0 - t * 5.0 + cx * 0.3) * 0.5 + 0.5;
2489
- return 0.1 + 0.9 * drop * (sin(nx * PI * 2.0 + t) * 0.3 + 0.7);
2490
- }
2491
- if (u_animStyle == 4) { // breathe
2492
- return clamp(sin(t * 2.0) * 0.3 + 0.7 + sin((cx + cy) * 0.1 + t) * 0.1, 0.1, 1.0);
2493
- }
2494
- if (u_animStyle == 5) { // sparkle
2495
- float sp = fract(sin(cx * 127.1 + cy * 311.7 + floor(t * 8.0) * 43758.5453) * 43758.5453);
2496
- return sp > 0.7 ? 1.0 : 0.15 + sp * 0.4;
2497
- }
2498
- if (u_animStyle == 6) { // glitch
2499
- float band = floor(cy / max(1.0, u_gridSize.y * 0.05));
2500
- float gv = fract(sin(band * 43.23 + floor(t * 6.0) * 17.89) * 43758.5453);
2501
- if (gv > 0.85) { float fl = sin(t * 30.0 + band) * 0.5 + 0.5; return fl < 0.3 ? 0.0 : fl; }
2502
- return 1.0;
2503
- }
2504
- if (u_animStyle == 7) { // spiral
2505
- float angle = atan(ny - 0.5, nx - 0.5);
2506
- float dist = length(vec2(nx - 0.5, ny - 0.5)) * 1.41421;
2507
- return 0.15 + 0.85 * (sin(angle * 3.0 + dist * PI * 8.0 - t * 3.0) * 0.5 + 0.5);
2508
- }
2509
- if (u_animStyle == 8) { // typewriter
2510
- float reveal = fract(t * 0.5) * u_gridSize.x * u_gridSize.y * 1.3;
2511
- float dist = cy * u_gridSize.x + cx - reveal;
2512
- if (dist > 0.0) return 0.0;
2513
- return clamp(1.0 + dist / (u_gridSize.x * u_gridSize.y * 0.15), 0.0, 1.0);
2514
- }
2515
- if (u_animStyle == 9) { // scatter
2516
- float d = length(vec2(nx - 0.5, ny - 0.5)) * 1.41421;
2517
- float phase = sin(t * 1.5) * 0.5 + 0.5;
2518
- if (d > phase) return max(0.0, 1.0 - (d - phase) * 3.0);
2519
- return 0.7 + 0.3 * sin(d * 10.0 - t * 2.0);
2520
- }
2521
- return 1.0;
2522
- }
2523
-
2524
- vec3 applyColorMode(vec3 rgb) {
2525
- float lum = dot(rgb, vec3(0.299, 0.587, 0.114));
2526
- if (u_colorMode == 0) return vec3(lum);
2527
- if (u_colorMode == 2) return vec3(0.0, lum, 0.0);
2528
- if (u_colorMode == 3) return lum * u_accentColor;
2529
- return rgb;
2530
- }
2531
-
2532
- void main() {
2533
- int cols = int(u_gridSize.x);
2534
- int rows = int(u_gridSize.y);
2535
- int cx = gl_InstanceID % cols;
2536
- int cy = gl_InstanceID / cols;
2537
-
2538
- float fnx = (float(cx) + 0.5) / float(cols);
2539
- float fny = (float(cy) + 0.5) / float(rows);
2540
-
2541
- // Sample cell data
2542
- vec4 cell = texture(u_cellTex, vec2(fnx, fny));
2543
- // cell.rgb = colour, cell.a = source alpha (0=transparent, 1=opaque)
2544
-
2545
- // Invisible cells (fully transparent source pixel)
2546
- if (cell.a < 0.004) {
2547
- gl_Position = vec4(2.0, 2.0, 0.0, 1.0); v_alpha = 0.0; return;
2548
- }
2549
-
2550
- // Char weight: 0=dark/space .. 1=dense/bright (luminance, matches luminanceToChar ordering)
2551
- float charW = dot(cell.rgb, vec3(0.299, 0.587, 0.114));
2552
-
2553
- float cellW = u_cellSize.x;
2554
- float cellH = u_cellSize.y;
2555
-
2556
- // Animation
2557
- float t = u_time * u_animSpeed;
2558
- float am = getAnim(fnx, fny, float(cx), float(cy), t);
2559
- if (am < 0.04) {
2560
- gl_Position = vec4(2.0, 2.0, 0.0, 1.0); v_alpha = 0.0; return;
2561
- }
2562
-
2563
- // Hover
2564
- float hoverScale = 1.0;
2565
- vec2 hoverOff = vec2(0.0);
2566
- float hoverGlow = 0.0;
2567
- float hoverBlend = 0.0;
2568
-
2569
- if (u_hoverStrength > 0.0 && u_hoverIntensity > 0.005) {
2570
- vec2 d = vec2(fnx - u_hoverPos.x, fny - u_hoverPos.y);
2571
- float r = 0.08 + u_hoverRadius * 0.35 + u_hoverStrength * 0.04;
2572
- float dist = length(d);
2573
- if (dist < r) {
2574
- float tv = 1.0 - dist / r;
2575
- float e = ss(tv) * u_hoverIntensity;
2576
- if (u_hoverEffect == 0) { // spotlight
2577
- hoverScale = 1.0 + e * u_hoverStrength * 1.8;
2578
- float ang = atan(d.y, d.x);
2579
- hoverOff = vec2(cos(ang), sin(ang)) * e * e * u_hoverStrength * 0.6 * vec2(cellW, cellH);
2580
- hoverGlow = e * u_hoverStrength * 0.4;
2581
- hoverBlend = e * e * u_hoverStrength * 0.25;
2582
- } else if (u_hoverEffect == 1) { // magnify
2583
- hoverScale = 1.0 + e * u_hoverStrength * 2.5;
2584
- hoverGlow = e * u_hoverStrength * 0.15;
2585
- } else if (u_hoverEffect == 2) { // repel
2586
- hoverScale = 1.0 + e * u_hoverStrength * 0.3;
2587
- float ang = atan(d.y, d.x);
2588
- hoverOff = vec2(cos(ang), sin(ang)) * e * e * u_hoverStrength * 1.2 * vec2(cellW, cellH);
2589
- } else if (u_hoverEffect == 3) { // glow
2590
- hoverGlow = e * u_hoverStrength * 0.8;
2591
- hoverBlend = e * u_hoverStrength * 0.4;
2592
- } else { // colorShift
2593
- hoverScale = 1.0 + e * u_hoverStrength * 0.4;
2594
- hoverGlow = e * u_hoverStrength * 0.2;
2595
- hoverBlend = e * u_hoverStrength * 0.7;
2596
- }
2597
- }
2598
- }
2599
-
2600
- // Final colour
2601
- vec3 rgb = (u_invert == 1) ? (1.0 - cell.rgb) : cell.rgb;
2602
- vec3 baseColor = applyColorMode(rgb);
2603
- v_color = vec4(mix(baseColor, u_hoverColor, hoverBlend), 1.0);
2604
-
2605
- // Quad size & alpha
2606
- vec2 quadSize;
2607
- float finalAlpha;
2608
-
2609
- if (u_renderMode == 1) { // dots: radius driven by char weight (bright=big dot)
2610
- float maxR = min(cellW, cellH) * 0.5 * u_dotSizeRatio;
2611
- float radius = maxR * charW * am * hoverScale;
2612
- if (radius < 0.3) { gl_Position = vec4(2.0, 2.0, 0.0, 1.0); v_alpha = 0.0; return; }
2613
- quadSize = vec2(radius * 2.0);
2614
- finalAlpha = min(1.0, cell.a * am * (1.0 + hoverGlow));
2615
- } else { // rect: size proportional to luminance \u2014 dark=tiny, bright=full (ASCII density look)
2616
- if (charW < 0.01) { gl_Position = vec4(2.0, 2.0, 0.0, 1.0); v_alpha = 0.0; return; }
2617
- quadSize = vec2(cellW * charW * hoverScale, cellH * charW * hoverScale);
2618
- finalAlpha = min(1.0, am * (1.0 + hoverGlow));
2619
- }
2620
-
2621
- v_alpha = finalAlpha;
2622
- v_localUv = a_corner;
2623
-
2624
- // NDC position (Y flipped: WebGL bottom-up, canvas top-down)
2625
- float px = float(cx) * cellW + cellW * 0.5 + hoverOff.x;
2626
- float py = float(cy) * cellH + cellH * 0.5 + hoverOff.y;
2627
- vec2 pos = vec2(px, py) + a_corner * quadSize;
2628
- vec2 ndc = pos / u_canvasSize * 2.0 - 1.0;
2629
- ndc.y = -ndc.y;
2630
- gl_Position = vec4(ndc, 0.0, 1.0);
2631
- }
2632
- `
2633
- );
2634
- var FRAG_SRC = (
2635
- /* glsl */
2636
- `#version 300 es
2637
- precision mediump float;
2638
- precision highp int;
2639
-
2640
- in vec4 v_color;
2641
- in float v_alpha;
2642
- in vec2 v_localUv;
2643
-
2644
- uniform int u_renderMode;
2645
-
2646
- out vec4 outColor;
2647
-
2648
- void main() {
2649
- if (v_alpha < 0.01) discard;
2650
- if (u_renderMode == 1) {
2651
- // Dots: circle SDF clip
2652
- if (length(v_localUv) > 0.5) discard;
2653
- }
2654
- outColor = vec4(v_color.rgb, v_alpha);
2655
- }
2656
- `
2657
- );
2658
- function compileShader(gl, type, src) {
2659
- const sh = gl.createShader(type);
2660
- gl.shaderSource(sh, src);
2661
- gl.compileShader(sh);
2662
- if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) {
2663
- const info = gl.getShaderInfoLog(sh) ?? "unknown";
2664
- gl.deleteShader(sh);
2665
- throw new Error(`Shader compile error: ${info}`);
2666
- }
2667
- return sh;
2668
- }
2669
- function linkProgram(gl, vert, frag) {
2670
- const prog = gl.createProgram();
2671
- gl.attachShader(prog, vert);
2672
- gl.attachShader(prog, frag);
2673
- gl.linkProgram(prog);
2674
- if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
2675
- const info = gl.getProgramInfoLog(prog) ?? "unknown";
2676
- gl.deleteProgram(prog);
2677
- throw new Error(`Program link error: ${info}`);
2678
- }
2679
- return prog;
2680
- }
2681
- function makeTexture(gl, filter = gl.NEAREST) {
2682
- const t = gl.createTexture();
2683
- gl.bindTexture(gl.TEXTURE_2D, t);
2684
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter);
2685
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
2686
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
2687
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
2688
- return t;
2689
- }
2690
- var ANIM_STYLES = ["none", "wave", "pulse", "rain", "breathe", "sparkle", "glitch", "spiral", "typewriter", "scatter", "waveField", "ripple", "melt", "orbit", "cellular"];
2691
- var HOVER_EFFECTS = ["spotlight", "magnify", "repel", "glow", "colorShift", "attract", "shatter", "trail"];
2692
- var COLOR_MODES = ["grayscale", "fullcolor", "matrix", "accent"];
2693
- function tryCreateWebGLRenderer(canvas) {
2694
- const glOrNull = canvas.getContext("webgl2", {
2695
- alpha: true,
2696
- antialias: false,
2697
- premultipliedAlpha: false,
2698
- preserveDrawingBuffer: false
2699
- });
2700
- if (!glOrNull) return null;
2701
- const gl = glOrNull;
2702
- try {
2703
- let render2 = function(frame, options, displayW, displayH, time, hoverPos) {
2704
- const rows = frame.length;
2705
- if (rows === 0) return;
2706
- const cols = frame[0].length;
2707
- if (cols === 0) return;
2708
- gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
2709
- let hasTransparency = false;
2710
- const sy = Math.max(1, rows >> 2), sx = Math.max(1, cols >> 2);
2711
- outer: for (let iy = 0; iy < rows; iy += sy)
2712
- for (let ix = 0; ix < cols; ix += sx)
2713
- if (frame[iy][ix].a < 200) {
2714
- hasTransparency = true;
2715
- break outer;
2716
- }
2717
- gl.clearColor(
2718
- hasTransparency ? 0 : 0.0392,
2719
- hasTransparency ? 0 : 0.0392,
2720
- hasTransparency ? 0 : 0.0392,
2721
- hasTransparency ? 0 : 1
2722
- );
2723
- gl.clear(gl.COLOR_BUFFER_BIT);
2724
- if (cols !== lastCols || rows !== lastRows) {
2725
- cellBuf = new Uint8Array(cols * rows * 4);
2726
- lastCols = cols;
2727
- lastRows = rows;
2728
- cellDataDirty = true;
2729
- }
2730
- if (cellDataDirty) {
2731
- for (let y = 0; y < rows; y++) {
2732
- const row = frame[y];
2733
- for (let x = 0; x < cols; x++) {
2734
- const cell = row[x];
2735
- const i = (y * cols + x) * 4;
2736
- cellBuf[i] = cell.r;
2737
- cellBuf[i + 1] = cell.g;
2738
- cellBuf[i + 2] = cell.b;
2739
- cellBuf[i + 3] = cell.a;
2740
- }
2741
- }
2742
- gl.activeTexture(gl.TEXTURE0);
2743
- gl.bindTexture(gl.TEXTURE_2D, cellTex);
2744
- gl.texImage2D(
2745
- gl.TEXTURE_2D,
2746
- 0,
2747
- gl.RGBA,
2748
- cols,
2749
- rows,
2750
- 0,
2751
- gl.RGBA,
2752
- gl.UNSIGNED_BYTE,
2753
- cellBuf
2754
- );
2755
- cellDataDirty = false;
2756
- }
2757
- gl.activeTexture(gl.TEXTURE0);
2758
- gl.bindTexture(gl.TEXTURE_2D, cellTex);
2759
- gl.useProgram(prog);
2760
- gl.bindVertexArray(vao);
2761
- const cellW = displayW / cols;
2762
- const cellH = displayH / rows;
2763
- gl.uniform2f(u.canvasSize, displayW, displayH);
2764
- gl.uniform2f(u.gridSize, cols, rows);
2765
- gl.uniform2f(u.cellSize, cellW, cellH);
2766
- gl.uniform1f(u.time, time);
2767
- gl.uniform1f(u.animSpeed, options.animationSpeed);
2768
- gl.uniform1i(u.animStyle, ANIM_STYLES.indexOf(options.animationStyle));
2769
- gl.uniform1i(u.colorMode, COLOR_MODES.indexOf(options.colorMode));
2770
- const acHex = (options.accentColor || "#ffffff").replace("#", "");
2771
- gl.uniform3f(
2772
- u.accentColor,
2773
- parseInt(acHex.slice(0, 2), 16) / 255,
2774
- parseInt(acHex.slice(2, 4), 16) / 255,
2775
- parseInt(acHex.slice(4, 6), 16) / 255
2776
- );
2777
- gl.uniform1i(u.invert, options.invert ? 1 : 0);
2778
- const totalCells = rows * cols;
2779
- const animActive = options.animationStyle !== "none";
2780
- const suppressHover = animActive && totalCells > 5e3;
2781
- const hoverActive = !suppressHover && !!hoverPos && options.hoverStrength > 0;
2782
- const radiusScale = totalCells > 3e4 ? 0.25 : totalCells > 15e3 ? 0.4 : totalCells > 5e3 ? 0.6 : 1;
2783
- gl.uniform1f(u.hoverStrength, hoverActive ? options.hoverStrength : 0);
2784
- gl.uniform1f(u.hoverIntensity, hoverActive ? hoverPos?.intensity ?? 0 : 0);
2785
- gl.uniform2f(u.hoverPos, hoverPos?.x ?? 0.5, hoverPos?.y ?? 0.5);
2786
- gl.uniform1f(u.hoverRadius, options.hoverRadius * radiusScale);
2787
- gl.uniform1i(u.hoverEffect, HOVER_EFFECTS.indexOf(options.hoverEffect));
2788
- const hc = options.hoverColor || "#ffffff";
2789
- gl.uniform3f(
2790
- u.hoverColor,
2791
- parseInt(hc.slice(1, 3), 16) / 255,
2792
- parseInt(hc.slice(3, 5), 16) / 255,
2793
- parseInt(hc.slice(5, 7), 16) / 255
2794
- );
2795
- gl.uniform1i(u.renderMode, options.renderMode === "dots" ? 1 : 0);
2796
- gl.uniform1f(u.dotSizeRatio, options.dotSizeRatio);
2797
- gl.uniform1i(u.cellTexU, 0);
2798
- gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, cols * rows);
2799
- gl.bindVertexArray(null);
2800
- };
2801
- var render = render2;
2802
- const vert = compileShader(gl, gl.VERTEX_SHADER, VERT_SRC);
2803
- const frag = compileShader(gl, gl.FRAGMENT_SHADER, FRAG_SRC);
2804
- const prog = linkProgram(gl, vert, frag);
2805
- gl.deleteShader(vert);
2806
- gl.deleteShader(frag);
2807
- const vao = gl.createVertexArray();
2808
- gl.bindVertexArray(vao);
2809
- const corners = new Float32Array([-0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5]);
2810
- const vbo = gl.createBuffer();
2811
- gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
2812
- gl.bufferData(gl.ARRAY_BUFFER, corners, gl.STATIC_DRAW);
2813
- const aCorner = gl.getAttribLocation(prog, "a_corner");
2814
- gl.enableVertexAttribArray(aCorner);
2815
- gl.vertexAttribPointer(aCorner, 2, gl.FLOAT, false, 0, 0);
2816
- gl.bindVertexArray(null);
2817
- const cellTex = makeTexture(gl);
2818
- const g = (n) => gl.getUniformLocation(prog, n);
2819
- const u = {
2820
- canvasSize: g("u_canvasSize"),
2821
- gridSize: g("u_gridSize"),
2822
- cellSize: g("u_cellSize"),
2823
- time: g("u_time"),
2824
- animSpeed: g("u_animSpeed"),
2825
- animStyle: g("u_animStyle"),
2826
- colorMode: g("u_colorMode"),
2827
- accentColor: g("u_accentColor"),
2828
- invert: g("u_invert"),
2829
- hoverStrength: g("u_hoverStrength"),
2830
- hoverIntensity: g("u_hoverIntensity"),
2831
- hoverPos: g("u_hoverPos"),
2832
- hoverRadius: g("u_hoverRadius"),
2833
- hoverEffect: g("u_hoverEffect"),
2834
- hoverColor: g("u_hoverColor"),
2835
- renderMode: g("u_renderMode"),
2836
- dotSizeRatio: g("u_dotSizeRatio"),
2837
- cellTexU: g("u_cellTex")
2838
- };
2839
- let lastCols = 0;
2840
- let lastRows = 0;
2841
- let cellBuf = new Uint8Array(0);
2842
- let cellDataDirty = true;
2843
- gl.enable(gl.BLEND);
2844
- gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
2845
- return {
2846
- render: render2,
2847
- notifyDirty() {
2848
- cellDataDirty = true;
2849
- },
2850
- destroy() {
2851
- gl.deleteProgram(prog);
2852
- gl.deleteVertexArray(vao);
2853
- gl.deleteBuffer(vbo);
2854
- gl.deleteTexture(cellTex);
2855
- }
2856
- };
2857
- } catch (err) {
2858
- console.error("[asciify] WebGL renderer setup FAILED (returning stub):", err);
2859
- const stub = {
2860
- render() {
2861
- try {
2862
- gl.clearColor(0.039, 0.039, 0.039, 1);
2863
- gl.clear(gl.COLOR_BUFFER_BIT);
2864
- } catch {
2865
- }
2866
- },
2867
- notifyDirty() {
2868
- },
2869
- destroy() {
2870
- }
2871
- };
2872
- return stub;
2873
- }
2874
- }
2875
-
2876
2428
  exports.ART_STYLE_PRESETS = ART_STYLE_PRESETS;
2877
2429
  exports.CHARSETS = CHARSETS;
2878
2430
  exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
@@ -2906,7 +2458,6 @@ exports.renderTerrainBackground = renderTerrainBackground;
2906
2458
  exports.renderTextBackground = renderTextBackground;
2907
2459
  exports.renderVoidBackground = renderVoidBackground;
2908
2460
  exports.renderWaveBackground = renderWaveBackground;
2909
- exports.tryCreateWebGLRenderer = tryCreateWebGLRenderer;
2910
2461
  exports.videoToAsciiFrames = videoToAsciiFrames;
2911
2462
  //# sourceMappingURL=index.cjs.map
2912
2463
  //# sourceMappingURL=index.cjs.map