Skip to content

Commit

Permalink
Merge pull request #2 from hypebright/fadeout
Browse files Browse the repository at this point in the history
Add fadeOut effect for stopping fireworks
  • Loading branch information
hypebright authored Mar 4, 2024
2 parents 50021e7 + a80ff60 commit 870446d
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 28 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: fireworks
Title: Fireworks for your 'Shiny' application
Version: 0.1.0
Version: 0.1.1
Authors@R:
person("Veerle", "van Leemput", , "[email protected]", role = c("aut", "cre"))
Description: 'fireworks' is a wrapper around the 'fireworks-js' library that contains
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# fireworks 0.1.1

* Added a `fadeOut` argument to the `stop` method to control whether the fireworks fade out or disappear instantly. The effects lasts 2000 ms to remove the fireworks in a natural manner. Thanks to @laresbernardo for the suggestion in issue #1. Example can be found in `inst/examples/02-fireworks-on-demand.R`.
* Added a `NEWS.md` file to track changes to the package.
11 changes: 7 additions & 4 deletions R/fireworks.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fireworks <- function(id, width = "100%", height = "400px", options = list()) {
#' @import R6
#' @export
Fireworks <- R6::R6Class(
"fireworks",
"Fireworks",
public = list(
#' @details
#' Create fireworks.
Expand Down Expand Up @@ -94,12 +94,15 @@ Fireworks <- R6::R6Class(
},
#' @details
#' Stop the fireworks
stop = function(){
#' @param fadeOut Whether to fade out the fireworks before stopping. Note that
#' this will take 2000ms to complete.
stop = function(fadeOut = FALSE){
if (is.null(private$.id)) {
private$.session$sendCustomMessage("fireworks-stop", list())
private$.session$sendCustomMessage("fireworks-stop", list(fadeOut = fadeOut))
} else {
for (i in 1:length(private$.id)) {
private$.session$sendCustomMessage("fireworks-stop", list(id = private$.id[[i]]))
private$.session$sendCustomMessage("fireworks-stop", list(id = private$.id[[i]],
fadeOut = fadeOut))
}
}
invisible(self)
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ fw <- Fireworks$new(options = list(hue = list(min = 0, max = 45),
traceSpeed = 5))
```

### Natural stop effect

By default, fireworks are removed immediately when `stop` is called. You can add a natural stop effect by setting `fadeOut = TRUE`:

```r
fw$stop(fadeOut = TRUE)
```

The effects takes 2000ms and changes the intensity of the fireworks to `1`:

![](./inst/images/fireworks-fadeout.gif)

## Acknowledgements

As this is a wrapper around an existing library, I want to give credit to the original authors:
Expand Down
2 changes: 1 addition & 1 deletion inst/examples/02-fireworks-on-demand.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ server <- function(input, output, session) {
}) |> bindEvent(input$launch)

observe({
fw$stop()
fw$stop(fadeOut = TRUE)
}) |> bindEvent(input$stop)

}
Expand Down
2 changes: 1 addition & 1 deletion inst/fireworks-0.1.0/dist/fireworks.min.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion inst/fireworks-0.1.0/dist/fireworks.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions inst/fireworks-0.1.0/dist/fireworks.min.js.map

Large diffs are not rendered by default.

Binary file added inst/images/fireworks-fadeout.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 21 additions & 13 deletions man/fireworksClass.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions srcjs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,28 @@ Shiny.addCustomMessageHandler("fireworks-start", function(message) {

Shiny.addCustomMessageHandler("fireworks-stop", function(message) {
const identifier = message.id == null ? "full-screen" : message.id;
window[`fireworks-${identifier}`].stop();

if (message.id == null) {
document.body.removeChild(document.querySelector(".fireworks-full-screen"));
const currentOpts = window[`fireworks-${identifier}`].currentOptions;

const stopFireworks = () => {
window[`fireworks-${identifier}`].stop();
if (message.id == null) {
document.body.removeChild(
document.querySelector(".fireworks-full-screen")
);
}
};

if (message.fadeOut) {
window[`fireworks-${identifier}`].updateOptions({
...currentOpts,
intensity: 1
});
// add 2000 ms delay to allow fireworks to fade out
setTimeout(() => {
stopFireworks();
}, 2000);
} else {
stopFireworks();
}
});

0 comments on commit 870446d

Please sign in to comment.