Skip to content

Instantly share code, notes, and snippets.

@mgdm
Created July 22, 2012 11:59
Show Gist options
  • Save mgdm/3159434 to your computer and use it in GitHub Desktop.
Save mgdm/3159434 to your computer and use it in GitHub Desktop.
Mesh pattern demo
/* Mesh pattern demo - requires Cairo 1.12
* Compile with:
* gcc -I/usr/include/cairo -o mesh_pattern mesh_pattern.c -lcairo
* (or similar)
*
* Running will create 'mesh.png' in the current directory
* Michael Maclean <[email protected]>
*/
#include <cairo.h>
int main(int argc, char **argv) {
cairo_t *ctx;
cairo_surface_t *surface;
cairo_pattern_t *pattern;
/* Create a 400x300px image surface, using 24-bit colour */
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 400, 300);
/* Create a context to do the drawing with */
ctx = cairo_create(surface);
/* Create our pattern */
pattern = cairo_pattern_create_mesh();
/* Use the mesh pattern functions to create a square (called a 'patch')
* This square will be the same size as the image */
cairo_mesh_pattern_begin_patch(pattern);
cairo_mesh_pattern_line_to(pattern, 0, 0); /* Corner 0 */
cairo_mesh_pattern_line_to(pattern, 400, 0); /* Corner 1 */
cairo_mesh_pattern_line_to(pattern, 400, 300); /* Corner 2 */
cairo_mesh_pattern_line_to(pattern, 0, 300); /* Corner 3 */
cairo_mesh_pattern_line_to(pattern, 0, 0); /* Back to corner 0 */
/* Assign colours to each corner */
cairo_mesh_pattern_set_corner_color_rgb(pattern, 0, 0, 0, 0); /* Corner 0 - black */
cairo_mesh_pattern_set_corner_color_rgb(pattern, 1, 1, 0, 0); /* Corner 1 - red */
cairo_mesh_pattern_set_corner_color_rgb(pattern, 2, 0, 1, 0); /* Corner 2 - green */
cairo_mesh_pattern_set_corner_color_rgb(pattern, 3, 0, 0, 1); /* Corner 2 - blue */
/* Finish the pattern */
cairo_mesh_pattern_end_patch(pattern);
/* Set the context to use the pattern for drawing */
cairo_set_source(ctx, pattern);
/* Draw a rectange and fill it */
cairo_rectangle(ctx, 0, 0, 400, 300);
cairo_fill(ctx);
/* Write the resulting image to a PNG */
cairo_surface_write_to_png(surface, "mesh.png");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment