Skip to content

SoftwareCanvas resize only works properly if it's the last step #5548

@Diniboy1123

Description

@Diniboy1123

Checklist

  • I have searched the issue tracker for open issues that relate to the same problem, before opening a new one.
  • This issue only relates to a single bug. I will open new issues for any other problems.

Describe the bug

Following up on the suggestion from Discord to report this here... If i take the following code:

width := 1024
height := 758

myCanvas := playground.NewSoftwareCanvas()

clockText := canvas.NewText(time.Now().Format("15:04:05"), color.White)
clockText.TextSize = 150
clockText.Alignment = fyne.TextAlignCenter

clockContainer := container.NewCenter(clockText)

myCanvas.SetContent(clockContainer)
myCanvas.Resize(fyne.Size{Width: width, Height: height})

img := myCanvas.Capture()

And save the result as an image, I get the expected result:

Image

But if I move myCanvas.Resize(fyne.Size{Width: width, Height: height}) directly after myCanvas := playground.NewSoftwareCanvas(), the result will be wrong:

Image

How to reproduce

  1. Copy my provided example code
  2. go run .
  3. See that the resulting clock.png has the wrong resolution, it should be 1024x758.
  4. Put myCanvas.Resize(fyne.Size{Width: width, Height: height}) under myCanvas.SetContent(clockContainer).
  5. Notice that the image is rendered properly.

Screenshots

No response

Example code

package main

import (
	"fmt"
	"image"
	"image/color"
	"image/png"
	"os"
	"time"

	"fyne.io/fyne"
	"fyne.io/fyne/canvas"
	"fyne.io/fyne/container"
	"fyne.io/fyne/tools/playground"
)

func main() {
	width := 1024
	height := 758

	myCanvas := playground.NewSoftwareCanvas()
	myCanvas.Resize(fyne.Size{Width: width, Height: height})

	clockText := canvas.NewText(time.Now().Format("15:04:05"), color.White)
	clockText.TextSize = 150
	clockText.Alignment = fyne.TextAlignCenter

	clockContainer := container.NewCenter(clockText)

	myCanvas.SetContent(clockContainer)

	img := myCanvas.Capture()

	saveImage(img, "clock.png")
}

func saveImage(img image.Image, filename string) {
	f, err := os.Create(filename)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		fmt.Println(err)
		return
	}
}

Fyne version

v2.5.4

Go compiler version

1.24.0

Operating system and version

Arch Linux amd64

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions