class Pattern

Pattern object for drawing gradients and patterns.

The MGraphics object can create Pattern objects with functions like MGraphics.pattern_create_linear(), MGraphics.pattern_create_radial(), and MGraphics.pattern_create_rgba(). Generally, you use this Pattern object to add color stops and transformations to customize the pattern, then set that pattern as a source before filling a path.

Example

function paint() {
		// create a path
		mgraphics.rectangle(50, 0, 200, 100);

		// create a gradient pattern to fill the shape
		var tmp = mgraphics.pattern_create_radial(20.,20.,20.,100.,100.,30.);
		mp.add_color_stop_rgba(1.,0.,1.,0.,1.);
		tmp.add_color_stop_rgba(0.,1.,0.,0.,1.);

		// set the gradient as a source
		mgraphics.set_source(tmp);

		// fill the shape
		mgraphics.fill();
}

Methods

add_color_stop_rgba

Add a color stop to a pattern.

Sets a color stop for the pattern. Linear and radial gradients will fade continuously between these color values.

add_color_stop_rgba(index: number, red: number, green: number, blue: number, alpha: number);
NameTypeDescription
indexnumberThe index of the color stop
rednumberThe red component of the color stop
greennumberThe green component of the color stop
bluenumberThe blue component of the color stop
alphanumberThe alpha component of the color stop

get_extend

Get the extend value of the pattern

The extend value determines how the pattern will be drawn to fill extra space. "none" will draw nothing, "reflect" will invert the pattern and continue to draw it as if reflected, "repeat" will start the pattern over and continue to draw, and "pad" will take the last color of the pattern and extend it outward.

get_extend(): "none" | "reflect" | "repeat" | "pad";
NameTypeDescription
Return Value"none" | "reflect" | "repeat" | "pad"

get_type

Get the pattern type

Get the type of the pattern. Values are 0-solid, 1-surface, 2-linear, 3-gradient.

get_type(): 0 | 1 | 2 | 3;
NameTypeDescription
Return Value0 | 1 | 2 | 3

identity_matrix

Reset the transform matrix to identity (no transformation).

identity_matrix();

rotate

Adds a rotation to the pattern

rotate(angle: number);
NameTypeDescription
anglenumberthe rotation angle in radians (counter-clockwise)

scale

Scale the pattern horizontally and vertically

scale(scale_x: number, scale_y: number);
NameTypeDescription
scale_xnumberhorizontal scaling
scale_ynumbervertical scaling

set_extend

Set the extend value of the pattern

set_extend(extend_value: "none" | "reflect" | "repeat" | "pad");
NameTypeDescription
extend_value"none" | "reflect" | "repeat" | "pad"The extend value to set

set_matrix

Set the transform matrix for the pattern directly.

set_matrix(xx: number, xy: number, yx: number, yy: number, x0: number, y0: number);
NameTypeDescription
xxnumberx scaling support
xynumberrotational warping
yxnumberrotational warping
yynumbery scaling support
x0numberx translation
y0numbery translation

translate

Translate the current pattern

translate(t_x: number, t_y: number);
NameTypeDescription
t_xnumberhorizontal translation
t_ynumbervertical translation