Skip to content

Commit

Permalink
Add scripts example.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu committed Apr 26, 2024
1 parent 2c8dcf5 commit ea1cebc
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions Documentation.docc/8.Scripts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Scripts

Scripts enhance the Draw Things experience by allowing users to create and run custom JavaScript snippets. The API is designed to be stable; scripts can specify an API version to ensure compatibility with future updates.

## API Versioning

Scripts should specify their API version to guarantee the proper API toolchain is used, even if API changes occur in future updates.

## Available Global Objects

Draw Things scripts provide access to three main global objects: `canvas`, `pipeline`, and `filesystem`.

### `canvas`

Manipulates the Draw Things interface's infinite canvas. The available methods include:

- **`canvas.boundingBox`**: Returns the bounding box of images collectively on the canvas.
- **`canvas.clear()`**: Removes all images, similar to tapping the "New Canvas" button.
- **`canvas.createMask(width, height, value)`**: Creates a mask of specified size and value.
- **`canvas.currentMask`**: Returns the current mask viewable on the canvas.
- **`canvas.foregroundMask`** and **`canvas.backgroundMask`**: Return masks corresponding to the foreground and background, respectively, using built-in segmentation models.
- **`canvas.loadImage(file)`**: Loads an image from a file.
- **`canvas.loadImageSrc(src)`**: Loads an image from an src content string, similar to an HTML `<img>` tag.
- **`canvas.moveCanvas(left, top)`** and **`canvas.moveCanvas(rect)`**: Move and zoom the canvas.
- **`canvas.saveImage(file, visibleRegionOnly)`** and **`canvas.saveImageSrc(visibleRegionOnly)`**: Save the current canvas image to a file or src string.
- **`canvas.canvasZoom`** and **`canvas.topLeftCorner`**: Control and report the canvas's zoom scale and top-left corner coordinates.
- **`canvas.updateCanvasSize(configuration)`**: Adjusts the canvas size based on a configuration object.
- **`canvas.loadMaskFromPhotos()`, `canvas.loadMaskFromFiles()`, `canvas.loadMaskFromSrc(srcContent)`**: Load masks from various sources.
- **`canvas.detectFaces()`**: Detects and returns face positions within the canvas.

### `pipeline`

Manages the image generation pipeline with methods like:

- **`pipeline.configuration`**: Extracts the current configuration.
- **`pipeline.findControlByName(name)`** and **`pipeline.findLoRAByName(name)`**: Find specific controls or LoRAs by name.
- **`pipeline.downloadBuiltins(names)`**: Downloads built-in models and components.
- **`pipeline.prompts`**: Returns the current prompts on the screen.
- **`pipeline.run(options)`**: Runs the image generation pipeline with optional prompts and masks.

### `filesystem`

Provides access to the file system, within sandbox limitations:

- **`filesystem.pictures.path`**: Retrieves the path of the pictures directory.
- **`filesystem.pictures.readEntries`** and **`filesystem.readEntries(directory)`**: Enumerate files in specific directories.

### `requestFromUser`

Creates a user interface for input via script configuration. Usage:

