Регулировка высоты и ширины скриншота с помощью html2canvas? - PullRequest
2 голосов
/ 07 октября 2019

I'd like to remove the highlighted yellow portion in the bottom, which should make the important parts of the png biggerand more clear. Вот воспроизводимый пример. Я попытался добавить параметры высоты и ширины, показанные здесь здесь , но они не влияют на мой скриншот. Это потому, что я использую тело как элемент? Я хотел бы просто удалить нижнюю часть моего shinyapp со скриншота. Как вы, вероятно, можете сказать, я новичок в JavaScript и кодировании в целом, и любая помощь очень ценится.

library(shiny)
library(shinyjs)
library(DT)
library(ggplot2)

ui <- fluidPage(
  tags$head(
    # include html2canvas library
    tags$script(src = "http://html2canvas.hertzen.com/dist/html2canvas.min.js"),
    # script for creating the prompt for download
    tags$script(
      "
            function saveAs(uri, filename) {

                var link = document.createElement('a');

                if (typeof link.download === 'string') {

                    link.href = uri;
                    link.download = filename;

                    //Firefox requires the link to be in the body
                    document.body.appendChild(link);

                    //simulate click
                    link.click();

                    //remove the link when done
                    document.body.removeChild(link);

                } else {

                    window.open(uri);

                }
            }
            "
    )
  ),
  useShinyjs(),
  actionButton("screenshot","Take Screenshot"),
  dataTableOutput("table"),
  plotOutput("plot")


)

server <- function(input, output, session) {
  observeEvent(input$screenshot,{
    shinyjs::runjs(
      'html2canvas(document.querySelector("body")).then(canvas => {
                saveAs(canvas.toDataURL(), "shinyapp.png");
           });'
    )
  })


  output$table <- renderDataTable(iris)

  output$plot <- renderPlot(ggplot(data = iris) + 
                              geom_point(aes(x = Sepal.Length, y = Sepal.Width)))
}

shinyApp(ui, server)

1 Ответ

1 голос
/ 08 октября 2019

Не уверен, что вы подразумеваете под уберите нижнюю часть моего shinyapp со скриншота . Вот рабочий пример для печати только таблицы или изображения графика в виде скриншотов.

# To print only the table in the screenshot
shinyjs::runjs(
      'html2canvas(document.querySelector("table")).then(canvas => {
                saveAs(canvas.toDataURL(), "shinyapp.png");
           });')

# To print only the image in the screenshot
shinyjs::runjs(
      'html2canvas(document.querySelector("img")).then(canvas => {
                saveAs(canvas.toDataURL(), "shinyapp.png");
           });')

Надеюсь, это поможет!

...