JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://raw.github.com/documentcloud/underscore/master/underscore-min.js"></script>
<canvas id="blah" width=400 height=400></canvas>
CoffeeScript
{floor, sin, cos, atan2, sqrt, random} = Math
{extend} = _
tau = 2 * Math.PI
TRANSPARENT = 'rgba(0, 0, 0, 0)'
rand = (min, max) ->
[min, max] = [0, min] unless max
min + random() * (max - min)
randint = (min, max) ->
[min, max] = [0, min] unless max
floor rand min, max + 1
saving_context = (ctx, func) ->
ctx.save()
func()
ctx.restore()
reset_transform = (ctx) ->
ctx.setTransform 1, 0, 0, 1, 0, 0
clear_canvas = (ctx) ->
saving_context ctx, ->
reset_transform ctx
ctx.clearRect 0, 0, ctx.canvas.width, ctx.canvas.height
ink_path = (ctx, path_set_func, transform_stroke=false, clear_colors_before=true) ->
saving_context ctx, ->
if clear_colors_before
extend ctx, fillStyle: TRANSPARENT, strokeStyle: TRANSPARENT
path_set_func()
reset_transform ctx unless transform_stroke
ctx.fill()
ctx.stroke()
ctx.beginPath()
leaf_color = -> "rgb(0, #{randint 128, 191}, 0)"
trunk_color = -> "rgb(#{randint 64, 127}, 50, 25)"
branch_color = (depth) -> if depth <= 2 then leaf_color() else trunk_color()
branch_angle = -> rand -tau/8, tau/8
branch_length = -> rand .6, 1
branch_width = -> .7
branch_count = -> randint 1, 4
draw_tree = (ctx, recurse_depth) ->
ink_path ctx, ->
ctx.strokeStyle = branch_color recurse_depth
ctx.moveTo 0, 0
ctx.lineTo 0, 1
return unless recurse_depth--
ctx.translate 0, 1
for i in [0...branch_count()]
saving_context ctx, ->
ctx.rotate branch_angle()
ctx.scale (l = branch_length()), l
ctx.lineWidth = ctx.lineWidth * branch_width()
draw_tree ctx, recurse_depth
c1 = ($ '#blah')[0].getContext('2d')
saving_context c1, ->
c1.translate 200, 350
c1.scale 50, -50
extend c1, lineWidth: 6, lineCap: 'round'
draw_tree(c1, 9)