Drawing one completed form with different stroke styles

Is it possible to draw one single filled figure on a canvas with different stroke styles?

In other words, can I draw a blue triangle and have a different color on each side?

I understand there is a less optimal solution:

  • - a new path, draw a triangle without strokes, fill it, then close the path
  • start a new path, redraw the first side with the first color stroke, close the path
  • start a new path, redraw the second side with a different color stroke, close the path
  • start a new path, redraw the third side with a different color stroke, close the path

In short, draw a completed form without strokes, then redraw each side with a unique stroke style.

This is not very important for a single triangle, but if you have many more complex shapes, it seems ineffective.

So - is it possible to draw one shape with different stroke styles across different segments of the path?

+5
source share
1 answer

The short answer is no, not in an inline way. The reason is that with every new call stroke()it will process all the way, and not just not all the non-primary parts.

You can always make your own function, which, say, takes a bunch of points or types of segments (you have to create your own class), giving each a color, and this function analyzes them and makes a drawing, But it's the same as you.

, , , . , , :)

(... , ctx.linecap = 'round'

+4

All Articles