### Install Dependencies via Poetry or Pip Source: https://github.com/subroy13/handanim/blob/master/README.md Provides commands to set up the project environment using either Poetry or standard pip requirements files. ```bash # Poetry installation poetry install poetry run python examples/pythagoras.py # Pip installation python -m venv venv source venv/bin/activate pip install -r requirements.txt python examples/pythagoras.py ``` -------------------------------- ### Install System Dependencies (Cairo and FFmpeg) Source: https://github.com/subroy13/handanim/blob/master/README.md Instructions for installing the required native Cairo graphics library and FFmpeg video encoder on Ubuntu, macOS, and Windows. ```bash # Ubuntu/Debian sudo apt install libcairo2 libcairo2-dev ffmpeg # macOS (Homebrew) brew install cairo ffmpeg # Verify FFmpeg ffmpeg -version ``` -------------------------------- ### Create and Animate Math Expressions using Python Source: https://context7.com/subroy13/handanim/llms.txt Shows how to render mathematical expressions using TeX notation. This includes examples of the quadratic formula and the Pythagorean theorem, with options for positioning, font size, stroke style, and custom fonts. The expressions are animated and rendered to a video. ```python from handanim.core import Scene, StrokeStyle from handanim.primitives import Math from handanim.animations import SketchAnimation from handanim.stylings.color import BLUE, BLACK scene = Scene(width=1200, height=800) # Quadratic formula quadratic = Math( tex_expression=r"$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$", position=(600, 200), font_size=64, stroke_style=StrokeStyle(color=BLUE, width=2), font_name="feasibly" # Use specific font ) # Pythagorean theorem pythagoras = Math( tex_expression=r"$a^2 + b^2 = c^2$", position=(600, 400), font_size=96, stroke_style=StrokeStyle(color=BLACK, width=2) ) # Note: The original text did not include scene.add() or scene.render() for the Math examples. # Assuming they would follow a similar pattern to other examples if rendering was intended. # scene.add(SketchAnimation(start_time=0, duration=3), drawable=quadratic) # scene.add(SketchAnimation(start_time=3, duration=2), drawable=pythagoras) # scene.render("math_example.mp4", max_length=6) ``` -------------------------------- ### Create a Polygon Drawable with Styles Source: https://github.com/subroy13/handanim/blob/master/src/handanim_ai/prompts/codegen.txt Instantiates a Polygon drawable with specified points, stroke style, sketch style, and fill style. This example demonstrates how to define the geometry and visual appearance of a shape using handanim's styling objects. ```python right_triangle = Polygon( points=[ (500, 500), (500, 700), (900, 700), ], stroke_style=StrokeStyle(color=BLACK, width=2), sketch_style=SketchStyle(roughness=5), fill_style=FillStyle(color=RED, hachure_gap=10), ) ``` -------------------------------- ### Create and Animate a Line in Python Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates how to create a simple line with specified start and end points and a stroke style. The line is then added to a scene with a sketch animation and rendered to a video file. Requires the handanim library. ```python from handanim.core import Scene, StrokeStyle from handanim.primitives import Line from handanim.animations import SketchAnimation from handanim.stylings.color import BLACK # Define the scene scene = Scene(width=800, height=600) # Create a simple line line = Line( start=(100, 100), # Starting point (x, y) end=(400, 300), # Ending point (x, y) stroke_style=StrokeStyle(color=BLACK, width=2) ) # Add to scene with sketch animation scene.add( event=SketchAnimation(start_time=0, duration=2), drawable=line ) # Render the animation to a video file scene.render("line_example.mp4", max_length=3) ``` -------------------------------- ### Create Filled Polygons in Python Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates how to create filled polygons with customizable stroke, sketch, and fill styles. The Polygon primitive requires at least three vertices. The example shows a right triangle with fill. Requires the handanim library. ```python from handanim.core import Scene, StrokeStyle, SketchStyle, FillStyle from handanim.primitives import Polygon from handanim.animations import SketchAnimation from handanim.stylings.color import BLACK, RED scene = Scene(width=800, height=600) # Right triangle with fill triangle = Polygon( points=[ (200, 200), (200, 400), (500, 400) ], stroke_style=StrokeStyle(color=BLACK, width=2), sketch_style=SketchStyle(roughness=3), fill_style=FillStyle(color=RED, hachure_gap=10, hachure_angle=45) ) scene.add( event=SketchAnimation(start_time=0, duration=3), drawable=triangle ) scene.render("polygon_example.mp4", max_length=4) ``` -------------------------------- ### Create Line Primitive in handanim Source: https://context7.com/subroy13/handanim/llms.txt Shows the basic setup for creating a Line primitive in handanim, which draws a hand-drawn style line between two points. It requires a Scene object and StrokeStyle for appearance. ```python from handanim.core import Scene, StrokeStyle from handanim.primitives import Line from handanim.animations import SketchAnimation from handanim.stylings.color import BLACK scene = Scene(width=800, height=600) ``` -------------------------------- ### TranslateTo/TranslateFromAnimation: Move Elements Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates TranslateToAnimation for moving elements to a target point and TranslateFromAnimation for moving them from a starting point back to their original position. This is useful for creating movement effects. ```python from handanim.core import Scene, StrokeStyle from handanim.primitives import Circle from handanim.animations import SketchAnimation, TranslateToAnimation, TranslateFromAnimation from handanim.stylings.color import BLACK, BLUE scene = Scene(width=800, height=600) # Circle that moves to a point circle = Circle( center=(200, 300), radius=50, stroke_style=StrokeStyle(color=BLACK, width=2) ) scene.add(SketchAnimation(start_time=0, duration=1), drawable=circle) scene.add( TranslateToAnimation( start_time=1.5, duration=2, data={"point": (600, 300)} # Target destination ), drawable=circle ) # Circle that moves from a point circle2 = Circle( center=(600, 500), radius=50, stroke_style=StrokeStyle(color=BLUE, width=2) ) scene.add( TranslateFromAnimation( start_time=4, duration=2, data={"point": (100, 100)} # Starting position ), drawable=circle2 ) scene.render("translate_example.mp4", max_length=7) ``` -------------------------------- ### Create Complex Animations with Math and Erasers Source: https://context7.com/subroy13/handanim/llms.txt A comprehensive example showing how to combine text, polygons, mathematical expressions, and eraser effects. It utilizes DrawableGroup to manage multiple elements and sequences them using SketchAnimation. ```python from handanim.core import Scene, SketchStyle, StrokeStyle, FillStyle, DrawableGroup from handanim.primitives import Text, Eraser, Polygon, Math from handanim.stylings.color import BLUE, RED, BLACK, ERASER_HINT_COLOR from handanim.animations import SketchAnimation scene = Scene(width=1920, height=1088) title = Text(text="Pythagoras' Theorem", position=(300, 500), font_size=192) scene.add(SketchAnimation(duration=3), drawable=title) eraser = Eraser(objects_to_erase=[title], drawable_cache=scene.drawable_cache) scene.add(SketchAnimation(start_time=3.5, duration=1.5), drawable=eraser) formula = Math(tex_expression=r"$a^2 + b^2 = c^2$", position=(900, 300), font_size=128) scene.add(SketchAnimation(start_time=10, duration=3), drawable=formula) scene.render("pythagoras_theorem.mp4", max_length=15) ``` -------------------------------- ### Add Sketch Animation to Scene Source: https://github.com/subroy13/handanim/blob/master/src/handanim_ai/prompts/codegen.txt Applies a SketchAnimation to a drawable object within the scene. The animation is configured with a start time and duration, and added to the scene using the `add` method, which links the animation event to the specified drawable. ```python scene.add(event=SketchAnimation(start_time=6, duration=3), drawable=right_triangle) ``` -------------------------------- ### Compose Multiple Animations with CompositeAnimationEvent Source: https://github.com/subroy13/handanim/blob/master/src/handanim_ai/prompts/codegen.txt Combines multiple animation events into a single `CompositeAnimationEvent`. This allows for sequential or parallel execution of animations on a drawable or group of drawables, managed by specifying start times and durations for each event. ```python scene.add( CompositeAnimationEvent( events=[ SketchAnimation(start_time=10.5, duration=1), ZoomOutAnimation(start_time=11.5, duration=2), TranslateToAnimation( start_time=11.5, duration=2, data={"point": a_sq_label.position} ), ] ), drawable=label_group, ) ``` -------------------------------- ### Create and Render Animations with Python Source: https://github.com/subroy13/handanim/blob/master/README.md Demonstrates how to initialize a scene, define a geometric primitive (NGon), apply a sketch animation, and render the final output as an MP4 video file. ```python from handanim.core import Scene from handanim.animations import SketchAnimation from handanim.primitives import NGon scene = Scene(width = 800, height = 608) triangle = NGon( center = (400, 304), radius = 100, n = 3 ) scene.add(SketchAnimation(start_time = 0, end_time = 5), drawable = triangle) scene.render("triangle_anim.mp4", fps = 30) ``` -------------------------------- ### Create and Render Scene in handanim Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates how to create a Scene object with custom dimensions, frame rate, and background color. It also shows how to render the scene as an MP4 video, a GIF, and a single SVG snapshot. ```python from handanim.core import Scene # Create a scene with custom dimensions and settings scene = Scene( width=1920, # Width in pixels height=1088, # Height in pixels fps=30, # Frames per second background_color=(1, 1, 1) # White background (RGB 0-1 range) ) # Access viewport bounds for positioning x_min, x_max, y_min, y_max = scene.get_viewport_bounds() print(f"Viewport: x({x_min}, {x_max}), y({y_min}, {y_max})") # Render as video file scene.render("output.mp4", max_length=10) # 10 seconds # Render as GIF scene.render("output.gif", max_length=10) # Render a single frame snapshot as SVG scene.render_snapshot("frame.svg", frame_in_seconds=5.0, max_length=10) ``` -------------------------------- ### Configure FillStyle in handanim Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates how to set up FillStyle for closed shapes, including basic color and opacity, as well as hachure (hatching) patterns with customizable angles, gaps, and line weights. Supports cross-hatching. ```python from handanim.core import FillStyle from handanim.stylings.color import RED, BLUE, ORANGE # Basic hachure fill hachure_fill = FillStyle( color=RED, # Fill color opacity=1.0, # Fill opacity fill_pattern="hachure", # Hatching pattern hachure_angle=45, # Angle of hatch lines in degrees hachure_gap=10, # Gap between hatch lines hachure_line_width=1, # Width of hatch lines fill_weight=2 # Overall fill weight ) # Cross-hatched style with different angle cross_fill = FillStyle( color=BLUE, hachure_angle=135, hachure_gap=8 ) ``` -------------------------------- ### Create and Animate Text using Python Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates rendering text with a handwritten style, including options for positioning, font size, stroke, sketch style, and a glow effect for emphasis. The text elements are animated and saved as a video. ```python from handanim.core import Scene, StrokeStyle, SketchStyle from handanim.primitives import Text from handanim.animations import SketchAnimation from handanim.stylings.color import BLUE, BLACK scene = Scene(width=800, height=600) # Title text with glow effect title = Text( text="Hello World!", position=(400, 200), # Center position font_size=96, stroke_style=StrokeStyle(color=BLUE, width=2), sketch_style=SketchStyle(roughness=1), glow_dot_hint={"color": BLUE, "radius": 5} # Glowing dot at drawing point ) # Smaller text subtitle = Text( text="handanim demo", position=(400, 400), font_size=48, stroke_style=StrokeStyle(color=BLACK, width=1) ) scene.add(SketchAnimation(start_time=0, duration=3), drawable=title) scene.add(SketchAnimation(start_time=3, duration=2), drawable=subtitle) scene.render("text_example.mp4", max_length=6) ``` -------------------------------- ### Create Open and Closed Paths in Python Source: https://context7.com/subroy13/handanim/llms.txt Shows how to create both open and closed paths using the LinearPath primitive. Paths are defined by a list of points and a 'close' parameter. Animations and rendering are also demonstrated. Requires the handanim library. ```python from handanim.core import Scene, StrokeStyle from handanim.primitives import LinearPath from handanim.animations import SketchAnimation from handanim.stylings.color import BLUE scene = Scene(width=800, height=600) # Open path through multiple points open_path = LinearPath( points=[(100, 100), (200, 50), (300, 100), (400, 50)], close=False, stroke_style=StrokeStyle(color=BLUE, width=2) ) # Closed path (triangle) closed_path = LinearPath( points=[(500, 100), (600, 200), (400, 200)], close=True, stroke_style=StrokeStyle(color=BLUE, width=2) ) scene.add(SketchAnimation(start_time=0, duration=2), drawable=open_path) scene.add(SketchAnimation(start_time=2, duration=2), drawable=closed_path) scene.render("path_example.mp4", max_length=5) ``` -------------------------------- ### Configure SketchStyle in handanim Source: https://context7.com/subroy13/handanim/llms.txt Illustrates how to customize the hand-drawn appearance of primitives using SketchStyle. Parameters like roughness, bowing, and curve fitting control the level of imperfection and sketchiness. ```python from handanim.core import SketchStyle # Default smooth sketch style smooth_sketch = SketchStyle( roughness=1, # Level of imperfection (0 = smooth) bowing=1, # Curvature/waviness of lines max_random_offset=2, # Maximum random jitter curve_tightness=0, # How tight curves follow points curve_fitting=0.95, # Curve approximation accuracy curve_step_count=9, # Steps for curve rendering disable_multi_stroke=False, # Enable double-pass for sketchy effect disable_font_mixture=True # Use consistent font ) # Rough hand-drawn style rough_sketch = SketchStyle( roughness=5, bowing=2, max_random_offset=4 ) ``` -------------------------------- ### Create and Animate Ellipse and Circle using Python Source: https://context7.com/subroy13/handanim/llms.txt Shows how to create and animate an ellipse with custom fill and sketch styles, and a circle (a special case of an ellipse) with a fill style. Both are added to a scene with animations and rendered to a video. ```python from handanim.core import Scene, StrokeStyle, FillStyle, SketchStyle from handanim.primitives import Ellipse, Circle from handanim.animations import SketchAnimation from handanim.stylings.color import BLACK, GREEN, SKY_BLUE scene = Scene(width=800, height=600) # Ellipse with fill ellipse = Ellipse( center=(200, 300), width=200, height=100, stroke_style=StrokeStyle(color=BLACK, width=2), fill_style=FillStyle(color=GREEN, hachure_gap=8), sketch_style=SketchStyle(roughness=2) ) # Circle circle = Circle( center=(500, 300), radius=80, stroke_style=StrokeStyle(color=BLACK, width=2), fill_style=FillStyle(color=SKY_BLUE, hachure_gap=10) ) scene.add(SketchAnimation(start_time=0, duration=2), drawable=ellipse) scene.add(SketchAnimation(start_time=2, duration=2), drawable=circle) scene.render("ellipse_example.mp4", max_length=5) ``` -------------------------------- ### Drawable Transformations: Translate and Scale in Python Source: https://context7.com/subroy13/handanim/llms.txt Illustrates how to apply translate and scale transformations to Drawable objects, returning new TransformedDrawable instances. This requires importing core, primitive, animation, and styling modules. The input is an original drawable object, and the output includes transformed copies with modified properties. ```python from handanim.core import Scene, StrokeStyle from handanim.primitives import Rectangle from handanim.animations import SketchAnimation from handanim.stylings.color import BLACK, BLUE, RED scene = Scene(width=800, height=600) # Original rectangle original = Rectangle( top_left=(100, 200), width=100, height=60, stroke_style=StrokeStyle(color=BLACK, width=2) ) # Translated copy translated = original.translate(offset_x=200, offset_y=0) translated.stroke_style = StrokeStyle(color=BLUE, width=2) # Scaled copy scaled = original.scale(scale_x=1.5, scale_y=1.5) scaled.stroke_style = StrokeStyle(color=RED, width=2) ``` -------------------------------- ### Initialize Handanim Scene Source: https://github.com/subroy13/handanim/blob/master/src/handanim_ai/prompts/codegen.txt Creates a new Scene object, which serves as the canvas for all animations. The Scene constructor accepts width and height parameters to define the viewport dimensions for the animation. ```python scene = Scene(width=1920, height=1088) # blank scene (viewport = 1777, 1000) ``` -------------------------------- ### Create N-sided Polygons (NGon) in Python Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates the creation of regular n-sided polygons, such as triangles and pentagons, using the NGon primitive. Defined by a center point, radius, and the number of sides (n). Includes stroke and fill styles. Requires the handanim library. ```python from handanim.core import Scene, StrokeStyle, FillStyle from handanim.primitives import NGon from handanim.animations import SketchAnimation from handanim.stylings.color import BLACK, PURPLE, TEAL scene = Scene(width=800, height=600) # Equilateral triangle (3-gon) triangle = NGon( center=(150, 300), radius=80, n=3, stroke_style=StrokeStyle(color=BLACK, width=2), fill_style=FillStyle(color=PURPLE, hachure_gap=10) ) # Pentagon (5-gon) pentagon = NGon( center=(350, 300), radius=80, n=5, stroke_style=StrokeStyle(color=BLACK, width=2), fill_style=FillStyle(color=TEAL, hachure_gap=8) ) # Note: The original snippet was incomplete, missing scene.add and scene.render calls for NGon. # Example of adding and rendering: # scene.add(SketchAnimation(start_time=0, duration=2), drawable=triangle) # scene.add(SketchAnimation(start_time=2, duration=2), drawable=pentagon) # scene.render("ngon_example.mp4", max_length=5) ``` -------------------------------- ### Create and Animate Arrows using Python Source: https://context7.com/subroy13/handanim/llms.txt Illustrates the creation of both straight arrows with different head types and sizes, and curved arrows defined by a series of points. These are animated and rendered into a video file. ```python from handanim.core import Scene, StrokeStyle from handanim.primitives import Arrow, CurvedArrow from handanim.animations import SketchAnimation from handanim.stylings.color import BLACK, RED, BLUE scene = Scene(width=800, height=600) # Simple arrow arrow1 = Arrow( start_point=(100, 200), end_point=(300, 200), arrow_head_type="->", # Single arrow head arrow_head_size=15, arrow_head_angle=45, stroke_style=StrokeStyle(color=BLACK, width=2) ) # Double arrow head arrow2 = Arrow( start_point=(100, 300), end_point=(300, 300), arrow_head_type=">->", # Double arrow head arrow_head_size=15, stroke_style=StrokeStyle(color=RED, width=2) ) # Curved arrow through control points curved = CurvedArrow( points=[(400, 400), (500, 200), (600, 300), (700, 200)], arrow_head_type="->", arrow_head_size=12, stroke_style=StrokeStyle(color=BLUE, width=2) ) scene.add(SketchAnimation(start_time=0, duration=1.5), drawable=arrow1) scene.add(SketchAnimation(start_time=1.5, duration=1.5), drawable=arrow2) scene.add(SketchAnimation(start_time=3, duration=2), drawable=curved) scene.render("arrow_example.mp4", max_length=6) ``` -------------------------------- ### ZoomIn/ZoomOutAnimation: Scale Elements Source: https://context7.com/subroy13/handanim/llms.txt Shows how to use ZoomInAnimation to scale elements from zero to full size and ZoomOutAnimation to scale them from full size to zero. This creates dramatic entrance and exit effects. ```python from handanim.core import Scene, StrokeStyle, FillStyle from handanim.primitives import Circle from handanim.animations import SketchAnimation, ZoomInAnimation, ZoomOutAnimation from handanim.stylings.color import BLACK, GREEN scene = Scene(width=800, height=600) # Create circle circle = Circle( center=(400, 300), radius=80, stroke_style=StrokeStyle(color=BLACK, width=2), fill_style=FillStyle(color=GREEN, hachure_gap=8) ) # Sketch first, then zoom out scene.add(SketchAnimation(start_time=0, duration=2), drawable=circle) scene.add(ZoomOutAnimation(start_time=3, duration=2), drawable=circle) # Create another circle and zoom it in circle2 = Circle( center=(400, 300), radius=120, stroke_style=StrokeStyle(color=BLACK, width=2) ) scene.add(ZoomInAnimation(start_time=5, duration=2), drawable=circle2) scene.render("zoom_example.mp4", max_length=8) ``` -------------------------------- ### Render Handanim Scene to Video Source: https://github.com/subroy13/handanim/blob/master/src/handanim_ai/prompts/codegen.txt Renders the entire scene into a video file. The `render` method takes the output path and the maximum length of the video in seconds as arguments. It processes all added drawables and animations to generate the final output. ```python scene.render(output_path, max_length=15) # save the scenes in 15 seconds of video ``` -------------------------------- ### Draw Rectangles and Squares in Python Source: https://context7.com/subroy13/handanim/llms.txt Shows how to create rectangles and squares using convenience classes. These primitives are defined by their top-left corner, dimensions, and can include stroke, fill, and sketch styles. Requires the handanim library. ```python from handanim.core import Scene, StrokeStyle, FillStyle, SketchStyle from handanim.primitives import Rectangle, Square from handanim.animations import SketchAnimation from handanim.stylings.color import BLACK, BLUE, ORANGE scene = Scene(width=800, height=600) # Rectangle with fill rect = Rectangle( top_left=(100, 150), width=200, height=100, stroke_style=StrokeStyle(color=BLACK, width=2), fill_style=FillStyle(color=BLUE, hachure_gap=8), sketch_style=SketchStyle(roughness=2) ) # Square square = Square( top_left=(400, 150), side_length=150, stroke_style=StrokeStyle(color=BLACK, width=2), fill_style=FillStyle(color=ORANGE, hachure_gap=10) ) scene.add(SketchAnimation(start_time=0, duration=2), drawable=rect) scene.add(SketchAnimation(start_time=2, duration=2), drawable=square) scene.render("rectangle_example.mp4", max_length=5) ``` -------------------------------- ### Create and Animate Hexagon using Python Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates the creation of a hexagon (6-sided polygon) with specified center, radius, and stroke style. It then animates its appearance on a scene and renders it to a video file. ```python from handanim.core import Scene, StrokeStyle from handanim.primitives import NGon from handanim.animations import SketchAnimation from handanim.stylings.color import BLACK # Assuming 'scene' is already initialized and 'triangle' and 'pentagon' are defined elsewhere # scene = Scene(width=800, height=600) hexagon = NGon( center=(550, 300), radius=80, n=6, stroke_style=StrokeStyle(color=BLACK, width=2) ) # scene.add(SketchAnimation(start_time=0, duration=2), drawable=triangle) # scene.add(SketchAnimation(start_time=2, duration=2), drawable=pentagon) scene.add(SketchAnimation(start_time=4, duration=2), drawable=hexagon) scene.render("ngon_example.mp4", max_length=7) ``` -------------------------------- ### Perform Geometric Transformations and Scene Rendering Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates how to apply transformations like rotation to drawable objects and sequence them within a scene. The scene is then rendered to an MP4 file using the render method. ```python rotated = original.rotate(angle=45) scene.add(SketchAnimation(start_time=0, duration=1), drawable=original) scene.add(SketchAnimation(start_time=1, duration=1), drawable=translated) scene.add(SketchAnimation(start_time=2, duration=1), drawable=scaled) scene.add(SketchAnimation(start_time=3, duration=1), drawable=rotated) scene.render("transform_example.mp4", max_length=5) ``` -------------------------------- ### Import Handanim Core and Animation Objects Source: https://github.com/subroy13/handanim/blob/master/src/handanim_ai/prompts/codegen.txt Imports necessary classes from the handanim library for scene management, styling, drawables, and animations. This includes core components like Scene, SketchStyle, and specific animation classes like SketchAnimation and FadeOutAnimation, as well as primitive shapes. ```python from handanim.core import Scene, SketchStyle, StrokeStyle, FillStyle, DrawableGroup, CompositeAnimationEvent from handanim.animations import SketchAnimation, FadeOutAnimation from handanim.primitives import Math, Eraser, Square, Line, Rectangle from handanim.stylings.color import BLUE, RED, BLACK, ERASER_HINT_COLOR, ORANGE ``` -------------------------------- ### Configure StrokeStyle in handanim Source: https://context7.com/subroy13/handanim/llms.txt Shows how to define StrokeStyle for drawing lines, including basic color, width, and opacity, as well as advanced pressure variation for a more dynamic look. Supports predefined colors like BLUE, RED, and BLACK. ```python from handanim.core import StrokeStyle from handanim.core.styles import StrokePressure from handanim.stylings.color import BLUE, RED, BLACK # Basic stroke style stroke = StrokeStyle( color=BLUE, # RGB tuple (0, 0, 1) width=2, # Line width in pixels opacity=1.0 # Fully opaque ) # Stroke with pressure variation pressure_stroke = StrokeStyle( color=RED, width=3, opacity=0.8, stroke_pressure=StrokePressure.PROPORTIONAL # Varies width based on position ) # Available pressure modes: # StrokePressure.CONSTANT - uniform width # StrokePressure.PROPORTIONAL - increases with distance # StrokePressure.INVERSE - decreases with distance ``` -------------------------------- ### DrawableGroup: Parallel and Series Animation in Python Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates how to use DrawableGroup to animate multiple elements simultaneously ('parallel') or sequentially ('series'). It requires importing core, primitive, animation, and styling modules from handanim. The input is a list of drawable elements and a grouping method, with the output being a grouped drawable object ready for scene addition. ```python from handanim.core import Scene, StrokeStyle, DrawableGroup from handanim.primitives import Circle, Text from handanim.animations import SketchAnimation, ZoomOutAnimation, TranslateToAnimation from handanim.stylings.color import BLACK, RED, BLUE scene = Scene(width=800, height=600) # Create multiple elements shapes = [ Circle(center=(150, 300), radius=40, stroke_style=StrokeStyle(color=RED, width=2)), Circle(center=(300, 300), radius=40, stroke_style=StrokeStyle(color=BLUE, width=2)), Circle(center=(450, 300), radius=40, stroke_style=StrokeStyle(color=BLACK, width=2)) ] # Parallel group - all animate together parallel_group = DrawableGroup( elements=shapes, grouping_method="parallel" ) scene.add(SketchAnimation(start_time=0, duration=3), drawable=parallel_group) # Series group - animate one after another labels = [ Text(text="A", position=(200, 500), font_size=48, stroke_style=StrokeStyle(color=BLACK)), Text(text="B", position=(350, 500), font_size=48, stroke_style=StrokeStyle(color=BLACK)), Text(text="C", position=(500, 500), font_size=48, stroke_style=StrokeStyle(color=BLACK)) ] series_group = DrawableGroup( elements=labels, grouping_method="series" # Each element animates sequentially ) scene.add(SketchAnimation(start_time=3, duration=3), drawable=series_group) scene.render("group_example.mp4", max_length=7) ``` -------------------------------- ### Create Math Expression Animation Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates how to create and animate a mathematical expression using Einstein's famous equation. It utilizes the Math primitive and SketchAnimation for drawing. ```python from handanim.core import Scene, StrokeStyle from handanim.primitives import Math from handanim.animations import SketchAnimation from handanim.stylings.color import BLUE scene = Scene(width=800, height=600) einstein = Math( tex_expression=r"$E = mc^2$", position=(600, 600), font_size=128, stroke_style=StrokeStyle(color=BLUE, width=3), glow_dot_hint={"color": BLUE, "radius": 5} ) scene.add(SketchAnimation(start_time=0, duration=3), drawable=quadratic) scene.add(SketchAnimation(start_time=3, duration=2), drawable=pythagoras) scene.add(SketchAnimation(start_time=5, duration=2), drawable=einstein) scene.render("math_example.mp4", max_length=8) ``` -------------------------------- ### Apply Transformations to Drawables Source: https://github.com/subroy13/handanim/blob/master/src/handanim_ai/prompts/codegen.txt Demonstrates how to apply transformations such as translation, rotation, and scaling to an existing drawable object. These methods modify the drawable's position, orientation, or size. ```python drawable1 = ... drawable1.translate(dx, dy) drawable1.rotate(angle_in_deg) drawable1.scale(sx, sy) ``` -------------------------------- ### Configure Viewport for Mathematical Coordinates Source: https://context7.com/subroy13/handanim/llms.txt Configures a custom viewport to map world coordinates to screen pixels. This allows drawing primitives like circles and lines using a mathematical coordinate system rather than raw pixel values. ```python from handanim.core import Scene from handanim.core.viewport import Viewport from handanim.primitives import Circle, Line from handanim.animations import SketchAnimation from handanim.core import StrokeStyle from handanim.stylings.color import BLACK, BLUE scene = Scene(width=800, height=600, fps=30) scene.viewport = Viewport( world_xrange=(-10, 10), world_yrange=(-10, 10), screen_width=800, screen_height=600, margin=50 ) origin_circle = Circle( center=(0, 0), radius=1, stroke_style=StrokeStyle(color=BLUE, width=2) ) scene.add(SketchAnimation(start_time=2, duration=1), drawable=origin_circle) scene.render("viewport_example.mp4", max_length=4) ``` -------------------------------- ### Draw Smooth Curves in Python Source: https://context7.com/subroy13/handanim/llms.txt Illustrates the creation of smooth, hand-drawn style curves using the Curve primitive. The curve is defined by a series of control points and can be customized with stroke and sketch styles. Requires the handanim library. ```python from handanim.core import Scene, StrokeStyle, SketchStyle from handanim.primitives import Curve from handanim.animations import SketchAnimation from handanim.stylings.color import RED scene = Scene(width=800, height=600) # Smooth curve through points curve = Curve( points=[ (100, 300), (200, 100), (400, 400), (600, 200), (700, 300) ], stroke_style=StrokeStyle(color=RED, width=2), sketch_style=SketchStyle(roughness=2, curve_tightness=0) ) scene.add(SketchAnimation(start_time=0, duration=3), drawable=curve) scene.render("curve_example.mp4", max_length=4) ``` -------------------------------- ### CompositeAnimationEvent: Sequential Animations in Python Source: https://context7.com/subroy13/handanim/llms.txt Shows how to combine multiple animation events into a single sequence for a drawable object using CompositeAnimationEvent. This requires importing core, primitive, animation, and styling modules. The input is a list of animation events, and the output is a composite event that can be applied to a drawable in a scene. ```python from handanim.core import Scene, StrokeStyle, CompositeAnimationEvent from handanim.primitives import Math from handanim.animations import ( SketchAnimation, FadeOutAnimation, ZoomOutAnimation, TranslateToAnimation ) from handanim.stylings.color import BLUE scene = Scene(width=1200, height=800) # Create math expression formula = Math( tex_expression=r"$(a + b)^2$", position=(300, 400), font_size=128, stroke_style=StrokeStyle(color=BLUE, width=2), glow_dot_hint={"color": BLUE, "radius": 5} ) # Apply multiple animations in sequence scene.add( event=CompositeAnimationEvent( events=[ SketchAnimation(start_time=0, duration=2), # Draw first ZoomOutAnimation(start_time=3, duration=1.5), # Then shrink TranslateToAnimation( # While moving start_time=3, duration=1.5, data={"point": (800, 200)} ) ] ), drawable=formula ) # Final result appears after transformation result = Math( tex_expression=r"$= a^2 + 2ab + b^2$", position=(600, 500), font_size=96, stroke_style=StrokeStyle(color=BLUE, width=2) ) scene.add(SketchAnimation(start_time=5, duration=2), drawable=result) scene.render("composite_example.mp4", max_length=8) ``` -------------------------------- ### Create a Drawable Group Source: https://github.com/subroy13/handanim/blob/master/src/handanim_ai/prompts/codegen.txt Groups multiple drawable elements into a single `DrawableGroup`. This allows for applying transformations or animations collectively to all elements within the group. ```python label_group = DrawableGroup( elements=[ # list of drawable elements ] ) ``` -------------------------------- ### FadeIn/FadeOutAnimation: Control Element Visibility Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates FadeInAnimation to make elements appear and FadeOutAnimation to make them disappear. This is useful for managing element visibility over time in animations. ```python from handanim.core import Scene, StrokeStyle from handanim.primitives import Text from handanim.animations import SketchAnimation, FadeInAnimation, FadeOutAnimation from handanim.stylings.color import BLUE scene = Scene(width=800, height=600) text = Text( text="Fade Demo", position=(400, 300), font_size=96, stroke_style=StrokeStyle(color=BLUE, width=2) ) # First sketch the text, then fade it out scene.add(SketchAnimation(start_time=0, duration=2), drawable=text) scene.add(FadeOutAnimation(start_time=3, duration=2), drawable=text) # Fade in another element text2 = Text( text="New Text", position=(400, 300), font_size=96, stroke_style=StrokeStyle(color=BLUE, width=2) ) scene.add(FadeInAnimation(start_time=5, duration=2), drawable=text2) scene.render("fade_example.mp4", max_length=8) ``` -------------------------------- ### SketchAnimation: Draw Elements with Hand Effect Source: https://context7.com/subroy13/handanim/llms.txt Illustrates the use of SketchAnimation to draw various shapes like circles with a hand-drawing effect. It allows for customization of stroke, fill, and glow effects, along with timing controls for animation. ```python from handanim.core import Scene, StrokeStyle, FillStyle from handanim.primitives import Circle from handanim.animations import SketchAnimation from handanim.stylings.color import BLUE, RED scene = Scene(width=800, height=600) # Circle with fill and wait before fill circle = Circle( center=(400, 300), radius=100, stroke_style=StrokeStyle(color=BLUE, width=2), fill_style=FillStyle(color=RED, hachure_gap=10), glow_dot_hint={"color": BLUE, "radius": 8, "frequency": 5} ) # Animation with timing control scene.add( event=SketchAnimation( start_time=0, # Start immediately duration=4, # Total animation duration data={"wait_before_fill": 0.5} # Wait before filling ), drawable=circle ) scene.render("sketch_animation.mp4", max_length=5) ``` -------------------------------- ### Eraser: Whiteboard Eraser Effect in Python Source: https://context7.com/subroy13/handanim/llms.txt Demonstrates the Eraser class to create a whiteboard eraser effect, removing specified drawables by drawing over them with the background color. This requires importing core, primitive, animation, and styling modules. The input includes the objects to erase and a scene's drawable cache, producing an eraser animation. ```python from handanim.core import Scene, StrokeStyle from handanim.primitives import Text, Eraser from handanim.animations import SketchAnimation from handanim.stylings.color import BLUE, ERASER_HINT_COLOR scene = Scene(width=800, height=600) # Create text to erase title = Text( text="Erase Me!", position=(400, 300), font_size=96, stroke_style=StrokeStyle(color=BLUE, width=2), glow_dot_hint={"color": BLUE, "radius": 5} ) # Draw the text first scene.add(SketchAnimation(start_time=0, duration=2), drawable=title) # Create eraser for the text eraser = Eraser( objects_to_erase=[title], drawable_cache=scene.drawable_cache, glow_dot_hint={"color": ERASER_HINT_COLOR, "radius": 10} ) # Erase after a pause scene.add(SketchAnimation(start_time=3, duration=2), drawable=eraser) scene.render("eraser_example.mp4", max_length=6) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.