p5.js | center() Function
Last Updated :
20 Aug, 2019
The center() function is used to set the alignment of element into the center either vertically, horizontally, or both, relative to its parent element or according to the body if the element has no parent. If this function does not contain any parameters then the element will align both vertically and horizontally.
Note: This function requires the p5.dom library. So add the following line in the head section of the index.html file.
<script language= "javascript"
type= "text/javascript" src= "path/to/p5.dom.js" >
</script>
|
Syntax:
center( align )
Parameters: This function accepts single parameter align which holds the string ‘vertical’ or ‘horizontal’ to align the element.
Below examples illustrate the center() function in p5.js:
Example 1:
function setup() {
createCanvas(1000, 200);
background( 'green' );
var div = createDiv( '' ).size(200, 70);
div.html( 'Welcome to GeeksforGeeks' , true );
div.center();
div.style( 'font-size' , '24px' );
div.style( 'color' , 'white' );
div.style( 'border' , '1px solid white' );
div.style( 'text-align' , 'center' );
}
|
Output:
Example 2:
function setup() {
createCanvas(1000, 200);
background( 'green' );
var input_val = createInput( '' );
input_val.attribute( 'value' , 'Welcome to GeeksforGeeks' );
input_val.center();
input_val.style( 'font-size' , '24px' );
input_val.style( 'width' , '300px' );
}
|
Output:
Share your thoughts in the comments
Please Login to comment...