Skip to content

Instantly share code, notes, and snippets.

@Moelf
Created June 26, 2022 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Moelf/f68af0015f96f98264194f05c544771d to your computer and use it in GitHub Desktop.
Save Moelf/f68af0015f96f98264194f05c544771d to your computer and use it in GitHub Desktop.
Cairo.jl hatching
using Cairo, LinearAlgebra
c = CairoRGBSurface(256,256);
cr = CairoContext(c);
save(cr);
set_source_rgb(cr,0.8,0.8,0.8); # light gray
rectangle(cr,0.0,0.0,256.0,256.0); # background
fill(cr);
restore(cr);
save(cr);
b = Cairo.Colors.ARGB32(Cairo.Colors.colorant"black")
w = Cairo.Colors.ARGB32(Cairo.Colors.colorant"white")
N = 10
image = fill(w, N, N)
for i = 1:N
image[N+1-i, i] = b
end
image = CairoImageSurface(image)
w = N
h = N
pattern = CairoPattern(image);
pattern_set_extend(pattern, Cairo.EXTEND_REPEAT);
translate(cr, 128.0, 128.0);
scale(cr, 1 / sqrt(2), 1 / sqrt(2));
translate(cr, -128.0, -128.0);
S = 20.0
m = CairoMatrix(w/256.0 * S,0,0,h/256.0 * S,0,0);
set_matrix(pattern, m);
set_source(cr, pattern);
rectangle(cr, 0, 0, 256.0, 256.0);
fill(cr);
## mark picture with current date
restore(cr);
move_to(cr,0.0,12.0);
set_source_rgb(cr, 0,0,0);
show_text(cr,Libc.strftime(time()));
write_to_png(c,"sample_imagepattern.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment