Legacy Examples
This directory retains the 16 demo entry points from the legacy
example/directory, preserving filenames with a.cjsextension. 14 entry points are adapted to the current API; 2 asynchronous drawing entry points reportUNSUPPORTEDand exit with code 2.
All examples · API guide · Development guide
These scripts show how legacy scenes behave with the current API. For new applications, start with Quick start.
Running
Run from the project root:
# The initial build includes the native module
npm run build
# Open the combined demo window
npm run demo:ported
# Run any ported script
node examples/ported/texture.cjs
node examples/ported/sound_buffer.cjs
# Verify results, reporting passes and unsupported cases separately
npm run test:portedExample entry points
| File | Demonstration |
|---|---|
| color.cjs | Color construction, component writes and JSON output |
| vector2.cjs | 2D vectors, scalar conversion, copying and named operations |
| vector3.cjs | 3D vectors, scalar conversion, copying and named operations |
| time.cjs | Time factories, bigint microseconds and Time.Zero |
| rect.cjs | position/size, containment and intersections, preserving the original invalid-input demonstration |
| video_mode.cjs | Video modes, sizes, NaN input, desktop and fullscreen mode queries |
| vertex.cjs | Vertex member views and VertexArray access |
| joystick.cjs | Joystick connection, identification, button counts and axis queries |
| sensor.cjs | Availability, enabling and reading of all sensors |
| touch.cjs | Touch state and position queries |
| font.cjs | Failed loading, font loading, glyph at code point 1/character size 10 and line spacing |
| texture.cjs | Failed loading, GIF loading, cropping, texture copying and Sprite |
| sound_buffer.cjs | WAV decoding, PCM copying, saving and reloading |
| render_window.cjs | The original combined scene's shapes, vertex lines, gradient pixels, cropping, masks, Sprite, Chinese text and audio |
| render_window_display_async.cjs | Unsupported: original synchronous/asynchronous display comparison; displayAsync is missing |
| render_window_display_draw_async.cjs | Unsupported: original combined asynchronous drawing scene; drawAsync and displayAsync are missing |
Window interaction
render_window.cjs uses the original VideoMode.getFullscreenModes()[3], title hello and style 5. It preserves the red background, original scene layout, default volume, a single Sound playback at startup, and Music configured to loop without starting playback. Scheduling remains setFramerateLimit(60) and setImmediate(frame).
| Key | Action |
|---|---|
| Esc/close event | Close the window; Esc also retains real-time querying |
| Left | Add 1 to both x and y of the window position |
| LControl | Recreate at 800 x 600 while held and enable vertical sync |
| LAlt | Print context settings |
| RAlt | Change the window title |
| S | Request the virtual keyboard, subject to system support |
The original mouse logic is also preserved: read desktop and window-relative coordinates every frame, clamp to the original mode bounds, then call Mouse.setPosition. The original key-repeat behavior remains.
On non-Windows systems, the combined example still saves /tmp/xxx.png. The SoundBuffer example still saves .game.wav under os.tmpdir(), then reloads it without automatically deleting it.
Porting conventions
Adaptations cover import paths, SFML 3 arguments and method names, value types and asynchronous loading. Scene parameters, animation formulas, draw order, input, file output and frame scheduling preserve the original logic. Images, fonts and audio are in assets/.
The two entry points with async in their names do not emulate legacy APIs with synchronous drawing or fixed sleeps. The display comparison retains 10 frames, a 50 ms heartbeat, the setImmediate wrapper, displayAsync and error checks after closing; the combined asynchronous scene retains sequential await drawAsync/displayAsync calls.
Coordinates and sizes use Vector2, rotations use Angle, colors use Color, and events are read through is/getIf; numeric enum iteration filters reverse mappings. Loading operations check booleans; the original missing paths abcdefg/abc.jpg are preserved. console.log/JSON.stringify receive new objects directly, with output following the new types' own representation. Integer conversion and invalid-value handling also follow the new value types, without emulating legacy validation in the examples.
Each script contains its demo logic independently, no longer using shared scenes or output-formatting helpers. Formerly synchronous resource loads now use await in the original order, not parallel preloading.
Automated verification
test:ported uses a separate test preload script that normal example runs do not load. During testing, it selects a null audio device, checks mouse settings without moving the system pointer, and redirects file output to an isolated directory. If the virtual display offers fewer than four fullscreen modes, the test fills the remaining options with the desktop mode.
The test script closes the combined window after 8 frames, checking layout, colors, point counts, scale, text and vertex lines per frame. Tests count 14 runnable entry points and 2 unsupported entry points separately; unsupported cases do not count as successful ports. Null-audio-device testing does not prove actual speaker output works.