Skip to content

Commit

Permalink
Support WebAssembly
Browse files Browse the repository at this point in the history
  • Loading branch information
dipterix committed Sep 20, 2024
1 parent b07713d commit 28fe8ae
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 36 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Changes since last CRAN release
* `cb5aed88 (HEAD -> master)` [_`dipterix`_]: Avoid using pandoc to save the whole page self-contained
* `bb7ba967 (origin/master, origin/HEAD)` [_`dipterix`_]: Fixed the depth issue in electrode material shader
* `91141d90 (HEAD -> master)` [_`dipterix`_]: Support `WebAssembly`
* `b07713dd (origin/master, origin/HEAD)` [_`dipterix`_]: Avoid using pandoc to save the whole page self-contained
* `bb7ba967` [_`dipterix`_]: Fixed the depth issue in electrode material shader
* `583d267e` [_`dipterix`_]: Set maximum render length cut-off for electrode prototypes
* `4037eddb` [_`dipterix`_]: Added model tangent (usually the direction) to electrode shader so outlines are correctly visualized
* `da148714` [_`dipterix`_]: Fixed electrode prototype rendering issue (color) on Windows
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: threeBrain
Type: Package
Title: Your Advanced 3D Brain Visualization
Version: 1.1.1.9018
Version: 1.1.1.9019
Authors@R: c(
person("Zhengjia", "Wang", email = "[email protected]", role = c("aut", "cre", "cph")),
person("John", "Magnotti", email = "[email protected]", role = c("ctb", "res")),
Expand Down
11 changes: 7 additions & 4 deletions R/aaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,21 @@ brain_setup <- function(continued = FALSE, show_example = TRUE, ...){

get_os <- function(){
os <- R.version$os
if(stringr::str_detect(os, '^darwin')){
if(grepl('^darwin', os, ignore.case = TRUE)){
return('darwin')
}
if(stringr::str_detect(os, '^linux')){
if(grepl('^linux', os, ignore.case = TRUE)){
return('linux')
}
if(stringr::str_detect(os, '^solaris')){
if(grepl('^solaris', os, ignore.case = TRUE)){
return('solaris')
}
if(stringr::str_detect(os, '^win')){
if(grepl('^win', os, ignore.case = TRUE)){
return('windows')
}
if(grepl("^(emscr|wasm)", os, ignore.case = TRUE)) {
return('emscripten')
}
return('unknown')
}

Expand Down
87 changes: 58 additions & 29 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,42 +102,71 @@ knit_print.threejs_brain <- function(x, ..., options = NULL) {
#' @export
print.threejs_brain <- function (x, ..., embed = NA, viewer = getOption("viewer", utils::browseURL)) {

tmp_dir <- tempdir(check = TRUE)

if(is.na(embed)) {
embed <- !inherits(x, "suppress_viewer")
}
if( identical(get_os(), "emscripten") ) {
# this is in WASM, use save_brain
digest_string <- dipsaus::digest(x)
tmp_file <- file.path(tempdir(check = TRUE), sprintf("threeBrain-wasm-%s.html", digest_string))
save_brain(x, tmp_file, as_zip = FALSE)

tmp_file <- normalizePath(tmp_file, mustWork = "/")

# You will have this package when running in WebAssembly.
webr <- asNamespace("webr")
webr$eval_js(
paste0(
"chan.write({",
" type: 'browse',",
" data: { url: '", tmp_file, "' },",
"});"
# "chan.write({",
# " type: 'pager',",
# " data: {",
# " path: '", tmp_file, "',",
# " header: '',",
# " title: 'RAVE 3D Viewer', ",
# " deleteFile: true,",
# " },",
# "});"
)
)

if( embed ) {
print(to_html(x), viewer = viewer)
} else {
# wrap up files as html object
html <- htmltools::as.tags(x, standalone = TRUE)
# prepare widget
www_dir <- file.path(tmp_dir, "threeBrainViewer")
if( !dir.exists(www_dir) ) {
dir.create(www_dir, showWarnings = FALSE, recursive = FALSE)
tmp_dir <- tempdir(check = TRUE)

if(is.na(embed)) {
embed <- !inherits(x, "suppress_viewer")
}
index_name <- x$x$data_filename
index_name <- gsub("^config", replacement = "index", index_name)
index_name <- gsub("json$", "html", index_name)
index_html <- file.path(www_dir, index_name)

htmltools::save_html(html, file = index_html, background = "white", libdir = "lib")
if( embed ) {
print(to_html(x), viewer = viewer)
} else {
# wrap up files as html object
html <- htmltools::as.tags(x, standalone = TRUE)
# prepare widget
www_dir <- file.path(tmp_dir, "threeBrainViewer")
if( !dir.exists(www_dir) ) {
dir.create(www_dir, showWarnings = FALSE, recursive = FALSE)
}
index_name <- x$x$data_filename
index_name <- gsub("^config", replacement = "index", index_name)
index_name <- gsub("json$", "html", index_name)
index_html <- file.path(www_dir, index_name)

if(!file.exists(file.path(www_dir, "favicon.ico"))) {
file.copy(
from = system.file("favicon.ico", package = "threeBrain"),
to = file.path(www_dir, "favicon.ico")
)
}
htmltools::save_html(html, file = index_html, background = "white", libdir = "lib")

app <- ensure_simple_server( www_dir )
url <- gsub("/$", "", app$url)
url <- sprintf("%s/%s", url, index_name)
utils::browseURL( url )
}
if(!file.exists(file.path(www_dir, "favicon.ico"))) {
file.copy(
from = system.file("favicon.ico", package = "threeBrain"),
to = file.path(www_dir, "favicon.ico")
)
}

app <- ensure_simple_server( www_dir )
url <- gsub("/$", "", app$url)
url <- sprintf("%s/%s", url, index_name)
utils::browseURL( url )
}
}
invisible(x)
}

Expand Down

0 comments on commit 28fe8ae

Please sign in to comment.