GithubHelp home page GithubHelp logo

rparticle's Introduction

rparticle

Quick experiment to try out react-particle-image in R using reactR

Installation

remotes::install_github("timelyportfolio/rparticle")

Example

side by side example

# remotes::install_github("timelyportfolio/rparticle")

library(rparticle)
library(ggplot2)
library(base64enc)
library(htmltools)

# replicate example
(ex1 <- rparticle(
  list(
    src = "https://malerba118.github.io/react-particle-image-demo/logo512.png",
    #src = src,
    scale = 0.75,
    entropy = 20,
    maxParticles = 4200,
    particleOptions = list(
      filter = htmlwidgets::JS('
        ({ x, y, image }) => {
          // Get pixel
          const pixel = image.get(x, y);
          // Make a particle for this pixel if blue > 50 (range 0-255)
          return pixel.b > 50;
        }
      '),
      color =  htmlwidgets::JS('({ x, y, image }) => "#fff"')
    ),
    mouseMoveForce = htmlwidgets::JS('
      (x,y) => {
        return forces.disturbance(x, y, 5);
      };
    ')
  )
)
)

tmp <- tempfile(fileext = ".png")
#on.exit({unlink(tmp)})
png(file = tmp, width =480, height=480, units="px")
ggplot(
  data.frame(x = letters[1:5], y = runif(5,1,100)),
  aes(x = x, y = y, fill = x)
) +
  geom_col(show.legend = FALSE) +
  theme_void() +
  theme(panel.background = element_rect(fill = "black"))
dev.off()
src <- paste0(
  "data:image/png;base64,",
  base64enc::base64encode(tmp)
)

#do it with a ggplot
(ex2 <- rparticle(
  list(
    src = src,
    scale = 0.75,
    entropy = 20,
    maxParticles = 4200,
    particleOptions = list(
      filter = htmlwidgets::JS('
        ({ x, y, image }) => {
          // Get pixel
          const pixel = image.get(x, y);
          // Make a particle for this pixel if red > 200 (range 0-255)
          return pixel.r > 50 || pixel.g > 50 || pixel.b > 50
        }
      '),
      color =  htmlwidgets::JS('
         ({ x, y, image }) => {
            const pixel = image.get(x, y);
            return("rgb(" + pixel.r + "," + pixel.g + "," + pixel.b + ")")
         }
      ')
    ),
    mouseMoveForce = htmlwidgets::JS('
      (x,y) => {
        return forces.disturbance(x, y, 5);
      };
    ')
  )
)
)

browsable(tagList(
  tags$style(".html-widget{display:inline;}"),
  ex1,
  ex2
))

rparticle's People

Contributors

timelyportfolio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

han-tun

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.