### OpenGL Backend Example Source: https://github.com/tdewolff/canvas/blob/master/README.md An example demonstrating the usage of the OpenGL backend for rendering. This showcases hardware-accelerated graphics capabilities. ```Go package main import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" ) func main() { // Example usage for the OpenGL backend // This would involve initializing an OpenGL context and rendering with canvas. // Specific code omitted for brevity, but would use canvas and renderers packages. _ = canvas.NewPath() _ = renderers.NewOpenGL() } ``` -------------------------------- ### Load and Run WASM Module with Canvas Source: https://github.com/tdewolff/canvas/blob/master/examples/html-canvas/index.html This snippet shows how to get a 2D rendering context from an HTML canvas, display a loading message, and then instantiate and run a WebAssembly module using the Go runtime. It assumes a 'lib.wasm' file is available. ```javascript const ctx = document.getElementById("canvas").getContext("2d"); ctx.font = "20px Arial"; ctx.fillText("Loading WASM...", 20, 40); const go = new Go(); WebAssembly.instantiateStreaming(fetch("lib.wasm"), go.importObject).then((result) => { go.run(result.instance); }); ``` -------------------------------- ### Example: PDF Generation with Context Source: https://github.com/tdewolff/canvas/wiki/Canvas-&-Context Illustrates a complete example of creating a PDF renderer, setting up a context, defining styles, constructing a path, filling and stroking it, and drawing an image. ```go import ( "image" "os" "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers/pdf" ) func main() { f, err := os.Create("output.pdf") if err != nil { panic(err) } defer f.Close() // Create a PDF renderer pdfRenderer := pdf.New(f, 100, 100) defer pdfRenderer.Close() // Setup a context around the renderer ctx := canvas.NewContext(pdfRenderer) // Set style ctx.SetFillColor(canvas.Lightblue) ctx.SetStrokeColor(canvas.Blue) ctx.SetStrokeWidth(0.5) ctx.SetDashes(0.0, 2.0, 1.0) // dash of 2mm and gap of 1mm, starting at offset 0mm in the path // Construct path ctx.MoveTo(10, 50) ctx.LineTo(30, 80) ctx.QuadTo(60, 80, 90, 50) ctx.Close() // Fill and stroke the path ctx.FillStroke() // Draw an image (assuming 'img' is a valid image.Image) // ctx.DrawImage(30, 10, img, canvas.DPI(96.0)) } ``` -------------------------------- ### Create Project with Canvas Source: https://github.com/tdewolff/canvas/wiki/Getting-Started Steps to create a new Go project, initialize go modules, and get the canvas library. ```bash mkdir project cd project go mod init go get -u github.com/tdewolff/canvas ``` -------------------------------- ### Gio Backend Example Source: https://github.com/tdewolff/canvas/blob/master/README.md An example showcasing the integration and usage of the Gio backend for rendering. Gio is a portable immediate mode GUI library. ```Go package main import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" ) func main() { // Example usage for the Gio backend // This would involve setting up a Gio application and using canvas for drawing. // Specific code omitted for brevity, but would use canvas and renderers packages. _ = canvas.NewPath() _ = renderers.NewGio() } ``` -------------------------------- ### Basic Canvas Drawing Example Source: https://github.com/tdewolff/canvas/wiki/Getting-Started A Go program demonstrating how to create a canvas, draw a triangle, and save it as a PNG file. ```go package main import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" ) func main() { // Create new canvas of dimension 100x100 mm c := canvas.New(100, 100) // Create a canvas context used to keep drawing state ctx := canvas.NewContext(c) // Create a triangle path from an SVG path and draw it to the canvas triangle, err := canvas.ParseSVGPath("L60 0L30 60z") if err != nil { panic(err) } ctx.SetFillColor(canvas.Mediumseagreen) ctx.DrawPath(20, 20, triangle) // Rasterize the canvas and write to a PNG file with 3.2 dots-per-mm (320x320 px) if err := renderers.Write("getting-started.png", c, canvas.DPMM(3.2)); err != nil { panic(err) } } ``` -------------------------------- ### HTMLCanvas Backend Example Source: https://github.com/tdewolff/canvas/blob/master/README.md An example using the HTMLCanvas backend, which allows rendering directly to an HTML5 canvas element. A live demo is available. ```Go package main import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" ) func main() { // Example usage for the HTMLCanvas backend // This would involve rendering to an HTML canvas, typically in a web browser. // Specific code omitted for brevity, but would use canvas and renderers packages. _ = canvas.NewPath() _ = renderers.NewHTMLCanvas() } ``` -------------------------------- ### gonum/plot Library Example Source: https://github.com/tdewolff/canvas/blob/master/README.md An example demonstrating the integration with the gonum/plot library. This showcases advanced plotting and data visualization features. ```Go package main import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" "gonum.org/v1/plot" "gonum.org/v1/plot/vg" ) func main() { // Example usage with gonum/plot // This would involve creating plots using gonum/plot and rendering them with canvas. // Specific code omitted for brevity, but would use canvas, renderers, and gonum/plot packages. _ = canvas.NewPath() _ = renderers.NewPDF() _ = plot.New() _ = vg.Length(0) } ``` -------------------------------- ### Text Document Example Source: https://github.com/tdewolff/canvas/blob/master/README.md Demonstrates the creation of a text document using the PDF backend. This example showcases text rendering and document generation. ```Go package main import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" ) func main() { // Example usage for a text document // This would involve creating text elements and rendering them to a PDF. // Specific code omitted for brevity, but would use canvas and renderers packages. _ = canvas.NewPath() _ = renderers.NewPDF() } ``` -------------------------------- ### go-chart Library Example Source: https://github.com/tdewolff/canvas/blob/master/README.md An example demonstrating the integration with the go-chart library to plot a financial graph. This showcases charting capabilities. ```Go package main import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" "github.com/wcharczuk/go-chart" ) func main() { // Example usage with go-chart // This would involve creating charts using go-chart and rendering them with canvas. // Specific code omitted for brevity, but would use canvas, renderers, and go-chart packages. _ = canvas.NewPath() _ = renderers.NewPDF() _ = go_chart.Chart{} } ``` -------------------------------- ### Fyne Backend Example Source: https://github.com/tdewolff/canvas/blob/master/README.md Demonstrates the usage of the Fyne backend for rendering. Fyne is a cross-platform GUI toolkit. ```Go package main import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" ) func main() { // Example usage for the Fyne backend // This would involve creating a Fyne application and using canvas for drawing. // Specific code omitted for brevity, but would use canvas and renderers packages. _ = canvas.NewPath() _ = renderers.NewFyne() } ``` -------------------------------- ### Install and Run go-fuzz for Canvas Source: https://github.com/tdewolff/canvas/blob/master/tests/README.md Installs go-fuzz and its build tool, then builds and runs the fuzz tests for the canvas project's font tests. It also provides a guideline for checking test stability. ```shell GO111MODULE=off go get -u github.com/dvyukov/go-fuzz/go-fuzz github.com/dvyukov/go-fuzz/go-fuzz-build cd $GOPATH/github.com/tdewolff/canvas/tests/font go-fuzz-build go-fuzz ``` -------------------------------- ### Go Build Tag Usage Example Source: https://github.com/tdewolff/canvas/wiki/Build-Tags Demonstrates how to use the `-tags` command line flag with Go commands to enable specific build tags for the Canvas project. This allows for the selection of optional CGO bindings or enhanced functionality. ```go go install -tags harfbuzz,fribidi # go build -tags harfbuzz,fribidi go run -tags harfbuzz,fribidi main.go ``` -------------------------------- ### Mauna-Loa CO2 Concentration Example Source: https://github.com/tdewolff/canvas/blob/master/README.md Visualizes carbon dioxide concentrations over time from the Mauna-Loa observatory. This example highlights time-series data plotting. ```Go package main import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" ) func main() { // Example usage for Mauna-Loa CO2 concentration // This would involve fetching CO2 data and plotting it over time. // Specific code omitted for brevity, but would use canvas and renderers packages. _ = canvas.NewPath() _ = renderers.NewPDF() } ``` -------------------------------- ### Amsterdam City Centre Example Source: https://github.com/tdewolff/canvas/blob/master/README.md Renders the city centre of Amsterdam using data fetched from the Open Street Map API. This example demonstrates data loading and visualization capabilities. ```Go package main import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" ) func main() { // Example usage for Amsterdam city centre // This would involve fetching data from OpenStreetMap API and rendering it. // Specific code omitted for brevity, but would use canvas and renderers packages. _ = canvas.NewPath() _ = renderers.NewPDF() } ``` -------------------------------- ### TeX/PGF (TikZ) Backend Example Source: https://github.com/tdewolff/canvas/blob/master/README.md Shows how to use the PGF (TikZ) LaTeX package as a renderer to generate a PDF via LaTeX. This enables high-quality vector graphics output. ```Go package main import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" ) func main() { // Example usage for TeX/PGF (TikZ) backend // This would involve generating TikZ code and compiling it with LaTeX. // Specific code omitted for brevity, but would use canvas and renderers packages. _ = canvas.NewPath() _ = renderers.NewPGF() } ``` -------------------------------- ### Canvas Initialization and Rendering Source: https://github.com/tdewolff/canvas/wiki/Canvas-&-Context Demonstrates creating a new canvas, setting its Z-index, fitting it to content, and writing to multiple renderers like PNG, SVG, and PDF with custom options. ```go import ( "github.com/tdewolff/canvas" "github.com/tdewolff/canvas/renderers" ) // Create a new canvas with width and height in millimeters c := canvas.New(width, height) // ... drawing operations // Set the z-index of the drawing operations to change the drawing order c.SetZIndex(-10) // ... drawing operations // Change the canvas' size to fit all drawn objects with a 5 millimeter margin c.Fit(5.0) // Write to multiple renderers: a PNG renderer at 96 DPI and an SVG renderer renderers.Write("output.png", c, canvas.DPI(96.0)) renderers.Write("output.svg", c) // Or to specify additional renderer settings renderers.Write("output.pdf", c, &pdf.Options{ Compress: false, }) ``` -------------------------------- ### Run Basic Template Source: https://github.com/tdewolff/canvas/wiki/Getting-Started Command to execute the Go program and generate the output PNG file. ```bash go run main.go ``` -------------------------------- ### Create Colors Source: https://github.com/tdewolff/canvas/wiki/Colors-&-Gradients Demonstrates creating colors using RGBA and Hex values in the canvas library. ```go red := canvas.RGBA(255,0,0,0.0) blue := canvas.Hex("#00ff00") ``` -------------------------------- ### Load Fonts and Create Font Face Source: https://github.com/tdewolff/canvas/wiki/Fonts-&-Text Demonstrates loading font files into a font family and creating a font face for rendering text. It shows how to handle potential errors during font loading and how the font face selection works with different font weights. ```go fontDejaVu := canvas.NewFontFamily("dejavu") if err := fontDejaVu.LoadFontFile("DejaVuSans.ttf", canvas.FontRegular); err != nil { panic(err) } if err := fontDejaVu.LoadFontFile("DejaVuSans-Bold.ttf", canvas.FontBold); err != nil { panic(err) } face := fontDejaVu.Face(12.0, canvas.Black, canvas.FontBold, canvas.FontNormal) ``` -------------------------------- ### Rasterizer Performance Improvement Source: https://github.com/tdewolff/canvas/wiki/Planning Enhance the rasterizer's performance by implementing a rasterized cache for each glyph and path. This aims to address slowness issues identified in examples like text_example.go. ```Go // TODO: Fix slowness in the rasterizer (text_example.go is slow! use rasterized cache for each glyph/path) ``` -------------------------------- ### Set Fill and Stroke Gradients Source: https://github.com/tdewolff/canvas/wiki/Colors-&-Gradients Illustrates setting fill and stroke gradients for a canvas context. ```go SetFillGradient(gradient Gradient) SetStrokeGradient(gradient Gradient) ``` -------------------------------- ### Renderer Interface Definition Source: https://github.com/tdewolff/canvas/wiki/Renderers Defines the essential methods that every renderer must implement to be compatible with the canvas library. This includes methods for getting the canvas size and rendering paths, text, and images with a given transformation matrix. ```go type Renderer interface { Size() (float64, float64) RenderPath(path *Path, style Style, m Matrix) RenderText(text *Text, m Matrix) RenderImage(img image.Image, m Matrix) } ``` -------------------------------- ### Create Canvas Hatch Patterns in Go Source: https://github.com/tdewolff/canvas/wiki/Patterns Demonstrates how to create different hatch patterns using the canvas library. This includes line, cross, and shape hatches, specifying their properties like color, angles, spacing, and stroke thickness. ```go thickness := 0.05 lineHatch := canvas.NewLineHatch(canvas.Black, 90.0, 0.5, thickness) crossHatch := canvas.NewCrossHatch(canvas.Black, 45.0, -45.0, 0.5, 0.5, thickness) shapeHatch := canvas.NewShapeHatch(canvas.Black, canvas.Circle(0.15), 0.5, thickness) ``` -------------------------------- ### Embed Path in Rich Text Source: https://github.com/tdewolff/canvas/wiki/Fonts-&-Text Demonstrates embedding a custom path (a blue triangle in this example) within a rich text object. It shows how to define a path using the `canvas.Path` type and add it to the rich text with specified color and baseline alignment. ```go // Add blue triangle to text p := &canvas.Path{} p.LineTo(2.0, 0.0) p.LineTo(1.0, 2.0) p.Close() rt.AddPath(p, canvas.Blue, canvas.Baseline) ```