web/canvas
Drawing shapes, text and images on a browser canvas element.
import web.canvas
This package gives plain functions for two dimensional drawing on a canvas element. As with dom, the program is translated for the browser with salam js. Get the drawing context with Get and pass it to every function. The coordinates start at the top left corner and the vertical axis points down.
Functions
Get
func Get(id: str): i64import dom
import web.canvas
func main:
el := dom.Create("canvas")
dom.SetAttr(el, "id", "board")
dom.SetAttr(el, "width", "320")
dom.SetAttr(el, "height", "200")
dom.Append(dom.Body(), el)
c := canvas.Get("board")
println canvas.Width(c), canvas.Height(c)
canvas.FillStyle(c, "#f5f0e6")
canvas.FillRect(c, 0, 0, canvas.Width(c), canvas.Height(c))
canvas.FillStyle(c, "#2a7")
canvas.RoundRect(c, 20, 20, 120, 60, 12)
canvas.Fill(c)
canvas.StrokeStyle(c, "#333")
canvas.LineWidth(c, 2)
canvas.StrokeRect(c, 160, 20, 120, 60)
canvas.ClearRect(c, 170, 30, 20, 20)
canvas.BeginPath(c)
canvas.MoveTo(c, 20, 180)
canvas.LineTo(c, 100, 100)
canvas.LineTo(c, 180, 180)
canvas.ClosePath(c)
canvas.Stroke(c)
canvas.BeginPath(c)
canvas.Arc(c, 250, 140, 30, 0, 6.2832)
canvas.Alpha(c, 0.5)
canvas.Fill(c)
canvas.Alpha(c, 1)
canvas.Save(c)
canvas.Translate(c, 160, 100)
canvas.Rotate(c, 0.2)
canvas.Scale(c, 1.5, 1.5)
canvas.Font(c, "16px Inter")
canvas.TextAlign(c, "center")
canvas.FillStyle(c, "#000")
canvas.FillText(c, "hello", 0, 0)
canvas.Restore(c)
image := dom.LoadImage("logo.png")
if dom.ImageReady(image):
canvas.DrawImage(c, image, 260, 10, 50, 50)
end
second := canvas.Of(dom.Create("canvas"))
println second != 0
endOf
func Of(el: i64): i64The example for this item is shown with the ones above: web-canvas-Draw.salam
Width
func Width(c: i64): f64The example for this item is shown with the ones above: web-canvas-Draw.salam
Height
func Height(c: i64): f64The example for this item is shown with the ones above: web-canvas-Draw.salam
FillStyle
func FillStyle(c: i64, v: str)#2a7 or red.The example for this item is shown with the ones above: web-canvas-Draw.salam
StrokeStyle
func StrokeStyle(c: i64, v: str)The example for this item is shown with the ones above: web-canvas-Draw.salam
LineWidth
func LineWidth(c: i64, v: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
Font
func Font(c: i64, v: str)16px Inter.The example for this item is shown with the ones above: web-canvas-Draw.salam
TextAlign
func TextAlign(c: i64, v: str)left, center or right.The example for this item is shown with the ones above: web-canvas-Draw.salam
Alpha
func Alpha(c: i64, v: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
FillRect
func FillRect(c: i64, x: f64, y: f64, w: f64, h: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
StrokeRect
func StrokeRect(c: i64, x: f64, y: f64, w: f64, h: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
ClearRect
func ClearRect(c: i64, x: f64, y: f64, w: f64, h: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
RoundRect
func RoundRect(c: i64, x: f64, y: f64, w: f64, h: f64, r: f64)Fill or Stroke afterwards.The example for this item is shown with the ones above: web-canvas-Draw.salam
BeginPath
func BeginPath(c: i64)The example for this item is shown with the ones above: web-canvas-Draw.salam
ClosePath
func ClosePath(c: i64)The example for this item is shown with the ones above: web-canvas-Draw.salam
MoveTo
func MoveTo(c: i64, x: f64, y: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
LineTo
func LineTo(c: i64, x: f64, y: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
Arc
func Arc(c: i64, x: f64, y: f64, r: f64, a: f64, b: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
Fill
func Fill(c: i64)The example for this item is shown with the ones above: web-canvas-Draw.salam
Stroke
func Stroke(c: i64)The example for this item is shown with the ones above: web-canvas-Draw.salam
FillText
func FillText(c: i64, t: str, x: f64, y: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
Save
func Save(c: i64)The example for this item is shown with the ones above: web-canvas-Draw.salam
Restore
func Restore(c: i64)The example for this item is shown with the ones above: web-canvas-Draw.salam
Translate
func Translate(c: i64, x: f64, y: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
Rotate
func Rotate(c: i64, a: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
Scale
func Scale(c: i64, x: f64, y: f64)The example for this item is shown with the ones above: web-canvas-Draw.salam
DrawImage
func DrawImage(c: i64, img: i64, x: f64, y: f64, w: f64, h: f64)LoadImage into this rectangle.The example for this item is shown with the ones above: web-canvas-Draw.salam