Hi Joe,
I believe the issue is that the D3 v4 default build does not include
d3-selection-multi, which is required to pass objects into selection.attr, which actually will become selection.attrs.
The following line from your snippet works with version 3:
.attr({widht:w,height:h})
When using version 4, you can either say
.attr("widht", w)
.attr("height", h)
or load
d3-selection-multi on the page (in addition to the default d3.js v4 build, which does not include this) and say
.attrs({widht:w,height:h})
All the best,
Curran