如何通过起点和终点绘制矩形?

时间:2017-07-21 08:19:53

标签: javascript html canvas

现在我可以通过宽度和高度的起点绘制一个矩形。

context.strokeStyle = "red";
context.beginPath();
context.rect(startX, startY, width, height);
context.stroke();

我的问题可以画,startX,startY,EndX,EndY?

2 个答案:

答案 0 :(得分:1)

尝试一下。希望这会有所帮助。

library(shiny)
library(Seurat)

#-----------------------------------------------------------
# Select dataset
#-----------------------------------------------------------
x <- readRDS("data/x.rds")
y <- readRDS("data/y.rds")
#-----------------------------------------------------------

#-----------------------------------------------------------
# App code
#-----------------------------------------------------------

ui <- fluidPage(

   titlePanel("Shiny app"),

   sidebarLayout(
      sidebarPanel(

        checkboxGroupInput("data",
                  "Select data to use",
                  choices = c("x", "y"),
                  selected = 'x',
                  inline = TRUE,
                  width = NULL),

        uiOutput('columns')

      ),

      mainPanel(
         plotOutput(outputId = "searchgene")
      )
   )
)

server <- function(input, output) {

   output$searchgene <- renderPlot({
     mydata = get(input$data) # repeated
     FeaturePlot(mydata,
                 features = input$search)})

   output$columns <- renderUI({
     mydata = get(input$data) # repeated
     selectInput(inputId = "search",
                 label = "Plot 1 gene",
                 choices = sort(rownames(mydata)),
                 multiple = FALSE,
                 selectize = TRUE)
   })  
}


#-----------------------------------------------------------
# Run the application 
#-----------------------------------------------------------
shinyApp(ui = ui, server = server)

答案 1 :(得分:0)

只需编写自己的函数来为您绘制矩形。如果您有两个分(x1 , y1)(x2, y2),则宽度是x的差异,因此x2 - x1和y的差异高度为y2 - y1

使用此功能,您的功能可能类似于:

function drawRect(x1, y1, x2, y2){
    context.beginPath();
    context.rect(x1, y1, x2 - x1, y2 - y1);
    context.stroke();
}

或者,如果您不想使用函数,只需执行:

context.rect(x1, y1, x2 - x1, y2 - y1)