aramvisser / blend2d.cr

Crystal bindings for Blend2D
zlib License
0 stars 0 forks source link

Creating a basic HTML renderer #1

Open avrame opened 1 day ago

avrame commented 1 day ago

I'm thinking of using this library to create a basic html renderer, just for fun. Instead of writing to a file, is it possible to output to a window? Should I use a library like SDL2 to do this?

aramvisser commented 1 day ago

Hi Avrame,

It is possible to output to a window. Instead of writing the image to a file directly, you write it to memory and access that memory in your windowing library.

I actually do this in my own project, using CrSFML instead of SDL2. I assume SDL2 has something similar.

examples/image_data.cr shows you how to write the image to memory.

# write_to_data returns a Blend2D::BLArray that gets converted to a slice
as_bytes = img.write_to_data(Blend2D::ImageCodec::BMP).to_slice

# write_to_io writes the image to the IO and returns the IO
as_io = img.write_to_io(IO::Memory.new, Blend2D::ImageCodec::PNG)

Then you access that memory in your GUI library. In CrSFML I do it like this:

bytes = image.write_to_data(Blend2D::ImageCodec::PNG).to_slice
texture = SF::Texture.from_memory bytes
sprite = SF::Sprite.new texture

I hope that helps you.

avrame commented 1 day ago

Thanks for the detailed reply @aramvisser . Do you think this would be fast enough to handle animations?

aramvisser commented 8 hours ago

I'm not sure how well animations would work. Simple animations should be fine, but complex or big animations might take too long? I don't know.

Blend2D itself does have some sample applications that animate. You could try to look how that works.