import{ fill, rect, circle /* … */}from"codetoy/canvas"// or: import * as canvas from "codetoy/canvas"
// Canvas is a global static class; no using needed.
Canvas.Fill(255,0,0);
from codetoy.canvas import fill, rect, circle # ...# or: import codetoy.canvas as canvas
All coordinates and sizes are in pixels, with (0, 0) at the top-left and Y pointing down. Colors use 0–255 for red, green and blue and 0.0–1.0 for alpha. New to drawing in Codetoy? Start with the Drawing guide.
push()Canvas.Push()push() saves the current drawing state (fill and stroke colors, line settings and transform). pop()Canvas.Pop()pop() restores the most recently pushed state. Calls nest, so every push()Canvas.Push()push() needs a matching pop()Canvas.Pop()pop().
push():voidpop():void
Canvas.Push()
Canvas.Pop()
push()->None
pop()->None
Example
push()translate(100,100)rotate(Math.PI/4)fill(255,0,0)rect(-20,-20,40,40)pop()// transform and fill color are back to what they were
Canvas.Push();
Canvas.Translate(100,100);
Canvas.Rotate(Math.PI /4);
Canvas.Fill(255,0,0);
Canvas.Rect(-20,-20,40,40);
Canvas.Pop();// transform and fill color are back to what they were
push()
translate(100,100)
rotate(math.pi /4)
fill(255,0,0)
rect(-20,-20,40,40)
pop()# transform and fill color are back to what they were
Turn off filling or outlining for the shapes that follow. Setting a color again with fill()Canvas.Fill()fill()/fillStyle()Canvas.FillStyle()fillStyle() or stroke()Canvas.Stroke()stroke()/strokeStyle()Canvas.StrokeStyle()strokeStyle() turns it back on.
Shapes are filled with the fill color and outlined with the stroke color, unless noFill()Canvas.NoFill()noFill() or noStroke()Canvas.NoStroke()noStroke() is on.
Draws a closed shape through any number of points, passed as one flat list: [x1, y1, x2, y2, …].
polygon(points: f64[]):void// a pentagonconst pts =newArray<f64>()for(let i =0; i <5; i++){const a =(i as f64)/5.0*2.0* Math.PI- Math.PI/2
pts.push(200+ Math.cos(a)*60)
pts.push(200+ Math.sin(a)*60)}polygon(pts)
Canvas.Polygon(double[] points)// a pentagonvar pts =newdouble[10];for(int i =0; i <5; i++){double a = i /5.0*2* Math.PI - Math.PI /2;
pts[i *2]=200+ Math.Cos(a)*60;
pts[i *2+1]=200+ Math.Sin(a)*60;}
Canvas.Polygon(pts);
polygon(points:list[float])->None# a pentagonimport math
pts =[]for i inrange(5):
a = i /5*2* math.pi - math.pi /2
pts +=[200+ math.cos(a)*60,200+ math.sin(a)*60]
polygon(pts)
Draws part of a circle centered at (x, y), from startAngle to endAngle in radians. 0 points right and angles increase clockwise. The ends are joined with a straight line, so a filled arc is a circular segment (not a pie slice).
Transforms change the coordinate system for everything drawn after them. They stack, and push()Canvas.Push()push()/pop()Canvas.Pop()pop() save and restore them.
Paths let you draw any outline: straight segments and curves. Start with beginPath()Canvas.BeginPath()beginPath(), add segments, then draw with fillPath()Canvas.FillPath()fillPath() and/or strokePath()Canvas.StrokePath()strokePath(). Paths use the current fill and stroke colors and ignore noFill()Canvas.NoFill()noFill()/noStroke()Canvas.NoStroke()noStroke().
Sets the font for text()Canvas.Text()text() and measureText()Canvas.MeasureText()measureText(). family is any CSS font family available in the browser ("Arial", "monospace", "Georgia" …), size is in pixels, and weight is a CSS weight like "bold" or "600".