Skip to content

Instantly share code, notes, and snippets.

@souzagab
Created June 25, 2020 13:47
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 souzagab/8ea87ad7bfbdf809621d836daf86dc05 to your computer and use it in GitHub Desktop.
Save souzagab/8ea87ad7bfbdf809621d836daf86dc05 to your computer and use it in GitHub Desktop.
The grid code I use to help measure and position elements, using prawn
require "prawn"
module Prawn
module Graphics
def stroke_grid(options = {})
options = {
:at => [0, 0],
:height => bounds.height.to_i - (options[:at] || [0, 0])[1],
:width => bounds.width.to_i - (options[:at] || [0, 0])[0],
:step_length => 50,
:negative_axes_length => 0,
:color => "000000"
}.merge(options)
Prawn.verify_options([:at, :width, :height, :step_length,
:negative_axes_length, :color], options)
save_graphics_state do
fill_color(options[:color])
stroke_color(options[:color])
stroke_bounds
fill_circle(options[:at], 1)
(options[:step_length]..options[:width]).step(options[:step_length]) do |point|
fill_circle([options[:at][0] + point, options[:at][1]], 1)
draw_text(point, :at => [options[:at][0] + point - 5, options[:at][1] + 10], :size => 7)
end
(options[:step_length]..options[:height]).step(options[:step_length]) do |point|
fill_circle([options[:at][0], options[:at][1] + point], 1)
draw_text(point, :at => [options[:at][0] + 10, options[:at][1] + point - 2], :size => 7)
end
self.line_width = 0.5
(0..options[:height]).step(5) do |point|
tp = point % 50 == 0 ? 0.5 : 0.1
transparent(tp) do
stroke_horizontal_line(options[:at][0],
options[:at][0] + options[:width], :at => options[:at][1] + point)
end
end
(0..options[:width]).step(5) do |point|
tp = point % 50 == 0 ? 0.5 : 0.1
transparent(tp) do
stroke_vertical_line(options[:at][1],
options[:at][1] + options[:height], :at => options[:at][0] + point)
end
end
end
end
end
end
Prawn::Document.generate("grid.pdf") do
# Remove canvas to draw grid within current bounds
canvas do
stroke_grid
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment