Advertisement
NickNDS

Canvas Tag Example

Feb 16th, 2022
1,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.06 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Canvas Test</title>
  8. </head>
  9.  
  10. <body>
  11.     <!I had to put the canvas inside another container to center it>
  12.     <div style="text-align: center;">
  13.         <!
  14.            The canvas has the id "myCanvas" for the JavaScript to reference
  15.            The width and height are set to 500
  16.            A small border is drawn to show where the canvas is
  17.            The text within a canvas tag is only shown if the canvas tag is not supported by the browser or JavaScript is disabled
  18.        >
  19.         <canvas id="myCanvas" width="500" height="500" style="border: 1px solid black;">
  20.             If you can see this text, your browser does not support Canvas tags
  21.         </canvas>
  22.     </div>
  23. </body>
  24.  
  25. <!I put the script outside of the main body and it still works>
  26. <script>
  27.     //First, the JavaScript has to store the canvas element in a variable
  28.     var canvas = document.getElementById("myCanvas");
  29.     //The context for drawing objects is stored in another variable.
  30.     var ctx = canvas.getContext("2d");
  31.  
  32.     //I made a color gradient about 500px wide. Using a width of 400, part of the text appears too white to read
  33.     var grd = ctx.createLinearGradient(0, 0, 500, 0);
  34.     //Two colors are added. The first color is the purple from a GCU Lopes logo converted to hex
  35.     grd.addColorStop(0, "#462089");
  36.     grd.addColorStop(1, "white");
  37.  
  38.     //Instruct the context to use the color style from the gradient
  39.     ctx.fillStyle = grd;
  40.     ctx.font = "72px Times New Roman";
  41.     ctx.textAlign = "center";
  42.     //Center the text "GCU Lopes" approximately in the center. I offset the Y coordinate by 20 to further center the text
  43.     ctx.fillText("GCU Lopes", canvas.width/2, canvas.height/2 + 20);
  44.  
  45.     //Start a drawing operation
  46.     ctx.beginPath();
  47.     //Draw a circle
  48.     ctx.arc(canvas.width/2, canvas.height/2, 200, 0, 2 * Math.PI);
  49.     //Apply the circle to the canvas
  50.     ctx.stroke();
  51. </script>
  52.  
  53. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement