R/fabric_text.R

Defines functions fabric_text

Documented in fabric_text

#' Insert text within canvas element
#'
#' @param cid the id of the canvas element
#' @param cwidth the width of the canvas element. Defaults to 800
#' @param cheight the height of the canvas element. Defaults to 600
#' @param cfill the color of the canvas element
#' @param textId the id of the text
#' @param text the content of the text
#' @param left the text's position from the left relative to the canvas element. Defaults to 100
#' @param top the text's position from the top relative to the canvas element. Defaults to 100
#' @param fill the text's color. Defaults to '#2F3941' (dark shade of cyan-blue)
#' @param angle the angle of rotation of the text. Defaults to 0 (no rotation)
#' @param opacity text opacity (from 0 to 1). Defaults to 1
#' @param fontFamily the font family of the text. Defaults to 'Comic Sans'
#' @param fontSize text sizing. Defaults to 40
#' @param fontStyle the font style of the text. Either 'normal' or 'italic'
#' @param strokecolor the stroke color of the text Defaults to '#282A36' (Very dark grayish blue)
#' @param strokewidth the stroke width of the text. Defaults to 1
#' @param fontWeight allows the user to make text thicker or thinner. Keywords can be used ('normal', 'bold'), or numbers. Defaults to 'normal'
#' @param underline logical. Whether to underline the text or not. Defaults to FALSE
#' @param linethrough logical. Whether to insert a line through the text or not. Defaults to FALSE
#' @param overline logical. Whether to put a line above the text or not. Defaults to FALSE
#' @param selectable logical. If TRUE, the user can modify interactively the image's size, position and rotation. Defaults to TRUE
#' @param shadow logical. If TRUE a text shadow will be inserted behind the raw text. Defaults to FALSE
#' @param shadowCol the color of the text shadow. Defaults to #FFFAF0 (floral white)
#' @param textAlign the alignment of text. Useful when there are line breaks. Defaults to "center"
#' @param lineHeight the height of the line breaks.Defaults to 1
#' @param textBackgroundColor the background color of the text, defaults to NULL
#' @param isDrawingMode logical. If TRUE, the user can draw inside the canvas element.
#'
#' @return a text object within a canvas element
#' @export
#'
#' @examples
#'
#'
#' if (interactive()) {
#'
#'
#'ui <- fluidPage(
#'
#'
#'fabric_text(cid = "can",
#'          textId = "text",
#'          text = "But A Hero Is A Guy Who Gives Out The Meat To Everyone Else.",
#'          cfill = "#DD5347",
#'          left = 120,
#'          shadowCol = "blue",
#'          fontSize = 20,
#'          fontWeight = "bold",
#'          lineHeight = 3
#'          )
#' )
#'server <- function(input, output) {}
#'
#'shinyApp(ui = ui, server = server)
#'
#'}


fabric_text <- function(cid,
                        cwidth = 800,
                        cheight = 600,
                        cfill = "#FFFFFF",
                        textId,
                        text,
                        left = 100,
                        top = 100,
                        fill = "#2F3941",
                        angle = 0,
                        opacity = 1,
                        fontFamily = 'Comic Sans',
                        fontSize = 40,
                        fontStyle = 'normal',
                        strokecolor = "#282A36",
                        strokewidth = 1,
                        fontWeight = "normal",
                        underline = FALSE,
                        linethrough = FALSE,
                        overline = FALSE,
                        selectable = TRUE,
                        shadow = FALSE,
                        shadowCol = "#FFFAF0",
                        textAlign = "center",
                        lineHeight = 1,
                        textBackgroundColor = NULL,
                        isDrawingMode = FALSE){




  if (!fontStyle %in% c("normal",
                    "italic")) {
    stop(paste0("fontStyle accepts two values: 'normal' or 'italic'"))
  }

  selectable <- ifelse(selectable == TRUE, "true", "false")

  isDrawingMode <- ifelse(isDrawingMode == TRUE, "true", "false")

  underline <- ifelse(underline == TRUE, "true", "false")

  linethrough <- ifelse(linethrough == TRUE, "true", "false")

  overline <- ifelse(overline == TRUE, "true", "false")

  shadow <- ifelse(shadow == TRUE, glue::glue("shadow:'{shadowCol} 5px 5px 5px'"), "")

  tBG <- ifelse(is.null(textBackgroundColor), character(0), glue::glue("textBackgroundColor: '{textBackgroundColor}',"))




  htmltools::tagList(

    htmltools::tags$canvas(id = cid, width = cwidth, height = cheight),

    fabric_dependency(),

    htmltools::tags$script(htmltools::HTML(glue::glue(


      "

  var {cid} = new fabric.Canvas('{cid}', {{

    isDrawingMode: {isDrawingMode}

    }});

  {cid}.backgroundColor = '{cfill}';

  var {textId} = new fabric.Text(\"{text}\", {{

  left: {left},
  top: {top},
  fontFamily: '{fontFamily}',
  fontSize: {fontSize},
  fontStyle: '{fontStyle}',
  fontWeight: '{fontWeight}',
  underline: {underline},
  linethrough: {linethrough},
  overline: {overline},
  fill: '{fill}',
  angle: {angle},
  opacity: {opacity},
  stroke: '{strokecolor}',
  strokeWidth: {strokewidth},
  textAlign: '{textAlign}',
  lineHeight: {lineHeight},
  {tBG}
  selectable: {selectable},
  {shadow}

}});

  {cid}.add({textId});

  "
    , .na = "")))



  )


}

Try the fabricerin package in your browser

Any scripts or data that you put into this service are public.

fabricerin documentation built on Aug. 15, 2020, 1:06 a.m.