#[cfg(target_family = "wasm")] use base64::Engine; #[cfg(target_family = "wasm")] use core_types::WasmNotSend; #[cfg(target_family = "wasm")] use core_types::math::bbox::Bbox; use core_types::table::Table; #[cfg(target_family = "wasm")] use core_types::transform::Footprint; use core_types::{Color, Ctx}; pub use graph_craft::document::value::RenderOutputType; pub use graph_craft::wasm_application_io::*; use graphene_application_io::ApplicationIo; #[cfg(target_family = "wasm")] use graphic_types::Graphic; #[cfg(target_family = "wasm")] use graphic_types::Vector; use graphic_types::raster_types::Image; use graphic_types::raster_types::{CPU, Raster}; #[cfg(target_family = "wasm")] use graphic_types::vector_types::gradient::GradientStops; #[cfg(target_family = "wasm")] use rendering::{Render, RenderParams, RenderSvgSegmentList, SvgRender}; use std::sync::Arc; #[cfg(target_family = "wasm")] use wasm_bindgen::JsCast; #[cfg(target_family = "wasm")] use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement}; /// Allocates GPU memory and a rendering context for vector-to-raster conversion. #[cfg(feature = "wgpu")] #[node_macro::node(category(""))] async fn create_surface<'a: 'n>(_: impl Ctx, editor: &'a WasmEditorApi) -> Arc { Arc::new(editor.application_io.as_ref().unwrap().create_window()) } /// Sends an HTTP GET request to a specified URL and optionally waits for the response (unless discarded) which is output as a string. #[node_macro::node(category("Web Request"))] async fn get_request( _: impl Ctx, _primary: (), /// The web address to send the GET request to. #[name("URL")] url: String, /// Makes the request run in the background without waiting on a response. This is useful for triggering webhooks without blocking the continued execution of the graph. discard_result: bool, ) -> String { #[cfg(target_family = "wasm")] { if discard_result { wasm_bindgen_futures::spawn_local(async move { let _ = reqwest::get(url).await; }); return String::new(); } } #[cfg(not(target_family = "wasm"))] { #[cfg(feature = "tokio")] if discard_result { tokio::spawn(async move { let _ = reqwest::get(url).await; }); return String::new(); } #[cfg(not(feature = "tokio"))] if discard_result { return String::new(); } } let Ok(response) = reqwest::get(url).await else { return String::new() }; response.text().await.ok().unwrap_or_default() } /// Sends an HTTP POST request to a specified URL with the provided binary data and optionally waits for the response (unless discarded) which is output as a string. #[node_macro::node(category("Web Request"))] async fn post_request( _: impl Ctx, _primary: (), /// The web address to send the POST request to. #[name("URL")] url: String, /// The binary data to include in the body of the POST request. body: Vec, /// Makes the request run in the background without waiting on a response. This is useful for triggering webhooks without blocking the continued execution of the graph. discard_result: bool, ) -> String { #[cfg(target_family = "wasm")] { if discard_result { wasm_bindgen_futures::spawn_local(async move { let _ = reqwest::Client::new().post(url).body(body).header("Content-Type", "application/octet-stream").send().await; }); return String::new(); } } #[cfg(not(target_family = "wasm"))] { #[cfg(feature = "tokio")] if discard_result { let url = url.clone(); let body = body.clone(); tokio::spawn(async move { let _ = reqwest::Client::new().post(url).body(body).header("Content-Type", "application/octet-stream").send().await; }); return String::new(); } #[cfg(not(feature = "tokio"))] if discard_result { return String::new(); } } let Ok(response) = reqwest::Client::new().post(url).body(body).header("Content-Type", "application/octet-stream").send().await else { return String::new(); }; response.text().await.ok().unwrap_or_default() } /// Converts a text string to raw binary data. Useful for transmission over HTTP or writing to files. #[node_macro::node(category("Web Request"), name("String to Bytes"))] fn string_to_bytes(_: impl Ctx, string: String) -> Vec { string.into_bytes() } /// Converts extracted raw RGBA pixel data from an input image. Each pixel becomes 4 sequential bytes. Useful for transmission over HTTP or writing to files. #[node_macro::node(category("Web Request"), name("Image to Bytes"))] fn image_to_bytes(_: impl Ctx, image: Table>) -> Vec { let Some(image) = image.iter().next() else { return vec![] }; image.element.data.iter().flat_map(|color| color.to_rgb8_srgb().into_iter()).collect::>() } /// Loads binary from URLs and local asset paths. Returns a transparent placeholder if the resource fails to load, allowing rendering to continue. #[node_macro::node(category("Web Request"))] async fn load_resource<'a: 'n>(_: impl Ctx, _primary: (), #[scope("editor-api")] editor_resources: &'a WasmEditorApi, #[name("URL")] url: String) -> Arc<[u8]> { let Some(api) = editor_resources.application_io.as_ref() else { return Arc::from(include_bytes!("../../../graph-craft/src/null.png").to_vec()); }; let Ok(data) = api.load_resource(url) else { return Arc::from(include_bytes!("../../../graph-craft/src/null.png").to_vec()); }; let Ok(data) = data.await else { return Arc::from(include_bytes!("../../../graph-craft/src/null.png").to_vec()); }; data } /// Converts raw binary data to a raster image. /// /// Works with standard image format (PNG, JPEG, WebP, etc.). Automatically converts the color space to linear sRGB for accurate compositing. #[node_macro::node(category("Web Request"))] fn decode_image(_: impl Ctx, data: Arc<[u8]>) -> Table> { let Some(image) = image::load_from_memory(data.as_ref()).ok() else { return Table::new(); }; let image = image.to_rgba32f(); let image = Image { data: image .chunks(4) .map(|pixel| Color::from_unassociated_alpha(pixel[0], pixel[1], pixel[2], pixel[3]).to_linear_srgb()) .collect(), width: image.width(), height: image.height(), ..Default::default() }; Table::new_from_element(Raster::new_cpu(image)) } /// Renders a view of the input graphic within an area defined by the *Footprint*. #[cfg(target_family = "wasm")] #[node_macro::node(category(""))] async fn rasterize( _: impl Ctx, #[implementations( Table, Table>, Table, Table, Table, )] mut data: Table, footprint: Footprint, surface_handle: Arc>, ) -> Table> where Table: Render, { use core_types::table::TableRow; if footprint.transform.matrix2.determinant() == 0. { log::trace!("Invalid footprint received for rasterization"); return Table::new(); } let mut render = SvgRender::new(); let aabb = Bbox::from_transform(footprint.transform).to_axis_aligned_bbox(); let size = aabb.size(); let resolution = footprint.resolution; let render_params = RenderParams { footprint, for_export: true, ..Default::default() }; for row in data.iter_mut() { *row.transform = glam::DAffine2::from_translation(-aabb.start) * *row.transform; } data.render_svg(&mut render, &render_params); render.format_svg(glam::DVec2::ZERO, size); let svg_string = render.svg.to_svg_string(); let canvas = &surface_handle.surface; canvas.set_width(resolution.x); canvas.set_height(resolution.y); let context = canvas.get_context("2d").unwrap().unwrap().dyn_into::().unwrap(); let preamble = "data:image/svg+xml;base64,"; let mut base64_string = String::with_capacity(preamble.len() + svg_string.len() * 4); base64_string.push_str(preamble); base64::engine::general_purpose::STANDARD.encode_string(svg_string, &mut base64_string); let image_data = web_sys::HtmlImageElement::new().unwrap(); image_data.set_src(base64_string.as_str()); wasm_bindgen_futures::JsFuture::from(image_data.decode()).await.unwrap(); context .draw_image_with_html_image_element_and_dw_and_dh(&image_data, 0., 0., resolution.x as f64, resolution.y as f64) .unwrap(); let rasterized = context.get_image_data(0., 0., resolution.x as f64, resolution.y as f64).unwrap(); let image = Image::from_image_data(&rasterized.data().0, resolution.x as u32, resolution.y as u32); Table::new_from_row(TableRow { element: Raster::new_cpu(image), transform: footprint.transform, ..Default::default() }) }