Crate core_animation

Crate core_animation 

Source
Expand description

Rust bindings for macOS Core Animation with ergonomic builder APIs.

§Builders

BuilderPurpose
WindowBuilderLayer-backed windows with background, border, transparency
CALayerBuilderBase layers with bounds, position, sublayers
CAShapeLayerBuilderVector shapes with path, fill, stroke, shadows
CATextLayerBuilderText rendering layers
CAEmitterLayerBuilderParticle systems with closure-based cell configuration
PointBurstBuilderConvenience API for radial particle bursts
CABasicAnimationBuilderStandalone GPU-accelerated animations

§Quick Start

use core_animation::prelude::*;

let window = WindowBuilder::new()
    .title("Demo")
    .size(400.0, 400.0)
    .centered()
    .background_color(Color::rgb(0.1, 0.1, 0.15))
    .build();

let circle = CAShapeLayerBuilder::new()
    .circle(80.0)
    .position(CGPoint::new(200.0, 200.0))
    .fill_color(Color::CYAN)
    .animate("pulse", KeyPath::TransformScale, |a| {
        a.values(0.85, 1.15)
            .duration(1.seconds())
            .easing(Easing::InOut)
            .autoreverses()
            .repeat(Repeat::Forever)
    })
    .build();

window.container().add_sublayer(&circle);
window.show_for(10.seconds());

§Animations

All layer builders support .animate() for GPU-accelerated animations:

.animate("name", KeyPath::TransformScale, |a| {
    a.values(0.8, 1.2)              // from/to values
        .duration(500.millis())      // timing
        .easing(Easing::InOut)       // curve
        .autoreverses()              // ping-pong
        .repeat(Repeat::Forever)     // loop
        .phase_offset(0.5)           // stagger multiple animations
})

Animatable properties: TransformScale, TransformRotation, Opacity, ShadowRadius, ShadowOpacity, Custom

Easing curves: Linear, In, Out, InOut

§Particle Systems

use std::f64::consts::PI;

let emitter = CAEmitterLayerBuilder::new()
    .position(320.0, 240.0)
    .shape(EmitterShape::Point)
    .particle(|p| {
        p.birth_rate(100.0)
            .lifetime(5.0)
            .velocity(80.0)
            .emission_range(PI * 2.0)
            .color(Color::CYAN)
            .image(ParticleImage::soft_glow(64))
    })
    .build();

Or use the convenience builder:

let burst = PointBurstBuilder::new(320.0, 240.0)
    .velocity(100.0)
    .color(Color::PINK)
    .build();

Particle images: soft_glow, circle, star, spark

§Examples

See the examples for runnable demos with screenshots.

cargo run --example window_builder

Use prelude to import common types.

Re-exports§

pub use window::Screen;
pub use window::Window;
pub use window::WindowBuilder;
pub use window::WindowLevel;
pub use window::WindowStyle;
pub use objc2_core_foundation;
pub use objc2_core_graphics;
pub use objc2_core_text;
pub use objc2_quartz_core;

Modules§

animation_builder
GPU-accelerated animations using CABasicAnimation.
particles
Particle emitter builders.
prelude
Prelude module for convenient imports.
window
Test window for running examples.

Structs§

CALayer
The base layer class. *
CALayerBuilder
Builder for CALayer.
CAShapeLayer
Apple’s documentation
CAShapeLayerBuilder
Builder for CAShapeLayer.
CATextLayer
Apple’s documentation
CATextLayerBuilder
Builder for CATextLayer.
CATransform3D
Apple’s documentation
Color
RGBA color (components 0.0–1.0).

Enums§

TextAlign
Text alignment modes for CATextLayer.
Truncation
Truncation modes for CATextLayer.

Traits§

CALayerExt
Extension trait providing snake_case methods for CALayer.
DurationExt
Extension trait for creating Duration from numeric values.