Download     Browse Source  

Introduction:

PyCanvas is a simple canvas implemented in pure python. It can output VML or SVG data for the 2D primitives drawn on canvas and can be easily extended to output more formats.

Sample :

A sample is listed below which draws various 2D shapes and outputs as VML and SVG, rendering of these files is also displayed below.

   
import sys
import os

# set where PyCanavs is located ( I do not install it :)
sys.path.append("/home/anurag/test/my")

from pycanvas.renderer import renderers
from pycanvas.Canvas import Canvas
from pycanvas import Pen
from pycanvas import Brush

# create a canavs of logical size 1000x1000
canvas = Canvas("Test", 1000, 1000)

#draw a line with default pen
canvas.drawLine(0, 500, 1000, 500)

# change default pen
pen = Pen.Pen(2,(255,0,0))
canvas.setPen(pen)

canvas.drawLine(500, 0, 500, 1000)

pen.width = 4
pen.color = (0,255,0)
canvas.drawLine(0, 0, 1000, 1000)

# change pen's style
pen.dashStyle = Pen.DashStyle.DASH
canvas.drawLine(0, 1000, 1000, 0)

pen.color = (255,255,0)
pen.dashStyle = Pen.DashStyle.SOLID

pen.color = (0,0,255)
pen.opacity = .25
pen.width = 10
canvas.drawRect(350, 350, 300, 300, 0.25)

pen.width = 2
pen.color = (255,0,255)
pen.dashStyle = Pen.DashStyle.DOT
canvas.drawRect(400, 400, 200, 200)

pen.dashStyle = Pen.DashStyle.SOLID
canvas.setBrush(Brush.Brush((255,255,100)))
canvas.drawPolyline([ (8,65),(72,65), (92,11), (112,65), (174,65),
(122,100), (142,155), (92,121), (42,155), (60,100), (8,65)])

pen.color = (0,255,255)
pen.dashStyle = Pen.DashStyle.SOLID
canvas.setBrush(Brush.Brush((100,120,150)))
canvas.drawOval(700, 400, 200)
canvas.drawOval(850, 450, 200, 100)

canvas.drawText(200, 100, "Test PyCanvas")

# output VML to a file
f = open("vml.html", "w")
f.write("<html><body>\n")
vml = canvas.render(500,500, renderers.VMLRenderer)
f.write(vml)
f.write("</body></html>\n")
f.close()

# output SVG to a file
f = open("test.svg", "w")
svg = canvas.render(500,500, renderers.SVGRenderer)
f.write(svg)
f.close()


Output:
VML as rendered by IE7
VML as rendered by IE7 
SVG as rendered by Mozilla
SVG as rendered by Mozilla