Comments (6)
Yes! You can use the image
crate to export the content of a texture to the filedisk. Check this example:
use notan::draw::*;
use notan::prelude::*;
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(setup)
.add_config(DrawConfig) // Simple way to add the draw extension
.build()
}
fn setup(app: &mut App, gfx: &mut Graphics) {
// create a render texture using the screen's size
let (width, height) = gfx.size();
let mut rt = gfx.create_render_texture(width, height).build().unwrap();
// draw a triangle
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
draw.triangle((400.0, 100.0), (100.0, 500.0), (700.0, 500.0))
.color_vertex(Color::RED, Color::BLUE, Color::GREEN);
// render the triangle into the RenderTexture
gfx.render_to(&mut rt, &draw);
// read the pixels from the RenderTexture
let mut pixels = vec![0; (width * height * 4) as usize];
gfx.read_pixels(&rt).read_to(&mut pixels).unwrap();
// use the image crate to export the buffer to the filedisk
image::save_buffer("triangle.png", &pixels, width as _, height as _, image::ColorType::Rgba8).unwrap();
// close the app
app.exit();
}
This will create a png with this:
This solution will not work on the web, if you need to do this on the web let me know, maybe we can create some custom solution integrated in notan to do it.
Feel free to close this if it resolve your question, have a nice day!
from notan.
Thanks for the quick response! Can you explain a little more about why webgl backend doesn't support image exporting? I think image crate works in wasm32 target.
from notan.
Just to be sure, I may be wrong with your question, but I am talking about render to GPU and then move it to the CPU and exporting it to a file. Is that your use case? If you don't have a GPU notan will not work for you because all the rendering happens in the GPU.
Anyway, going back to the question.
The problem is not the image crate but the filesystem and how the browsers work. Save a file on browsers is "hard" and saving an image can be tricky because of encoding.
So, there are two options:
- We can render the textures to an offscreen canvas and then use the canvas to create an
url object
and force the download. The heavy lifting of encoding will be done by the browser here, but we need at least a secondary (hidden) canvas to do it. - We can use
image
crate, do the encoding with it, and then create a jsBlob
object, the blob to anurl object
and force the download. This seems cleaner (though maybeimage
crate is a little slower than the encoding systems of the browser itself.)
This can be a nice feature behind a feature flag. What do you think? If this is useful for you I can try to do it for the develop branch with a simple API that works across platforms.
from notan.
I see why you are feeling confused. I'm actually trying to export image buffer for other jobs like image analyzing and more. I think this is quite straightforward (after I know how to do it LOL) so I won't bother you to add a new API. Let's just close this issue and thanks for your explanation!
from notan.
Hey @zimond I thought that this was a cool feature and I added it behind the texture_to_file
feature in the develop branch.
The same example now will be:
use notan::draw::*;
use notan::prelude::*;
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(setup)
.add_config(DrawConfig) // Simple way to add the draw extension
.build()
}
fn setup(app: &mut App, gfx: &mut Graphics) {
// create a render texture using the screen's size
let (width, height) = gfx.size();
let mut rt = gfx.create_render_texture(width, height).build().unwrap();
// draw a triangle
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
draw.triangle((400.0, 100.0), (100.0, 500.0), (700.0, 500.0))
.color_vertex(Color::RED, Color::BLUE, Color::GREEN);
// render the triangle into the RenderTexture
gfx.render_to(&mut rt, &draw);
// export texture content to a file
rt.to_file(gfx, "triangle.png").unwrap();
// close the app
app.exit();
}
It will take care of flipping the output for the RenderTexture and It works on browsers and desktop. I hope it helps.
from notan.
Thanks!
from notan.
Related Issues (20)
- Use the CSS property `image-rendering: pixelated` when `antialias: false` HOT 1
- Error building notan_macro (Ubuntu 22.04.2 LTS) HOT 2
- Unresponsive window if using .vsync(true) on linux HOT 2
- DrawTransform matrix multiplication possibly in wrong order HOT 1
- Windows OS - Issue with scaling factor HOT 2
- Large image files load to black texture HOT 13
- Event::Exit is never triggered
- Elaborate the nine slicing example. HOT 1
- Allow users to select SubPixel precision drawing text. HOT 1
- Renderer slowing down from 0.8 to 0.9 version HOT 2
- Allow enable/disable `touch_as_mouse` at runtime. HOT 1
- Investigate cosmic-text and etagere used on glyphon to improve Text rendering support
- 0.9.5 EGUI Custom font example error HOT 2
- Allow to use Uniform Buffers across pipelines HOT 1
- Wayland + GNOME: WindowConfig::size() sets size incorrectly HOT 11
- Mouse scrolling issue HOT 2
- Clipboard not working on Linux x11 (i3wm) HOT 1
- Multisampling setting seems to be ignored HOT 2
- notan_draw docs won't build since 0.8.0 HOT 2
- `notan_graphics` depends on `image` when feature `texture_to_file` is disabled HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from notan.