Expand description
This crate is a rustlang port of Rough.js npm package written by @pshihn.
This package exposes functions to generate rough drawing primitives which looks like hand drawn sketches. This is the core create of operations to create rough drawings. It exposes its own primitive drawing types for lines curves, arcs, polygons, circles, ellipses and even svg paths. Works on Point2D type from euclid crate
On its own this crate can not draw on any context. One needs to use existing drawing libraries such as piet, raqote, tiny-skia etc in combination with roughr. In this workspace an example adapter is implemented for piet. Below examples are output of rough_piet adapter.
§📦 Cargo.toml
[dependencies]
roughr = "0.1"
§🔧 Example
§Rectangle
ⓘ
let options = OptionsBuilder::default()
.stroke(Srgba::from_raw(&[114u8, 87u8, 82u8, 255u8]).into_format())
.fill(Srgba::from_raw(&[254u8, 246u8, 201u8, 255u8]).into_format())
.fill_style(FillStyle::Hachure)
.fill_weight(DPI * 0.01)
.build()
.unwrap();
let generator = KurboGenerator::new(options);
let rect_width = 100.0;
let rect_height = 50.0;
let rect = generator.rectangle::<f32>(
(WIDTH as f32 - rect_width) / 2.0,
(HEIGHT as f32 - rect_height) / 2.0,
rect_width,
rect_height,
);
let background_color = Color::from_hex_str("96C0B7").unwrap();
rc.fill(
Rect::new(0.0, 0.0, WIDTH as f64, HEIGHT as f64),
&background_color,
);
rect.draw(&mut rc);
§🖨️ Output Rectangle
§Circle
ⓘ
let options = OptionsBuilder::default()
.stroke(Srgba::from_raw(&[114u8, 87u8, 82u8, 255u8]).into_format())
.fill(Srgba::from_raw(&[254u8, 246u8, 201u8, 255u8]).into_format())
.fill_style(FillStyle::Hachure)
.fill_weight(DPI * 0.01)
.build()
.unwrap();
let generator = KurboGenerator::new(options);
let circle_paths = generator.circle::<f32>(
(WIDTH as f32) / 2.0,
(HEIGHT as f32) / 2.0,
HEIGHT as f32 - 10.0f32,
);
let background_color = Color::from_hex_str("96C0B7").unwrap();
rc.fill(
Rect::new(0.0, 0.0, WIDTH as f64, HEIGHT as f64),
&background_color,
);
circle_paths.draw(&mut rc);
§🖨️ Output Circle
§Ellipse
ⓘ
let options = OptionsBuilder::default()
.stroke(Srgba::from_raw(&[114u8, 87u8, 82u8, 255u8]).into_format())
.fill(Srgba::from_raw(&[254u8, 246u8, 201u8, 255u8]).into_format())
.fill_style(FillStyle::Hachure)
.fill_weight(DPI * 0.01)
.build()
.unwrap();
let generator = KurboGenerator::new(options);
let ellipse_paths = generator.ellipse::<f32>(
(WIDTH as f32) / 2.0,
(HEIGHT as f32) / 2.0,
WIDTH as f32 - 10.0,
HEIGHT as f32 - 10.0,
);
let background_color = Color::from_hex_str("96C0B7").unwrap();
rc.fill(
Rect::new(0.0, 0.0, WIDTH as f64, HEIGHT as f64),
&background_color,
);
ellipse_paths.draw(&mut rc);
§🖨️ Output Ellipse
§Svg Path
ⓘ
let options = OptionsBuilder::default()
.stroke(Srgba::from_raw(&[114u8, 87u8, 82u8, 255u8]).into_format())
.fill(Srgba::from_raw(&[254u8, 246u8, 201u8, 255u8]).into_format())
.fill_style(FillStyle::Hachure)
.fill_weight(DPI * 0.01)
.build()
.unwrap();
let generator = KurboGenerator::new(options);
let heart_svg_path = "M140 20C73 20 20 74 20 140c0 135 136 170 228 303 88-132 229-173 229-303 0-66-54-120-120-120-48 0-90 28-109 69-19-41-60-69-108-69z".into();
let heart_svg_path_drawing = generator.path::<f32>(heart_svg_path);
let background_color = Color::from_hex_str("96C0B7").unwrap();
rc.fill(
Rect::new(0.0, 0.0, WIDTH as f64, HEIGHT as f64),
&background_color,
);
heart_svg_path_drawing.draw(&mut rc);
§🖨️ Output Svg Path
§Filler Implementation Status
- Hachure
- Zigzag
- Cross-Hatch
- Dots
- Dashed
- Zigzag-Line
§🔭 Examples
For more examples have a look at the examples folder.
Modules§
Structs§
- Angle
- Representation of the
<angle>
type. - Aspect
Ratio - Representation of the
preserveAspectRatio
attribute. - Color
- Representation of the
<color>
type. - Filter
Value List Parser - A pull-based
<filter-value-list>
parser. - FuncIRI
- Representation of the
<FuncIRI>
type. - IRI
- Representation of the
<IRI>
type. - Length
- Representation of the
<length>
type. - Length
List Parser - A pull-based
<list-of-length>
parser. - Number
- An SVG number.
- Number
List Parser - A pull-based
<list-of-numbers>
parser. - Paint
Order - Representation of the
paint-order
property. - Path
Parser - A pull-based path data parser.
- Point2D
- A 2d Point tagged with a unit.
- Points
Parser - A pull-based
<list-of-points>
parser. - Simplifying
Path Parser - A simplifying Path Data parser.
- Transform
- Representation of the
<transform>
type. - Transform
List Parser - A pull-based
<transform-list>
parser. - ViewBox
- Representation of the
<viewBox>
type.
Enums§
- Align
- Representation of the
align
value of thepreserveAspectRatio
attribute. - Angle
Unit - List of all SVG angle units.
- Enable
Background - Representation of the
enable-background
attribute. - Error
- List of all errors.
- Filter
Value - Representation of the
<filter-function>
|<url>
type. - Filter
Value List Parser Error - A list of possible
FilterValueListParser
errors. - Length
Unit - List of all SVG length units.
- Paint
- Representation of the
<paint>
type. - Paint
Fallback - Representation of the fallback part of the
<paint>
type. - Paint
Order Kind paint-order
property variants.- Path
Segment - Representation of a path segment.
- Simple
Path Segment - Representation of a simple path segment.
- Transform
List Token - Transform list token.
- View
BoxError - List of possible
ViewBox
parsing errors.
Type Aliases§
- Srgba
- Non-linear sRGB with an alpha component.