| Crates.io | line_tools |
| lib.rs | line_tools |
| version | 0.2.0 |
| created_at | 2025-08-09 18:41:04.375749+00 |
| updated_at | 2025-08-09 18:41:04.375749+00 |
| description | High-performance line rendering for WebGL applications with anti-aliasing and batch processing |
| homepage | https://github.com/Wandalen/cgtools/tree/master/module/helper/line_tools |
| repository | https://github.com/Wandalen/cgtools |
| max_upload_size | |
| id | 1788141 |
| size | 106,651 |
High-performance line rendering for WebGL applications
A specialized library for rendering lines, curves, and vector graphics in WebGL. Optimized for real-time rendering with support for various line styles, anti-aliasing, and efficient batch processing. Perfect for data visualization, CAD applications, and interactive graphics.
Add to your Cargo.toml:
line_tools = { workspace = true }
use line_tools::*;
use minwebgl as gl;
use ndarray_cg::*;
fn render_lines(gl: &gl::WebGl2RenderingContext) -> Result<(), Box<dyn std::error::Error>> {
// Create line renderer
let mut renderer = LineRenderer::new(gl)?;
// Define line points
let points = vec![
[0.0, 0.0], // Start point
[1.0, 0.5], // Control point
[2.0, 0.0], // End point
];
// Render line with styling
renderer.draw_line(&points)
.width(2.0)
.color([1.0, 0.0, 0.0, 1.0]) // Red color
.antialias(true)
.render(gl)?;
Ok(())
}
use line_tools::*;
fn styled_lines(renderer: &mut LineRenderer, gl: &WebGl2RenderingContext) -> Result<(), Box<dyn std::error::Error>> {
// Dashed line
let line_points = vec![[0.0, 0.0], [1.0, 1.0]];
renderer.draw_line(&line_points)
.width(3.0)
.dash_pattern(&[10.0, 5.0, 2.0, 5.0])
.color([0.0, 1.0, 0.0, 1.0])
.render(gl)?;
// Gradient line
let curve_points = vec![[0.0, 0.0], [0.5, 1.0], [1.0, 0.0]];
renderer.draw_line(&curve_points)
.width(4.0)
.gradient(
[1.0, 0.0, 0.0, 1.0], // Start color (red)
[0.0, 0.0, 1.0, 1.0] // End color (blue)
)
.line_cap(LineCap::Round)
.render(gl)?;
// Variable width line
let points = vec![[0.0, 0.0], [0.25, 0.5], [0.5, 0.0], [0.75, 0.5], [1.0, 0.0]];
let widths = vec![1.0, 3.0, 2.0, 4.0, 1.0];
renderer.draw_line_variable_width(&points, &widths)
.color([1.0, 1.0, 0.0, 1.0])
.render(gl)?;
Ok(())
}
| Type | Description | Use Case |
|---|---|---|
LineRenderer |
Main rendering engine | All line drawing operations |
LineStyle |
Styling configuration | Colors, widths, patterns |
LineCap |
Line ending styles | Round, square, butt |
LineJoin |
Connection styles | Round, bevel, miter |
| Method | Purpose | Example |
|---|---|---|
draw_line() |
Render basic line | renderer.draw_line(&points) |
draw_curve() |
Render smooth curve | renderer.draw_curve(&control_points) |
draw_polyline() |
Connected line segments | renderer.draw_polyline(&vertices) |
draw_batch() |
Multiple lines efficiently | renderer.draw_batch(&line_data) |
renderer.draw_line(&points)
.width(2.5) // Line thickness
.color([r, g, b, a]) // RGBA color
.dash_pattern(&[dash, gap]) // Dash pattern
.line_cap(LineCap::Round) // Line endings
.line_join(LineJoin::Miter) // Connections
.antialias(true) // Smooth edges
.render(gl)?;
// Efficient: batch multiple lines
let line1 = vec![[0.0, 0.0], [1.0, 0.0]];
let line2 = vec![[0.0, 1.0], [1.0, 1.0]];
let line3 = vec![[0.0, 2.0], [1.0, 2.0]];
let lines = vec![line1, line2, line3];
renderer.draw_batch(&lines).render(gl)?;
// Less efficient: individual calls
for line in lines {
renderer.draw_line(&line).render(gl)?;
}
LineRenderer instances across framesIntegration with ndarray_cg for complex transformations:
use ndarray_cg::*;
use std::f64::consts::PI;
let transform = mat3x3::scale([2.0, 1.5]) * mat3x3::rot(PI / 4.0);
let points = vec![[0.0, 0.0], [1.0, 1.0]];
renderer.set_transform(&transform);
renderer.draw_line(&points).render(gl)?;
Extend functionality with custom GLSL shaders:
let vertex_shader_source = "#version 300 es\n...";
let fragment_shader_source = "#version 300 es\n...";
let custom_shader = renderer.create_custom_shader(
vertex_shader_source,
fragment_shader_source
)?;
renderer.use_shader(&custom_shader);
Add this to your Cargo.toml:
[dependencies]
line_tools = "0.1.0"
For detailed API documentation and examples, run:
cargo doc --open
This crate works seamlessly with other cgtools modules:
Perfect for building comprehensive graphics applications with the cgtools ecosystem.