```javascript
const config = requestFromUser("This is the title", "Next Button", function() {
return [
this.section("This is a section", "Some details of this section.", [
this.slider(0.5, this.slider.percent, 0, 1, "a slider"),
this.size(960, 1280, 128, 2048)
]),
this.plainText("Some plain text."),
this.textField("", "This is a placeholder", true, 94),
this.imageField(),
this.directory(),
this.image("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAADAFBMVEWVLyaMJiCFIBt8FxRpDAtUBwRgDQhZAwRlEQVoNzVvLSh6MyRqMB1qMSeHUUGTSClZIgtMGQNhIRFSLBhyRitsSB+1IiJmRiVfOBJRLw5tRRd8VCV8ViyIXzChcTyMeHauVU7ISUO+Niu3QDWtPjChNChkT0NtWVBYPDNHHRlOEQo2AwBEBgRICAE7AQBUEweGMyeJHReSJR2ZIRyoKCOrQz+qbGaaeVdwRzF7UjRoOiJlNSZpSztaPCO/oGKcfEz47Lfq05bt0473783UvZHWxqy9p4jQvaOvmXvItpzMwK24qZWnmYSxmIepiHyjeHaglZKUjIiIfXp2a2ZjUEtkSUdYPTlLLChcODRUJyRrXVpyVlJ9amdYUEs3LShSPzlsRkGQSUCAPTWqeW7KsaN2RkB6Uj+iclard2XElYi8XlvPnJzMrq3IoaLCkZLEjI2+lpfBqKjItLO8eHbGhIS2mpm0mXOvkGNqTDKGZ0RZPitIMB43IhIuGAYoEwUhCAEsAwAkBQMSBQEaBgIfDwImG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S7LRw8sSI37Lu/RR7qQT9mLdg7Sigrn84NG5OEgPMETT635YfH9qg6Jl1SGe8NLibWQ/PipUzuf4XxcWHRzZSjNpKt+XPvxsfbAkyUtuy9sXLSP2FRGSnX6OGjzFoXUuoJ8TrakOJVbrEDqVNWkymvKC65dbFemctY23KFBkxYcpl3tSoUz06MKGuWUO45BWjR+0bgkxvds0PBlCVK8WJussBICUoGzYW375Y3ZNl/6maVZh7Ehr8iSF7ii9IlvXOaxncMZk9/Z+615/bEZWwG3d7WZ6YKxmYhc7Pjpzkl1Uj3lQPGk79tWZ2rG9otxcu3qqWHzTW+MrI9V4uZzjtixtxPFLh0QlvbrU7no8zkLw7G230w8UYRjboi5gJeh+pQSMb7FkUTKCtzool0qyO8uW/G/R+UJmw0ihJ3FMaxC3VcXyPjX2FirV4KJAd5Bg4tqXv109h0CodDYmEEAIpjMoZdLFZWE4hAQWQCrjreHRDMsgQghDzJrwYLLiWeaTPdcIRIGOcSQ7lR2Xqs7EEnbOJUkmpb3Kg21xen0DB0lUbf3L4K+8A7AyyXkR2hJE0rTtZ+GzJib7BdODpqRsaWk+6wb7cLVf/8ED4iisn69DSIFLnP+UK3bh53H3254TuLfhdkk4MZLi0eNfqVJlwNo+xU9XBr6Ck1J2/4iwe/u/iYeSF4nWp7l3Y3gbg3ra/PdBPTtheYmNkG9vponmH3mLL4Lfbb/H170a5Sv4+TyAAAAAElFTkSuQmCC")
];
});
```

Supported widgets include sliders, text fields, image and directory pickers, switches, segmented controls, menus, sections with details, plain text displays, and image displays. This function is blocking; it waits for user input before returning an array of selected values.

- **`this.slider(value, valueType, minValue, maxValue, title)`**: a single slider. valueType can be `this.slider.percent`, `this.slider.fractional(k)` or `this.slider.scale`.
- **`this.textField(value, placeholder, multiline, height)`**: a free-form text field for user input.
- **`this.imageField(title, multiSelect)`**: a image picker.
- **`this.directory()`**: a directory picker.
- **`this.switch(isOn, title)`**: a on / off switch.
- **`this.segmented(index, options)`**: a segmented control. options should be a list of strings.
- **`this.menu(index, options)`**: a menu. options should be a list of strings.
- **`this.section(title, detail, views)`**: a title + detail wrapper. views should be an array of these widgets again.
- **`this.plainText(value)`**: show a text display.
- **`this.image(src, height, selectable)`**: show a image display. src can be a string or an array of strings. Later is when selectable is true.

This comprehensive API suite allows developers to create powerful custom functionalities within the Draw Things environment.

0 comments on commit ea1cebc

Please sign in to comment.