Giter Site home page Giter Site logo

Slow text rendering? about canvas HOT 3 CLOSED

tfriedel6 avatar tfriedel6 commented on May 22, 2024
Slow text rendering?

from canvas.

Comments (3)

tfriedel6 avatar tfriedel6 commented on May 22, 2024

Hi, yes that is how it works. Currently there isn't any caching mechanism. It might be worth considering since it can be a common use case to draw the same strings every frame, but for now I have not planned it.

One thing you can try is to use separate canvases for text caching. Here is an example:

package main

import (
	"github.com/tfriedel6/canvas"
	"github.com/tfriedel6/canvas/backend/goglbackend"
	"github.com/tfriedel6/canvas/sdlcanvas"
)

func main() {
	wnd, cv, err := sdlcanvas.CreateWindow(1280, 720, "Canvas Test")
	if err != nil {
		panic(err)
	}
	defer wnd.Destroy()

	backend, err := goglbackend.NewOffscreen(110, 60, true, nil)
	if err != nil {
		panic(err)
	}
	cv2 := canvas.New(backend)

	cv2.SetFillStyle("#FFF")
	cv2.SetFont("_testapp/Roboto-Light.ttf", 40)
	cv2.FillText("hello", 10, 50)

	wnd.MainLoop(func() {
		cv.ClearRect(0, 0, 1280, 720)
		cv.DrawImage(cv2, 50, 50)
		cv.DrawImage(cv2, 50, 100)
	})
}

The offscreen canvas should be used directly in the DrawImage call, so it should be quite fast. However if you need to render different strings every frame it will not work.

from canvas.

JohnEmhoff avatar JohnEmhoff commented on May 22, 2024

Thanks, that's very helpful! Most of my text is static but not all of it, and zoom levels change pretty frequently -- I think I'm going to see if I can look into drawing text using a font atlas.

If I start interspersing calls to opengl (e.g., changing current texture, vertex lists, and shaders), what do I need to do to play nicely with and not step on any state that your library depends on?

from canvas.

tfriedel6 avatar tfriedel6 commented on May 22, 2024

The shaders, textures, and buffers should be set as they are needed, so that shouldn't be a problem, I think. The stencil buffer settings are also set as needed, but stencil testing needs to be enabled for it to work. Similarly, the blend modes need to be kept, the depth buffer and depth testing should be disabled, and back face culling as well. Otherwise I can't think of anything right now. I think it should work, but I keep getting surprised by some aspects of OpenGL, so I might be missing something.

from canvas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.