draw a beaver in javascript

To draw a beaver in JavaScript, we can use the HTML5 canvas element. Here's the code to draw a simple beaver:

index.tsx
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

// Draw the beaver body.
ctx.fillStyle = "brown";
ctx.beginPath();
ctx.arc(100, 100, 50, 0, 2 * Math.PI);
ctx.fill();

// Draw the beaver head.
ctx.beginPath();
ctx.arc(50, 80, 30, 0, 2 * Math.PI);
ctx.fill();

// Draw the beaver nose.
ctx.fillStyle = "black";
ctx.beginPath();
ctx.arc(65, 80, 5, 0, 2 * Math.PI);
ctx.fill();

// Draw the beaver teeth.
ctx.fillStyle = "white";
ctx.fillRect(60, 85, 10, 5);

// Draw the beaver eyes.
ctx.fillStyle = "white";
ctx.beginPath();
ctx.arc(40, 70, 5, 0, 2 * Math.PI);
ctx.fill();
ctx.beginPath();
ctx.arc(55, 70, 5, 0, 2 * Math.PI);
ctx.fill();
ctx.fillStyle = "black";
ctx.beginPath();
ctx.arc(42, 68, 2, 0, 2 * Math.PI);
ctx.fill();
ctx.beginPath();
ctx.arc(57, 68, 2, 0, 2 * Math.PI);
ctx.fill();

// Draw the beaver tail.
ctx.fillStyle = "brown";
ctx.beginPath();
ctx.moveTo(150, 100);
ctx.quadraticCurveTo(170, 110, 150, 120);
ctx.quadraticCurveTo(140, 110, 150, 100);
ctx.fill();
1017 chars
48 lines

This code draws a brown circle for the beaver's body, a smaller brown circle for the head, and uses quadratic curves to draw the tail. It also draws the eyes, nose, and teeth in the appropriate places. However, this code draws a very simple beaver. You could add more detail to the beaver by adding fur or shading, for example.

related categories

gistlibby LogSnag