Nodejs | GM drawRectangle() Function

The drawRectangle() function is an inbuilt function in GraphicsMagick library which is used to draw a rectangle with specified coordinates. The function returns the true value on success.

Syntax:

drawRectangle( x0, y0, x1, y1, wc, hc )

Parameters: This function accept six parameters as mentioned above and desecribed below:

  • x0: It holds the first x-coordinate of the rectangle.
  • y0: It holds the first y-coordinate of the rectangle.
  • x1: It holds the second x-coordinate of the rectangle.
  • y1: It holds the second y-coordinate of the rectangle.
  • wc: It is an optional parameter that holds the roundness of the width.
  • hc: It is an optional parameter that holds the roundness of the height.

Return Value: This function returns the GraphicsMagick object.

Example 1: Without optional parameters



filter_none

edit
close

play_arrow

link
brightness_4
code

// Include gm library
var gm = require('gm');
  
//Import the image
gm(600, 300, 'white')
  
// set the color for the stroke
.stroke("green", 3)
  
// Set the font 
.font("Helvetica.ttf", 60)
  
// Invoke drawRectangle function with
// x0: 10, y0: 100, x1:260, y1:210
.drawRectangle(10, 100, 260, 210)
  
// Invoke drawText in order to
// name the box as 1 
.drawText(150, 170, "1")
  
// Invoke drawrectangle function with
// x0: 260, y0:100, x1: 350, y1: 210
.drawRectangle(260, 100, 350, 210)
  
// Invoke drawText in order to
// name the box as 2
.drawText(300, 170, "2")
  
// Call to drawText Function
.drawText(100, 280, "GeeksforGeeks!")
  
// Process and write the image 
.write("drawRectangle1.png", function (err) {
  if (!err) console.log('done');
});

chevron_right


Output:

Example 2: With use of optional parameters

filter_none

edit
close

play_arrow

link
brightness_4
code

// Include gm library
var gm = require('gm');
  
// Import the image
gm(600, 300, 'white')
  
// set the color for the stroke
.stroke("green", 3)
  
// Set the font 
.font("Helvetica.ttf", 60)
  
// Invoke drawRectangle function with
// x0: 10, y0: 100, x1:260, y1:210,
// wc: 40, wh: 40
.drawRectangle(10, 100, 260, 210, 40, 40)
  
// Invoke drawText in order to name
// the box as 1 
.drawText(150, 170, "1")
  
// Invoke drawrectangle function with
// x0: 260, y0:100, x1: 350, y1: 210,
// wc: 80, wh: 80
.drawRectangle(260, 100, 350, 210, 80, 80)
  
// Invoke drawText in order to
// name the box as 2
.drawText(300, 170, "2")
  
// Call to drawText Function
.drawText(100, 280, "GeeksforGeeks!")
  
// Process and write the image 
.write("drawRectangle1.png", function (err) {
  if (!err) console.log('done');
});

chevron_right


Output:

Reference:




My Personal Notes arrow_drop_up

Recommended Posts: