Skip to content

Instantly share code, notes, and snippets.

@mRB0
Created August 22, 2020 13:29
Show Gist options
  • Save mRB0/6ee40d5a865d7ffe2311d2807dd23b09 to your computer and use it in GitHub Desktop.
Save mRB0/6ee40d5a865d7ffe2311d2807dd23b09 to your computer and use it in GitHub Desktop.
extern crate cairo;
use std::fs::File;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let surface = cairo::ImageSurface::create(cairo::Format::ARgb32, 640, 480)?;
let context = cairo::Context::new(&surface); // Borrows `surface` (immutable!)
// Use the context to modify the surface
context.set_source_rgb(1.0, 0.0, 0.0);
context.new_path();
context.move_to(100.0, 100.0);
context.line_to(100.0, 200.0);
context.stroke();
context.set_source_rgb(0.8, 0.4, 1.0);
context.new_path();
context.move_to(200.0, 100.0);
context.line_to(200.0, 300.0);
context.stroke();
// Export the surface (nb. still borrowed by context)
let mut file = File::create("foo.png")?;
surface.write_to_png(&mut file)?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment