PiDraw is a script designed to create and generate geometrical illustration. It is based above svg.js to display the graphics. PiDraw comes also with a parser, which is a "text to graphical illustration" application.
This application is made for me and I'm glad to share it :)
Made with Love and LOTS of coffee :)
The Draw class is the main class. It is simply created with:
const draw = new Draw(element
:
<id | HTMLElement>, config ? : IParserConfig
)config is an object describing the display and the constructions
interface IParserConfig {
parameters?: string,
code?: string
tex?: TeXConverterType,
}parameters is a string code, defined by <key1>[=<value1>],<key1>[=<value1>],.... For example, a complete
parameters string could be
parameters = "axis,grid,ppu=80,x=-5:10,y=-2:9,subgrid=2"| key | value | description |
|---|---|---|
| axis | null |
if axis is given, the axis are displayed |
| grid | null |
if grid is given, the grids are displayed |
| subgrid | <number> |
number of subdivisions of the grid |
| x | <number>:<number> |
gives the x-axis min and max value, separated by : |
| y | <number>:<number> |
gives the y-axis min and max value, separated by : |
| ppu | <number> |
points per unit - used to defined the scale of the viewbox |
code describes the geometric figures to be created. Each line of the string is a figure.
A(x,y)create a point at coordinatesxandyA(x,y)->dragcreates a point at coordinatesxandyand make it draggable.
d=ABcreates an infinite line through the pointsAandB. These points must be created befored=AB.creates a segment from the pointAtoB.d=vABcreates a vector from the pointAtoB.d=AB[creates a ray from the pointAthroughB.
f(x)=x^2-4x+2orf=plot x^2-4x+2creates a plot using the natural math language (asciiMath).
Graph is the root class to display the illustration. Even if it's possible to use it directly, this class is designed to be used in conjunction with the Draw class.
const graph = new Graph(element
:
id | HTMLElement, config
:
IGraphConstructorConfig
)config is an object describing the global configuration of the illustration:
interface IGraphConstructorConfig {
width?: number,
height?: number,
origin?: { x: number, y: number },
ppu?: number,
system?: cartesian_2d | polar,
axis?: {
x: { x: number, y: number }
y: { x: number, y: number }
},
display?: {
axis?: boolean | {
x: boolean | number | IAxisConfig,
y: boolean | number | IAxisConfig
},
grid?: boolean,
subgrid?: number,
},
tex?: (value: string) => string
}| key | value | default | description |
|---|---|---|---|
| width | number |
800 |
width of the svg viewbox in pixels |
| height | number |
600 |
width of the svg viewbox in pixels |
| origin | {x:number,y:number} |
{x: 400, y: 300} |
origin of the axis, from the top left of the viewbox. |
| ppu | number |
50 |
points per unit. With the default width at 800, the x-axis will have 800/50=16 units |
| system | string |
"cartesian_2d" |
type of coordinate system. Allowed values: cartesian_2d or polar |
| axis | {x: {x:number,y:number}, y:{x:number,y:number}} |
{x: {x: 50, y: 0}, y: {x: 0, y:-50}} |
x and y axis. Each axis is defined by it's vector in pixels, with the y-axis inverted |
| display | {axis: boolean, grid: boolean, subgrid: number } |
{grid: true, subgrid: 0, axis: true} |
grid: hide or show the subgrid: set a subgrid, defined by the number of subdivision (0=no subgrid, 1=one subdivision) axis: show or hide the grid (TODO: more possibilities...) |
| tex | (value:string)=>string |
(value)=>value |
the TeX converter (I'm using katex) |