Advertisement
sourav8256

Untitled

Aug 4th, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 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.  
  22. // Function to draw rounded rectangle with text on canvas
  23. function drawRoundedRectangleWithText(x, y, text) {
  24. const width = 200;
  25. const height = 100;
  26. const cornerRadius = 20;
  27. const borderWidth = 2;
  28. const fontSize = 30;
  29.  
  30. context.strokeStyle = 'black';
  31. context.fillStyle = 'black';
  32. context.lineWidth = borderWidth;
  33. context.font = `${(fontSize * 0.7)}px Arial`; // Reduce the text size by 30 percent
  34. context.textAlign = 'center';
  35. context.textBaseline = 'middle';
  36.  
  37. context.beginPath();
  38. context.moveTo(x + cornerRadius, y);
  39. context.arcTo(x + width, y, x + width, y + height, cornerRadius);
  40. context.arcTo(x + width, y + height, x, y + height, cornerRadius);
  41. context.arcTo(x, y + height, x, y, cornerRadius);
  42. context.arcTo(x, y, x + width, y, cornerRadius);
  43. context.closePath();
  44. context.stroke();
  45.  
  46. context.fillText(text, x + width / 2, y + height / 2);
  47. }
  48.  
  49. // Mouseup event listener
  50. canvas.addEventListener('mouseup', function(event) {
  51. const rect = canvas.getBoundingClientRect();
  52. const x = event.clientX - rect.left;
  53. const y = event.clientY - rect.top;
  54. drawRoundedRectangleWithText(x, y, 'Custom Text');
  55. });
  56. </script>
  57. </body>
  58. </html>
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement