Skip to content

Commit

Permalink
Add Canvas.Write, see #300
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed May 13, 2024
1 parent 1cb7175 commit 50f56b3
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,14 +836,19 @@ func (c *Canvas) RenderViewTo(r Renderer, view Matrix) {
// Writer can write a canvas to a writer.
type Writer func(w io.Writer, c *Canvas) error

// WriteFile writes the canvas to a file named by filename using the given writer.
func (c *Canvas) WriteFile(filename string, w Writer) error {
// Write writes the canvas to an io.Writer using the given writer. See renderers/ for an overview of implementations of canvas.Writer.
func (c *Canvas) Write(w io.Writer, writer Writer) error {
return writer(w, c)
}

// WriteFile writes the canvas to a file using the given writer. See renderers/ for an overview of implementations of canvas.Writer.
func (c *Canvas) WriteFile(filename string, writer Writer) error {
f, err := os.Create(filename)
if err != nil {
return err
}

if err = w(f, c); err != nil {
if err = writer(f, c); err != nil {
f.Close()
return err
}
Expand Down

0 comments on commit 50f56b3

Please sign in to comment.