For example, given image like above, what I want to do is draw the exact same shaped polyline object on SVG(Im creating a 'drawing' or should I say 'brush' tool based on SVG and that is why Im using polyline so that user can paint with his mouse or can even use eraser with his or hers mouse). And following is how I would achieve this.
- draw the given image on canvas context.
- get all the coordinates of pixel that is colored #000000.
- with that list of coordinates create a Polyline on SVG.
and by this process I get this as a result doughnut drawin with svg polyline(now this is just an example result that it is formed ugly because I had to draw it manually with my hand. But my purpose is to get same shaped with an input image)
But I'm not sure if this is the only way or even not sure if I should stick with SVG. Are there any other good ways to achieve this? or would using canvas instead of SVG make it easier?
This is supposing that you already have an SVG path.
In order to draw a polygon you will need to split your path by the
M
commands. In the next example I did it manually but you can do it dynamically. This is important because otherwise you'll get a split in the polygon.You will also need to set a precision, meaning the distance between the points of the polygon.
Please read the comments in my code.
This shape can be drawn with circles.
Cutouts made using a mask composed of circles