@nexart/ui-renderer 0.2.1 → 0.4.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/README.md +240 -174
- package/dist/compiler.d.ts +18 -2
- package/dist/compiler.d.ts.map +1 -1
- package/dist/compiler.js +25 -11
- package/dist/index.d.ts +19 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -7
- package/dist/presets/backgrounds.d.ts +14 -0
- package/dist/presets/backgrounds.d.ts.map +1 -0
- package/dist/presets/backgrounds.js +222 -0
- package/dist/presets/index.d.ts +7 -0
- package/dist/presets/index.d.ts.map +1 -0
- package/dist/presets/index.js +6 -0
- package/dist/presets/primitives.d.ts +16 -0
- package/dist/presets/primitives.d.ts.map +1 -0
- package/dist/presets/primitives.js +282 -0
- package/dist/presets/sketch-wrapper.d.ts +14 -0
- package/dist/presets/sketch-wrapper.d.ts.map +1 -0
- package/dist/presets/sketch-wrapper.js +70 -0
- package/dist/preview/code-renderer.d.ts +46 -0
- package/dist/preview/code-renderer.d.ts.map +1 -0
- package/dist/preview/code-renderer.js +759 -0
- package/dist/preview/renderer.d.ts +1 -1
- package/dist/preview/renderer.d.ts.map +1 -1
- package/dist/preview/renderer.js +23 -13
- package/dist/preview/unified-renderer.d.ts +16 -0
- package/dist/preview/unified-renderer.d.ts.map +1 -0
- package/dist/preview/unified-renderer.js +270 -0
- package/dist/system.d.ts +7 -3
- package/dist/system.d.ts.map +1 -1
- package/dist/system.js +204 -11
- package/dist/types.d.ts +127 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +12 -3
- package/package.json +5 -2
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sketch Wrapper - Safety normalization for raw Code Mode sketches
|
|
3
|
+
* Wraps user-provided code with aesthetic defaults and safety measures
|
|
4
|
+
*/
|
|
5
|
+
import { AESTHETIC_DEFAULTS } from '../types';
|
|
6
|
+
export function wrapSketch(config) {
|
|
7
|
+
const { code, normalize = true } = config;
|
|
8
|
+
if (!normalize) {
|
|
9
|
+
return code;
|
|
10
|
+
}
|
|
11
|
+
const bgColor = `rgb(${AESTHETIC_DEFAULTS.background.r}, ${AESTHETIC_DEFAULTS.background.g}, ${AESTHETIC_DEFAULTS.background.b})`;
|
|
12
|
+
const hasSetup = /function\s+setup\s*\(/.test(code);
|
|
13
|
+
const hasDraw = /function\s+draw\s*\(/.test(code);
|
|
14
|
+
if (hasSetup && hasDraw) {
|
|
15
|
+
return injectNormalizationIntoExistingCode(code, bgColor);
|
|
16
|
+
}
|
|
17
|
+
if (!hasSetup && !hasDraw) {
|
|
18
|
+
return wrapBareCode(code, bgColor);
|
|
19
|
+
}
|
|
20
|
+
return code;
|
|
21
|
+
}
|
|
22
|
+
function injectNormalizationIntoExistingCode(code, bgColor) {
|
|
23
|
+
let modified = code;
|
|
24
|
+
if (!/strokeWeight\s*\(/.test(code)) {
|
|
25
|
+
modified = modified.replace(/function\s+setup\s*\(\s*\)\s*\{/, `function setup() {\n strokeWeight(${AESTHETIC_DEFAULTS.strokeWeight.default});`);
|
|
26
|
+
}
|
|
27
|
+
if (!/background\s*\(/.test(code)) {
|
|
28
|
+
modified = modified.replace(/function\s+draw\s*\(\s*\)\s*\{/, `function draw() {\n background('${bgColor}');`);
|
|
29
|
+
}
|
|
30
|
+
return modified;
|
|
31
|
+
}
|
|
32
|
+
function wrapBareCode(code, bgColor) {
|
|
33
|
+
return `
|
|
34
|
+
function setup() {
|
|
35
|
+
strokeWeight(${AESTHETIC_DEFAULTS.strokeWeight.default});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function draw() {
|
|
39
|
+
background('${bgColor}');
|
|
40
|
+
|
|
41
|
+
${code}
|
|
42
|
+
}
|
|
43
|
+
`;
|
|
44
|
+
}
|
|
45
|
+
export function validateSketchSafety(code) {
|
|
46
|
+
const warnings = [];
|
|
47
|
+
const dangerousPatterns = [
|
|
48
|
+
{ pattern: /eval\s*\(/, message: 'eval() is not allowed' },
|
|
49
|
+
{ pattern: /Function\s*\(/, message: 'Function constructor is not allowed' },
|
|
50
|
+
{ pattern: /document\s*\./, message: 'Direct DOM access is not allowed' },
|
|
51
|
+
{ pattern: /window\s*\./, message: 'Window access is not allowed' },
|
|
52
|
+
{ pattern: /import\s*\(/, message: 'Dynamic imports are not allowed' },
|
|
53
|
+
{ pattern: /require\s*\(/, message: 'require() is not allowed' },
|
|
54
|
+
];
|
|
55
|
+
for (const { pattern, message } of dangerousPatterns) {
|
|
56
|
+
if (pattern.test(code)) {
|
|
57
|
+
warnings.push(message);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (/Math\.random\s*\(/.test(code)) {
|
|
61
|
+
warnings.push('Math.random() may break determinism - use random() instead');
|
|
62
|
+
}
|
|
63
|
+
if (/Date\s*\(/.test(code) || /Date\.now\s*\(/.test(code)) {
|
|
64
|
+
warnings.push('Date functions may break determinism - use t or frameCount instead');
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
safe: warnings.filter(w => !w.includes('may break')).length === 0,
|
|
68
|
+
warnings,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @nexart/ui-renderer v0.4.0 - Code Mode Renderer
|
|
3
|
+
*
|
|
4
|
+
* ╔══════════════════════════════════════════════════════════════════════════╗
|
|
5
|
+
* ║ PREVIEW RENDERER — MIRRORS @nexart/codemode-sdk BEHAVIOR ║
|
|
6
|
+
* ║ ║
|
|
7
|
+
* ║ This file is a MIRROR, not an authority. ║
|
|
8
|
+
* ║ ║
|
|
9
|
+
* ║ Authority: @nexart/codemode-sdk (Protocol v1.0.0) ║
|
|
10
|
+
* ╚══════════════════════════════════════════════════════════════════════════╝
|
|
11
|
+
*
|
|
12
|
+
* ARCHITECTURAL NOTE:
|
|
13
|
+
* -------------------
|
|
14
|
+
* Live preview animation requires local p5 runtime execution because:
|
|
15
|
+
* - @nexart/codemode-sdk.executeCodeMode() returns blobs (PNG/MP4), not frames
|
|
16
|
+
* - Real-time animation in the browser requires frame-by-frame canvas updates
|
|
17
|
+
* - The SDK's loop mode produces video files, not real-time rendering
|
|
18
|
+
*
|
|
19
|
+
* To ensure faithful mirroring, this renderer:
|
|
20
|
+
* 1. Uses identical forbidden pattern validation as the SDK
|
|
21
|
+
* 2. Uses identical VAR handling (read-only, 10 elements, 0-100 range, errors not clamps)
|
|
22
|
+
* 3. Uses identical seeded RNG (Mulberry32) and Perlin noise
|
|
23
|
+
* 4. Uses identical time variable semantics (frameCount, t, time, tGlobal)
|
|
24
|
+
*
|
|
25
|
+
* For archival/canonical output, use @nexart/codemode-sdk directly.
|
|
26
|
+
*/
|
|
27
|
+
import type { NexArtCodeSystem, PreviewOptions } from '../types';
|
|
28
|
+
export interface CodeRenderer {
|
|
29
|
+
render: () => void;
|
|
30
|
+
start: () => void;
|
|
31
|
+
stop: () => void;
|
|
32
|
+
destroy: () => void;
|
|
33
|
+
isCanonical: false;
|
|
34
|
+
isArchival: false;
|
|
35
|
+
}
|
|
36
|
+
interface P5Runtime {
|
|
37
|
+
[key: string]: any;
|
|
38
|
+
width: number;
|
|
39
|
+
height: number;
|
|
40
|
+
frameCount: number;
|
|
41
|
+
VAR: readonly number[];
|
|
42
|
+
}
|
|
43
|
+
export declare function createP5Runtime(canvas: HTMLCanvasElement, width: number, height: number, seed: number, vars?: number[]): P5Runtime;
|
|
44
|
+
export declare function renderCodeModeSystem(system: NexArtCodeSystem, canvas: HTMLCanvasElement, options?: PreviewOptions): CodeRenderer;
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=code-renderer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-renderer.d.ts","sourceRoot":"","sources":["../../src/preview/code-renderer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAMjE,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,WAAW,EAAE,KAAK,CAAC;IACnB,UAAU,EAAE,KAAK,CAAC;CACnB;AA4CD,UAAU,SAAS;IACjB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,SAAS,MAAM,EAAE,CAAC;CACxB;AAgFD,wBAAgB,eAAe,CAC7B,MAAM,EAAE,iBAAiB,EACzB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,IAAI,GAAE,MAAM,EAAmC,GAC9C,SAAS,CA0eX;AAsCD,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,gBAAgB,EACxB,MAAM,EAAE,iBAAiB,EACzB,OAAO,GAAE,cAAmB,GAC3B,YAAY,CA4Md"}
|