AI Stroke-by-Stroke Painting: How Code-as-Canvas Generation Works
This article breaks down an emerging approach to AI image generation in which a model writes an executable drawing program instead of outputting a finished bitmap. It compares this code-driven, stroke-by-stroke method with mainstream diffusion-based generation, explains three core mechanisms—structured stroke instructions, observable canvas state with visual feedback, and additive self-correction—and outlines a layered architecture for long-horizon tasks. It closes with practical limitations and adoption guidance for developers.
1. The Phenomenon: A Painting Is Really a Program
A recent release of an intelligent coding tool demonstrated a way of painting that differs sharply from conventional image generation. Instead of emitting a finished bitmap, the model writes an executable drawing program. That program simulates the physical properties of the brush, paint, and canvas on a virtual surface, building the image up one stroke at a time.
According to the public technical demo, several characteristics of this kind of output stand out:
- The code behind a single piece typically runs on the order of 1,000 to 2,000 lines;
- The total number of strokes varies widely—from a simple sketch of a little over a hundred strokes to a “photorealistic” still life of roughly 700,000 strokes;
- It relies on no external image-generation model, and there is no pre-rendered or traced base image;
- Each stroke is an independent instruction with its own coordinates and parameters, and once laid down it cannot be undone.
This means the “artistic effect” of the output is not a direct product of the model’s parameters, but the accumulated result of an execution engine physically simulating a sequence of instructions. Understanding this point is the starting point for understanding the entire technical approach.
2. Path Differences: Iterative Denoising vs. Instruction-Based Construction
Most mainstream image-generation approaches are built on diffusion models: they start from random noise, repeatedly denoise in latent space, and finally decode the result into a pixel matrix in a single pass. The advantage is that the process is end-to-end, highly parallel, and produces an image in one inference run. The shortcomings are equally clear—the process is an indivisible black box; the model cannot “take a look at what it has drawn so far” partway through, and it struggles to make targeted rework on a local region.
Code-simulated painting takes a different route: it decomposes drawing into a large number of discrete, serializable stroke instructions, with the model acting as the planner and the engine as the renderer. The two approaches can be contrasted as follows:
| Dimension | Diffusion-based generation | Code-simulated stroke-by-stroke painting |
|---|---|---|
| Output form | Pixel matrix | Executable program + canvas state |
| Intermediate observability | Weak (latent variables are unreadable) | Strong (canvas can be sampled and reviewed at any time) |
| Local correction | Relies on redrawing / local resampling | Adds new strokes to cover |
| Controllability | Prompt + random seed | Explicitly controllable per stroke |
| Compute profile | Single high-parallelism inference | Long-running, multi-turn, serial decisions |
It is worth stressing that these two routes are not replacements for each other. The former suits high-throughput visual-asset production, while the latter is better suited to scenarios that require the process to be explainable, auditable, and interruptible.
3. Breaking Down the Three Core Mechanisms
3.1 Stroke Instruction-ization: Reducing Painting to Structured Output
What the model outputs is not an image but a sequence of structured instructions. Take the first stroke recorded in the demo as an example; its semantics are roughly as follows:
stroke(color="#30346a", # color codewidth=90, # brush width (pixels)points=[ # path control points(120, 340),(126, 341),(131, 340),(137, 342),],wetness=0.6, # paint wetness, affects edge diffusionbristle=0.15, # bristle splitting intensity, affects texture residue)
The detail most worth studying here is “imperfection injection”: in the demo, the model deliberately gave the path points a slight wobble of about 7 pixels, on the reasoning that a human arm cannot draw an absolutely straight line either. This is a classic case of anthropomorphic noise modeling—deliberately introducing controlled randomness at the parameter level to avoid the “too clean” plastic feel common in computer graphics.
From an engineering standpoint, such an instruction set usually needs to cover: brush type and size, color and transparency, wetness and drying time, path interpolation method, pressure curve, and canvas texture and absorption. The closer the instruction set is designed to real media, the more credible the final image.
3.2 Observable Canvas State: Giving the Model “Visual Feedback”
The premise that makes the stroke-by-stroke route work is that the model can read the canvas’s current state at any moment and re-encode it into understandable input. This forms a standard perception–decision loop:
initialize a blank canvas↓render the current stroke → update canvas state↓sample the canvas (full thumbnail + local high-resolution crops)↓model evaluates: composition / proportion / light and shade / edges meet the bar?↓ (no)generate corrective strokes → back to rendering↓ (yes)output the final canvas and the complete instruction log
Engineering recommendation: do not return only a full thumbnail. High-resolution tasks should use a dual-channel observation of “global thumbnail + key-region crops,” otherwise the model struggles to judge fine-detail problems; at the same time, version the canvas state so it can be traced and reproduced.
3.3 Closed-Loop Self-Correction: Errors Can Only Be Covered, Not Deleted
Because strokes cannot be undone, the correction strategy must be “additive.” Two representative cases from the public demo:
- The lines on the bird’s back were too thin and looked messy, so the model switched to thicker strokes to cover the original area and made the residual bristle marks along the edges closer to the texture of fluffy feathers;
- In a dusk scene, after lighting effects were added later, the paint tubes and cup on the desk were accidentally blurred, so the model laid down fresh strokes to redraw these objects; it also adjusted the poses of two figures so that each paints with the hand on the outer side of the body, allowing their inner shoulders to touch as the prompt required.
This suggests that in a non-revertible, construction-based generation setting, correction capability is equivalent to the ability to “constrain earlier results with later operations.” In system design terms, this calls for explicit quality-check stages (composition constraints, occlusion relationships, lighting consistency) and a failure-retry budget, rather than hoping to get it right in one pass.
4. An Engineering Architecture for Long-Horizon Tasks
The value of this capability extends beyond painting. The paradigm of stepwise planning, observing results, and self-correcting is highly isomorphic to physical tasks such as robotic control. An internal test disclosed by a cloud vendor mentions that, for complex, long-running technical tasks, this class of model can coordinate multiple sub-agents during execution and repeatedly verify and improve results; cited cases include solving two open problems in black hole physics, collaborating with a circuit board manufacturer to design three working electronic devices, and coordinating more than 1,000 sub-agents over 8 days to write an operating system that runs on a laptop.
To implement similar capabilities in your own system, you can refer to the following layering:
- Execution sandbox layer: run the model-generated code in isolation, restricting the file system, network, and CPU time to prevent long loops and unauthorized access.
- State storage layer: use object storage to save canvas snapshots and instruction logs, and a message queue to decouple “rendering” from “evaluation,” supporting parallel sub-agents.
- Observation and evaluation layer: provide a unified canvas-sampling interface and a quality-scoring service, outputting structured feedback rather than natural-language descriptions.
- Orchestration layer: responsible for task decomposition, sub-agent scheduling, budget control (max strokes / max rounds / timeout), and termination decisions.
- Observability layer: track per-round latency, stroke increments, and number of corrections via logging services and monitoring alerts, making cost attribution easier.
A schematic orchestration skeleton:
def paint(task, engine, critic, budget):canvas = engine.blank_canvas()for step in range(budget.max_steps):obs = critic.observe(canvas) # global + local samplingif obs.satisfied():breakfor cmd in planner.next_strokes(obs): # per-stroke structured instructionscanvas = engine.render(canvas, cmd) # strokes cannot be undoneengine.checkpoint(canvas, step) # versioned snapshotreturn canvas, engine.instruction_log()
5. Limitations and Adoption Recommendations
- Different cost structure: hundreds of thousands of strokes mean a large amount of serial decision-making and rendering overhead, which is not suitable for high-concurrency, low-latency asset-production scenarios.
- Consistency depends on validation: the non-revertible nature magnifies the cost of early errors, so composition and proportion constraints in the early stages must be front-loaded.
- The instruction set is the moat: the fidelity of the physical simulation sets the ceiling, so it is advisable to prioritize investment in the brush model and media modeling.
- Suitable scenarios: visual and technical tasks that require the process to be recorded, auditable, and locally reworkable; throughput-sensitive scenarios should still choose an iterative generation approach.
On the whole, the significance of code-simulated stroke-by-stroke painting lies not in replacing existing image-generation technology, but in demonstrating a generation paradigm that is “observable, verifiable, and correctable.” For developers, the truly reusable takeaway is this: reframe a generation task from a one-shot inference into a long-running execution flow with a feedback loop.
