antiboredom / easyskia

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EasySkia

A quick, very very work-in-progress redux of p5py. Lets you draw with Skia in Python in a Processing-style way, with many functions borrowed from p5py's skia renderer. You probably shouldn't use this yet.

Goals & Non-Goals:

Goals

  • simple, p5/processing-like api for skia
  • more "pythony" than p5py, and exposes more skia stuff
  • good for exporting animation as video files, images, and pdfs suitable for printing
  • can be headless

Non-Goals

  • user interaction (mouse/keyboard/etc)
  • audio

Basic Examples

Drawing

from easyskia import Canvas
c = Canvas(width=600, height=600, show=True)
x = 0
while c.animate():
    c.background(1, 1, 1)
    c.fill(1, 0, 0)
    c.stroke(0, 1, 0)
    c.ellipse(x, 100, 50, 50)
    x = x + 1

Saving videos

from easyskia import Canvas
c = Canvas(width=600, height=600, show=False)
c.save_video("testing.mp4", frames=200)
x = 0
while c.animate():
    c.background(1, 1, 1)
    c.fill(1, 0, 0)
    c.stroke(0, 1, 0)
    c.ellipse(x, 100, 50, 50)
    x = x + 1

Exporting PDFs

from easyskia import Canvas
c = Canvas(width=600, height=600, renderer="PDF", output="testing.pdf")
c.background(1, 1, 1)
c.rect(100, 100, 120, 150)
c.add_page()
c.rect(200, 200, 90, 90)
c.save_pdf()

Docs

easyskia.canvas

Canvas Objects

class Canvas()

__init__

def __init__(width: int = DEFAULT_WIDTH,
             height: int = DEFAULT_HEIGHT,
             show: bool = False,
             renderer: str = "GPU",
             title: str = "EasySkia",
             output: Optional[str] = None)

Create a canvas

Arguments:

  • width int - width of canvas
  • height int - height of canvas
  • show bool - show the canvas
  • renderer str - renderer to use (GPU, CPU, PDF)
  • title str - title of window
  • output str - output path for PDF renderer

setup_raster

def setup_raster()

Setup a raster canvas

setup_pdf

def setup_pdf(output: str)

Setup a PDF canvas

Arguments:

  • output str - output path

setup_gl

def setup_gl()

Setup a GPU canvas

background

def background(r: float, g: float, b: float, a=1.0)

Set the background color

Arguments:

  • r float - red value
  • g float - green value
  • b float - blue value
  • a float - alpha value (default: 1.0)

clear

def clear()

Clear the canvas

fill

def fill(r: float, g: float, b: float, a: float = 1.0)

Set the fill color

Arguments:

  • r float - red value
  • g float - green value
  • b float - blue value
  • a float - alpha value (default: 1.0)

stroke

def stroke(r: float, g: float, b: float, a: float = 1.0)

Set the stroke color

Arguments:

  • r float - red value
  • g float - green value
  • b float - blue value
  • a float - alpha value (default: 1.0)

stroke_weight

def stroke_weight(w: float)

Set the stroke weight

Arguments:

  • w float - stroke weight

no_fill

def no_fill()

Disable fill

no_stroke

def no_stroke()

Disable stroke

text_font

def text_font(fontname: str)

Set the text font

Arguments:

  • fontname str - font name

text_size

def text_size(size: float)

Set the text size

Arguments:

  • size float - font size

text_style

def text_style(s: str)

Set the text style

Arguments:

  • s str - text style

line

def line(x1: float, y1: float, x2: float, y2: float)

Draw a line

Arguments:

  • x1 float - x1
  • y1 float - y1
  • x2 float - x2
  • y2 float - y2

ellipse

def ellipse(x: float, y: float, w: float, h: float)

Draw an ellipse

Arguments:

  • x float - x
  • y float - y
  • w float - width
  • h float - height

circle

def circle(x: float, y: float, d: float)

Draw a circle

Arguments:

  • x float - x
  • y float - y
  • d float - diameter

quad

def quad(x1: float, y1: float, x2: float, y2: float, x3: float, y3: float,
         x4: float, y4: float)

Draw a quad

Arguments:

  • x1 float - x1
  • y1 float - y1
  • x2 float - x2
  • y2 float - y2
  • x3 float - x3
  • y3 float - y3
  • x4 float - x4
  • y4 float - y4

rect

def rect(x: float,
         y: float,
         w: float,
         h: float,
         tl: Optional[float] = None,
         tr: Optional[float] = None,
         br: Optional[float] = None,
         bl: Optional[float] = None)

Draw a rectangle

Arguments:

  • x float - x
  • y float - y
  • w float - width
  • h float - height
  • tl float - top left corner radius
  • tr float - top right corner radius
  • br float - bottom right corner radius
  • bl float - bottom left corner radius

triangle

def triangle(x1: float, y1: float, x2: float, y2: float, x3: float, y3: float)

Draw a triangle

Arguments:

  • x1 float - x1
  • y1 float - y1
  • x2 float - x2
  • y2 float - y2
  • x3 float - x3
  • y3 float - y3

text

def text(text: str, x: float, y: float)

Draw text

Arguments:

  • text str - text to draw
  • x float - x
  • y float - y

load_font

def load_font(path: str) -> skia.Typeface

Load a font

Arguments:

  • path str - path to font file

load_image

def load_image(path: str) -> skia.Image

Load an image

Arguments:

  • path str - path to image file

image

def image(image: skia.Image,
          x: float,
          y: float,
          w: Optional[float] = None,
          h: Optional[float] = None)

Draw an image

Arguments:

  • image skia.Image - image to draw
  • x float - x
  • y float - y
  • w float - width
  • h float - height

animate

def animate()

Animate the canvas

add_page

def add_page(width: Optional[float] = None, height: Optional[float] = None)

Add a page to a PDF canvas

Arguments:

  • width float - width of page
  • height float - height of page

render

def render(rewind=True)

Render the shape/image/text etc to the canvas

push

def push()

Push the canvas state

pop

def pop()

Pop the canvas state

translate

def translate(x: float, y: float)

Translate the canvas

Arguments:

  • x float - x
  • y float - y

rotate

def rotate(deg: float)

Rotate the canvas

Arguments:

  • deg float - degrees to rotate

scale

def scale(sx: float, sy: Optional[float] = None)

Scale the canvas

Arguments:

  • sx float - x scale
  • sy float - y scale

save

def save(filename: str = "frame.png")

Save the canvas to a file

Arguments:

  • filename str - filename to save to

save_frame

def save_frame(filename: Optional[str] = None)

Save a frame. If filename is None, it will be named frame_0000000000.jpg

Arguments:

  • filename str - filename to save to

save_pdf

def save_pdf()

Save the PDF canvas

save_video

def save_video(filename: str = "sketch.mp4",
               fps: int = 60,
               max_frames: int = 0)

Save a video

Arguments:

  • filename str - filename to save to
  • fps float - frames per second
  • max_frames int - maximum number of frames to record

save_video_frame

def save_video_frame()

Save a video frame

finish_video

def finish_video()

Finish recording a video

About


Languages

Language:Python 99.7%Language:Shell 0.3%