Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
Diniboy1123 opened this issue Feb 24, 2025 · 1 comment
Closed
2 tasks done

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

Diniboy1123 opened this issue Feb 24, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@Diniboy1123
Copy link

Diniboy1123 commented Feb 24, 2025

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

@Diniboy1123 Diniboy1123 added the unverified A bug that has been reported but not verified label Feb 24, 2025
@Diniboy1123
Copy link
Author

Update: I can reproduce this with fyne/v2 as well:

width := float32(1024)
height := float32(758)

myCanvas := software.NewTransparentCanvas()

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.NewSize(width, height))

img := myCanvas.Capture()

@andydotxyz andydotxyz added bug Something isn't working and removed unverified A bug that has been reported but not verified labels Feb 25, 2025
@andydotxyz andydotxyz added this to the "F" release, Early 2025 milestone Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants