Advertisement
sourav8256

Untitled

Aug 4th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Rounded Border and Text on Mouseup</title>
  7. <style>
  8. #canvas {
  9. display: block;
  10. background-color: #f0f0f0;
  11. margin: 20px auto;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <canvas id="canvas" width="400" height="200"></canvas>
  17.  
  18. <script>
  19. const canvas = document.getElementById('canvas');
  20. const context = canvas.getContext('2d');
  21. const elements = []; // Array to store drawn elements
  22.  
  23. // Function to draw rounded rectangle with text on canvas
  24. function drawRoundedRectangleWithText(x, y, text) {
  25. const width = 200;
  26. const height = 100;
  27. const cornerRadius = 20;
  28. const borderWidth = 2;
  29. const fontSize = 30;
  30.  
  31. context.strokeStyle = 'black';
  32. context.fillStyle = 'black';
  33. context.lineWidth = borderWidth;
  34. context.font = `${(fontSize * 0.7)}px Arial`; // Reduce the text size by 30 percent
  35. context.textAlign = 'center';
  36. context.textBaseline = 'middle';
  37.  
  38. context.beginPath();
  39. context.moveTo(x + cornerRadius, y);
  40. context.arcTo(x + width, y, x + width, y + height, cornerRadius);
  41. context.arcTo(x + width, y + height, x, y + height, cornerRadius);
  42. context.arcTo(x, y + height, x, y, cornerRadius);
  43. context.arcTo(x, y, x + width, y, cornerRadius);
  44. context.closePath();
  45. context.stroke();
  46.  
  47. context.fillText(text, x + width / 2, y + height / 2);
  48.  
  49. // Store the element information in the elements array
  50. elements.push({ x, y, text });
  51. }
  52.  
  53. // Mouseup event listener
  54. canvas.addEventListener('mouseup', function(event) {
  55. const rect = canvas.getBoundingClientRect();
  56. const x = event.clientX - rect.left;
  57. const y = event.clientY - rect.top;
  58. drawRoundedRectangleWithText(x, y, 'Custom Text');
  59. });
  60.  
  61. // Function to print the stored elements to console (for demonstration purposes)
  62. function printElements() {
  63. console.log(elements);
  64. }
  65. </script>
  66. </body>
  67. </html>
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement