Skip to content

aaqidmasoodi/CodeScapes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

682 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeScapes

A Multi-Runtime, Browser-Native IDE for Code Education and Creative Development

CodeScapes is an A zero-setup, browser-based playground for learning, teaching and sharing code.


Demonstration: What Actually Works

Before diving into architecture, let's establish what "works" means in practice. These are real programs running in the browser, unchanged from their desktop versions.

Interactive Games

Pong: Two-player game with keyboard controls (W/S and Arrow keys), ball physics, paddle collision detection and live scoring.

View Project
GIF: Pong Game Build with Turtle Graphics Running in the Browser

Brick Breaker: Paddle-controlled ball bouncing through destructible bricks. Requires onkeypress(), collision detection, and tracer(0)/update() for smooth animation.

View Project
GIF: Brick Breaker showing brick destruction and score updates

Both games use the standard game loop pattern:

  1. Disable automatic screen updates with tracer(0)
  2. Process input events
  3. Update game state
  4. Redraw everything
  5. Call update() to display the frame
  6. Sleep briefly to control framerate

This pattern requires blocking operations, event handling and flicker-free rendering, all of which fail in browser-based Python environments like Skulpt, Brython, and previous Pyodide turtle implementations.


Generative Art and Visualization

Animated Spirograph: A spiral that rotates continuously in real-time.

View Project
GIF: Rotating spiral animation at 60 FPS

GIF only shows 15 fps, view original project at https://www.codescapes.io/view/71622718-fe46-4c80-a27c-d172585ce6c0

Pattern Playlist: Distinct geometric patterns (spirographs, mandalas, kaleidoscopes) cycling automatically with one-second intervals.

View Project
GIF: Multiple patterns transitioning

These demonstrate sustained animation loops with time.sleep() for pacing. This is another operation that typically breaks in Pyodide without special handling.


Interactive 3D Graphics

3D Spiral with Mouse Control: A golden spiral rendered in 3D space. Click and drag to rotate the view around the Y-axis.

View Project
GIF: 3D spiral responding to mouse drag input

This example combines onscreenclick(), onrelease(), and ondrag() for continuous position updates. The full mouse event API working correctly is rare in browser implementations.


Multi-File Applications

Live Weather Dashboard: A three-file application that prompts for a city name, fetches weather data from an API and displays results using turtle graphics as the UI layer.

View Project
GIF: Weather app workflow: input city, fetch data, display results

This demonstrates Python's input() function working correctly, blocking execution until the user responds without freezing the browser—a critical feature for educational scripts.

Slow Drawing Examples

Neon Vortex Glow Rosette
View Project View Project
Skulpt Example Spiral Multi Color
View Project View Project

System Architecture

The fundamental challenge of running Turtle in the browser is the mismatch between Python's synchronous, blocking execution model and the browser's asynchronous, event-driven nature.

In Python, time.sleep(1) blocks the thread. In the browser's main thread, blocking freezes the UI. To solve this, we run Pyodide in a Web Worker, isolating it from the UI thread. However, the Worker cannot access the DOM or Canvas directly.

Our solution bridges these two worlds through a message-passing protocol.

High-Level Component Diagram

PNG: Pyodide Turtle Graphics in Browser High-Level Component Diagram

  1. Python (Web Worker): Runs the user's code. We shim the turtle module to capture commands (e.g., forward, penup) and send them to the main thread. It maintains its own internal state so queries like xcor() return immediately.
  2. Renderer (Main Thread): A React component that receives commands and updates the HTML5 Canvas.
  3. Synchronization: The Worker waits for the Renderer when necessary (e.g., during sleep or input), ensuring the timing matches the user's expectations.

Double-Buffered Rendering Pipeline

Flicker-free animation is critical for games. We improved upon standard canvas drawing by implementing a double-buffered rendering pipeline.

We maintain multiple off-screen canvases:

  1. Background Buffer: For static background colors or images.
  2. Ink Buffer: For permanent drawings (lines, stamps, fills).
  3. Sprite Buffer: For moving turtles.

PNG: Pyodide Turtle Buffer Architecture

When running animations with tracer(0), we draw to these off-screen buffers and only composite them to the visible display when update() is called. This eliminates the flickering often seen in other implementations where every single drawing command triggers a repaint.


Event System Design

Perhaps the most interesting technical challenge was handling synchronous events. How do you implement a blocking input() or wait for a keypress in a Web Worker without freezing the browser?

We use a Service Worker combined with synchronous XHR.

  1. Python needs to wait for an event (like a keypress).
  2. It makes a Synchronous XHR request to a special URL intercepted by our Service Worker.
  3. This blocks the Worker thread (pausing execution), which is exactly what we want.
  4. The Main Thread continues to run, listening for user input (keyboard/mouse).
  5. When input occurs, the Main Thread sends the data to the Service Worker.
  6. The Service Worker responds to the pending XHR request, unblocking the Python Worker.

This allows Python to feel "blocked" while the browser UI remains completely responsive.


Conclusion

This implementation demonstrates that we don't have to compromise on the educational integrity of Python when bringing it to the browser. By staying true to the CPython execution model, we empower students to use standard learning resources and build real, improved applications.

None of this would be possible without Pyodide. This turtle shim runs directly in the browser on Pyodide.


⚠️ License & Usage Restrictions

This software is licensed for NON-COMMERCIAL use only.

You may:

  • Use this software for personal, educational, or research purposes
  • Modify and fork the code for non-commercial projects
  • Reference this architecture in academic papers (with attribution)

You may NOT:

  • Use this software or derivatives in commercial products
  • Sell or monetize applications built with this codebase
  • Deploy this software as a paid service

For commercial licensing inquiries, please contact:


🤝 Collaboration & Contact

We welcome contributions from the open-source community! Whether you're interested in:

  • Adding new runtime environments
  • Improving the execution sandbox
  • Enhancing the block-based FlowScape editor
  • Documentation and research papers

Get in touch:

About

CodeScape is a modern, creative coding environment designed for building and prototyping interactive coding Scapes

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors