+ 3
How to create an array of shapes(e.g. circles, rectangles) using just javascript?
6 Answers
+ 6
Oh. If you mean draw shapes then you need canvas. To be able to draw using JavaScript, you need HTML as well
Check the lessonđ
https://www.sololearn.com/learn/HTML/2201/
+ 5
var shapes = ["circle", "square", "triangle", "rectangle"];
+ 5
svg
+ 4
i mean to create shapes like rectangle, circle in browser using JS
+ 4
thank you for help đ
+ 4
create canvas element
and define id
ex: id="canvas1"
canvas default size is width 300 height 150
move to js and get canvas id
and create var for getContext
var can = document.getElementById('canvas1')
var ctx = can.getContext('2d')
now we can draw in canvas
ex: circle
var x = 50;
var y = 50;
var r = 100;
ctx.beginPath();
ctx.arc(x,y,r,0,360,false)
ctx.fillStyle ='red';
ctx.fill();
ctx.stroke()
ctx.closePath()
x and y is move the center of circle to x coordinates and y coordinates
r is for radius of circle
0 is for start of circle
360 is for end of circle
0,360 is full circle
0,180 is half circle
0,90 is half of half circle
if you want to draw circle in counter clockwise set false to true
fillStyle is define a color of circle
default is black
stroke is for stroke of circle
u can set the stroke width
ex : ctx.lineWidth = '10'
closePath is for end the drawing
copy paste this example
to see the result