To create a pumpkin in JavaScript, we can use the canvas
element, which is an HTML tag that allows us to draw graphics on a web page using JavaScript. Here's an example code:
545 chars26 lines
In this code, we first create a canvas
element with an ID of myCanvas
. We then retrieve the canvas element using JavaScript and get its 2D drawing context using the getContext
method.
We use the fillStyle
property of the context object to set the color of the pumpkin body and stem.
To draw the body of the pumpkin, we use the arc
method, which draws a circular arc. The first two parameters are the coordinates of the center of the circle, the third parameter is the radius, and the last two parameters specify the start and end angles of the arc, in radians.
To draw the stem, we use the moveTo
and lineTo
methods to specify the stem's shape as three connected lines.
When we run this code, we should see a pumpkin drawn on the canvas element.
gistlibby LogSnag