To draw text with a line on a canvas, you can first set the font style, size, and color using the context's font property. Then, use the context's fillText() method to write the text on the canvas at a specified position. You can also use the context's strokeText() method to draw the text with an outline. To do this, set the context's strokeStyle to the desired line color and use the strokeText() method to draw the text with a line border. This will create text with a line on the canvas.
How to draw text over a line on canvas?
To draw text over a line on a canvas, you can use the following steps:
- First, create a canvas element in your HTML file:
1
|
<canvas id="myCanvas" width="500" height="200"></canvas>
|
- Get the canvas context using JavaScript:
1 2 |
var canvas = document.getElementById('myCanvas'); var ctx = canvas.getContext('2d'); |
- Draw a line on the canvas using the beginPath(), moveTo(), and lineTo() methods:
1 2 3 4 |
ctx.beginPath(); ctx.moveTo(50, 50); ctx.lineTo(450, 50); ctx.stroke(); |
- Draw text over the line using the fillText() method. You can specify the position and properties of the text, such as font size and color:
1 2 3 |
ctx.font = '14px Arial'; ctx.fillStyle = 'black'; ctx.fillText('Text over line', 220, 40); |
- Finally, you can customize the text further by changing the font size, font style, and text alignment as needed. You can also add additional text over the line by using the fillText() method again.
Here is an example of drawing text over a line on a canvas:
1 2 3 4 5 6 7 8 9 10 11 |
var canvas = document.getElementById('myCanvas'); var ctx = canvas.getContext('2d'); ctx.beginPath(); ctx.moveTo(50, 50); ctx.lineTo(450, 50); ctx.stroke(); ctx.font = '14px Arial'; ctx.fillStyle = 'black'; ctx.fillText('Text over line', 220, 40); |
This code will draw a line on the canvas from the point (50, 50) to (450, 50) and write the text "Text over line" above the line at the position (220, 40).
How to draw text with a shadow on canvas?
To draw text with a shadow on a canvas in HTML5, you can use the shadowColor
, shadowBlur
, and shadowOffsetX
and shadowOffsetY
properties of the CanvasRenderingContext2D
object. Here is an example code snippet that demonstrates how to draw text with a shadow on a canvas:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Text with Shadow on Canvas</title> </head> <body> <canvas id="myCanvas" width="400" height="200"></canvas> <script> const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); // Set the shadow properties ctx.shadowColor = 'black'; ctx.shadowBlur = 5; ctx.shadowOffsetX = 2; ctx.shadowOffsetY = 2; // Set the font properties ctx.font = '30px Arial'; ctx.fillStyle = 'red'; // Draw the text with shadow ctx.fillText('Hello, World!', 50, 80); </script> </body> </html> |
In the code above, we first set the shadow properties for the canvas context using the shadowColor
, shadowBlur
, shadowOffsetX
and shadowOffsetY
properties. Then, we set the font properties using the font
and fillStyle
properties. Finally, we use the fillText
method to draw the text with a shadow on the canvas. You can adjust the values of the shadow properties to achieve the desired shadow effect.
How to rotate a line on canvas?
To rotate a line on a canvas, you can use the following steps:
- Define the initial coordinates of the line on the canvas. Let's say the line starts at point (x1, y1) and ends at point (x2, y2).
- Calculate the center point of the line by finding the average of the x and y coordinates of the starting and ending points. Let's call this point (cx, cy).
- Calculate the angle by which you want to rotate the line. You can do this using trigonometric functions or by providing an angle value.
- To rotate the line around its center point, translate the canvas to the center point (cx, cy), rotate the canvas by the desired angle, draw the line from the translated starting point to the translated ending point, and then translate the canvas back to its original position.
Here's an example in JavaScript using the HTML5 canvas element:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); const x1 = 100; const y1 = 100; const x2 = 200; const y2 = 200; const cx = (x1 + x2) / 2; const cy = (y1 + y2) / 2; const angleInRadians = Math.PI / 4; // rotate 45 degrees // Translate to center point ctx.translate(cx, cy); // Rotate canvas by angle ctx.rotate(angleInRadians); // Translate line coordinates to rotated position ctx.beginPath(); ctx.moveTo(x1 - cx, y1 - cy); ctx.lineTo(x2 - cx, y2 - cy); ctx.stroke(); // Reset transformations ctx.rotate(-angleInRadians); ctx.translate(-cx, -cy); |
This code will rotate a line by 45 degrees around its center point on the canvas. You can adjust the initial coordinates and rotation angle to suit your needs.