SkCanvas canvas; canvas.translate(50, 50); // Move to (50, 50) canvas.save(); // Save the current state canvas.rotate(30); // Rotate by 30 degrees canvas.drawRect({0, 0, 50, 50}, {255, 0, 0}); // Draw a red rectangle canvas.restore(); // Restore the saved state canvas.drawRect({0, 0, 50, 50}, {0, 0, 255}); // Draw a blue rectangle
cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 200, 200); cairo_t* cr = cairo_create(surface); cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 1.0); cairo_paint(cr); // Fill whole surface with red cairo_save(cr); // Save the current state cairo_translate(cr, 50, 50); // Move to (50, 50) cairo_rotate(cr, M_PI / 4); // Rotate by 45 degrees cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0); cairo_rectangle(cr, 0, 0, 50, 50); // Draw a green rectangle cairo_fill(cr); cairo_restore(cr); // Restore the saved state cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0); cairo_rectangle(cr, 0, 0, 50, 50); // Draw a blue rectangle cairo_fill(cr); cairo_surface_write_to_png(surface, "output.png"); // Save image to file cairo_destroy(cr); cairo_surface_destroy(surface);In this example, a red surface is created with cairo_image_surface_create and filled using cairo_paint. A green rectangle is then drawn by translating and rotating the context before restoring the saved state to draw a blue rectangle. Finally, the image is saved to a file with cairo_surface_write_to_png. In both examples, the push_transform function is used to temporarily apply a new transformation matrix to the current context in order to draw elements with a different position or orientation without affecting the previous transformations or subsequent elements.