- HTML Canvas Tutorial
- HTML Canvas - Home
- HTML Canvas - Introduction
- Environmental Setup
- HTML Canvas - First Application
- HTML Canvas - Drawing 2D Shapes
- HTML Canvas - Path Elements
- 2D Shapes Using Path Elements
- HTML Canvas - Colors
- HTML Canvas - Adding Styles
- HTML Canvas - Adding Text
- HTML Canvas - Adding Images
- HTML Canvas - Canvas Clock
- HTML Canvas - Transformations
- Composting and Clipping
- HTML Canvas - Basic Animations
- Advanced Animations
- HTML Canvas API Functions
- HTML Canvas - Element
- HTML Canvas - Rectangles
- HTML Canvas - Lines
- HTML Canvas - Paths
- HTML Canvas - Text
- HTML Canvas - Colors and Styles
- HTML Canvas - Images
- HTML Canvas - Shadows and Transformations
- HTML Canvas Useful Resources
- HTML Canvas - Quick Guide
- HTML Canvas - Useful Resources
- HTML Canvas - Discussion
HTML Canvas - Direction Property
The HTML Canvas direction property uses CanvasRenderingContext2D interface context object can be used to change the text direction inside the Canvas element.
It can be used to specify the current text direction while adding text to the canvas.
Possible input values
The possible values taken by the property are −
S.No | Value & Description |
---|---|
1 | ltr
Text direction is from left to right. |
2 | rtl
Text direction is from right to left |
3 | inherit
The direction to draw the text onto the Canvas element is taken from the canvas element specifications as appropriate. |
Example 1
The following example uses the default 'inherit' direction to draw the text onto the Canvas element using HTML Canvas direction property.
<!DOCTYPE html> <html lang="en"> <head> <title>Reference API</title> <style> body { margin: 10px; padding: 10px; } </style> </head> <body> <canvas id="canvas" width="300" height="200" style="border: 1px solid black;"></canvas> <script> var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); context.font = "25px Verdana"; context.fillText('direction-', 150, 50); context.direction = 'inherit'; context.fillText('direction-', 150, 130); </script> </body> </html>
Output
The output returned by the following code on the webpage as −
Example 2
The following example draws text onto the canvas from right to left direction using the property direction of CanvasRenderingContext2D object.
<!DOCTYPE html> <html lang="en"> <head> <title>Reference API</title> <style> body { margin: 10px; padding: 10px; } </style> </head> <body> <canvas id="canvas" width="300" height="200" style="border: 1px solid black;"></canvas> <script> var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); context.font = "25px Verdana"; context.fillText('hello world-', 150, 50); context.direction = 'rtl'; context.fillText('hello world-', 150, 130); </script> </body> </html>
Output
The output returned by the above code on the webpage as −
Example 3
The following example draws text onto the canvas from left to right direction using the property direction of CanvasRenderingContext2D object.
<!DOCTYPE html> <html lang="en"> <head> <title>Reference API</title> <style> body { margin: 10px; padding: 10px; } </style> </head> <body> <canvas id="canvas" width="300" height="200" style="border: 1px solid black;"></canvas> <script> var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); context.font = "25px Verdana"; context.fillText('-hello world', 150, 50); context.direction = 'ltr'; context.fillText('-hello world', 150, 130); </script> </body> </html>
Output
The output returned by the above code on the webpage as −
To Continue Learning Please Login
Login with Google