Creating a 3D cube using canvas in HTML5


We can create a 3D cube using the <canvas> element and JavaScript in HTML5. The following document defines a canvas element with a width and height of 400 pixels, and uses JavaScript to get the canvas context to draw on the canvas. The cube is drawn using three separate faces: the front face, the top face, and the right face. This element is helpful in rendering 2D and 3D graphics in web browsers.

Another alternate method to create a 3D cube is by using CSS 3D transitions, which allows us to create and animate the 3D cube, and WebGL which is a popular Javascript API used in rendering 3D graphics.

Algorithm

  • Get the canvas element and its context in an HTML document.

  • Define the properties of the cube: center position, size, and depth.

  • Draw the front face of the cube and style the element using CSS.

  • Draw the top face of the cube.

  • Complete by designing the right face of the cube.

Example

<!DOCTYPE html>
<html>
  <head>
    <title>3D Cube</title>
  </head>
  <body>
    <!-- Create a canvas element with id "myCanvas" and dimensions 400x400 pixels -->
    <canvas id="myCanvas" width="400" height="400"></canvas>

    <script>
      // Get the canvas element with id "myCanvas"
      var canvas = document.getElementById("myCanvas");

      // Get the canvas context to draw on the canvas
      var ctx = canvas.getContext("2d");

      // Define the properties of the cube: center position, size, and depth
      var x = canvas.width / 2;
      var y = canvas.height / 2;
      var size = 100;
      var depth = 100;

      // Draw the front face of the cube
      ctx.fillStyle = "blue"; // Set the fill color to blue
      ctx.fillRect(x, y, size, size); // Draw a filled rectangle at the center position with the given size

      // Draw the top face of the cube
      ctx.fillStyle = "lightblue"; // Set the fill color to light blue
      ctx.beginPath(); // Start a new path for drawing
      ctx.moveTo(x, y); // Move the pen to the center position
      ctx.lineTo(x + size, y); // Draw a line to the right edge of the front face
      ctx.lineTo(x + size + depth, y - depth); // Draw a line to the top right corner of the top face
      ctx.lineTo(x + depth, y - depth); // Draw a line to the top left corner of the top face
      ctx.closePath(); // Close the path
      ctx.fill(); // Fill the path with the current fill color

      // Draw the right face of the cube
      ctx.fillStyle = "darkblue"; // Set the fill color to dark blue
      ctx.beginPath(); // Start a new path for drawing
      ctx.moveTo(x + size, y); // Move the pen to the right edge of the front face
      ctx.lineTo(x + size, y + size); // Draw a line to the bottom edge of the front face
      ctx.lineTo(x + size + depth, y + size - depth); // Draw a line to the bottom right corner of the right face
      ctx.lineTo(x + size + depth, y - depth); // Draw a line to the top right corner of the right face
      ctx.closePath(); // Close the path
      ctx.fill(); // Fill the path with the current fill color
    </script>
  </body>
</html>

Conclusion

This code demonstrates how to create a 3D cube with the <canvas> element. However, canvas is a bitmapāˆ’based graphics API, which means that it draws pixels directly to the canvas, which can lead to performance issues.

Also, the graphics are not accessible to screen readers or other assistive technologies, so it's important to consider alternative methods for providing content to users with disabilities. These graphics can also be impacted by browser compatibility issues, so it's important to test your code across different browsers.

Updated on: 18-Aug-2023

867 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements