2.0.0-beta.2 • Published 5 months ago

@code-not-art/sketch v2.0.0-beta.2

Weekly downloads
-
License
GPL-3.0-or-later
Repository
github
Last release
5 months ago

Code and Contributions License NPM Version Language: TypeScript

Sketch Framework

Framework used to create generative art using pseudo-random and parametric algorithms. This library provides an interface for a Sketch object that defines a parameterized algorithm for generative artwork, and React components to run those Sketch algorithms and render them on an HTML canvas.

The primary use of this is to be used in an interactive development environment, where a coder can make changes to a Sketch and their browser will immediately render their updated algorithm.

In addition to this original purpose, the React components can be embedded into any website as a means to provide control over an HTML canvas, or to display a sketch in a (non-development) context.

Sketch Web Interface Controls

The sketch controller provides keyboard shortcuts for controlling the development environment.

These are disabled by default to prevent hijacking keyboard commands outside of the sketch developer use case.

KeyAction
sSave the current image
uShareable image URL - Create a shareable URL and copy that link to your clipboard
mShow/Hide Parameter Menu
spaceDraw new image with random image and color seeds.
Move to next color seed, or generate a new one if at end of list. Draw new image.
Move to previous color seed. Draw new image.
Move to next image seed, or generate a new one if at end of list. Draw new image.
Move to previous image seed. Draw new image.
cGenerate new color seed. Draw new image.
iGenerate new image seed. Draw new image.

The Sketch Interface and Lifecycle

The SketchController expects a prop of the Sketch type. This interface allows you to provide configuration details for your sketch (config), and interactable parameters that you can update in browser (params). There are several methods for available for you to implement that will interact with the canvas and the seeded random generators provided by the framework. The only one of these that are absolutely required to provide is the draw(props) method, all others have sensible (mostly empty) defaults.

Sketch lifecycle function sequence diagram

PropertyTypeRequiredDescriptionDefault
configJSON matching type ConfigInputNoConfigure image and project properties such as canvas size.Image size: 1080x1080Color and Palette seeds: Random Seeds based on current date and timeFrameRate: 60
controlsAn array of Param objects.YesDefines the parameters that control the sketch. These are used to create the control panel for the sketch.No interactive parameters
init(props)Function providing SketchPropsYesRuns when sketch is first passed to the Canvas and is responsible for initializing the Sketch data model. This function is a good place to put expensive up front work to be done once and not repeated when new images are generated.No operations performed.
reset(props)Function providing SketchPropsNoRuns when user requests a new image to be drawn. Use this to reset any data as needed between draws.Clears the canvas back to empty (all transparent).
draw(props)Function providing SketchPropsNoThe main drawing actions of the sketch. This will be run once whenever a user requests a new image to be generated.-
loop(props, frameData)Function providing SketchProps and FrameData.NoWill be called every frame of the animation loop controlled by the page. The framerate will attempt to match the value specified in config. The loop will stop once this function returns true (indicating the animation has finished).No operations performed.

SketchProps

The SketchProps are provided provided to every function in the Sketch definition. They provide access to the Canvas, and to the seeded Random generators. The full list of properties available and links to their code or documentation is:

PropertyTypeDescription
canvas@code-not-art/core.CanvasProvides access to the canvas and 2D context directly, plus all the drawing tools provided by the code-not-art core library.
rng@code-not-art/core.RandomRandom number generator provided the image seed
palettePaletteRandom Color Palette with 5 randomly selected colors. Changing the color seed will update the colors in the palette without affecting the random seed of the rng Random generator provided in the SketchProps
paramsStringMap<any>The values for the parameters provided in the sketch definition. If these are updated in the UI then this params object will have the updated values.

Example Sketch Code

This repository provides an application to run demonstration sketchs found at /apps/demos. These are a great place to start to look at implementing an example sketch.

The following is a bare minimum sketch. It does nothing but can be passed to a SketchController and will result in no changes to the canvas:

const bareMinimumSketch = createSketch({
	init: () => ({}),
	controls: {},
});

A sketch that will modify the canvas requires a draw() or loop() function. As a minimum example, the following sketch will completely fill canvas with a single color, using the first color from the provided palette:

const simpleSketch = createSketch({
	controls: {},
	init: () => ({}),
	draw: ({ canvas, palette }) => {
		canvas.fill(palette.colors[0]);
	},
});
2.0.0-beta.2

5 months ago

2.0.0-beta.1

10 months ago

1.0.1

2 years ago

1.0.0

2 years ago

0.11.0

2 years ago

0.10.0

2 years ago

0.9.5

2 years ago

0.9.3

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.8.2

3 years ago

0.9.0

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.5.0

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.3.0

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.2.3

4 years ago

0.2.1

4 years ago

0.2.2

4 years ago

0.2.0

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago