Liz
June 20, 2016
280

# A Foolish Quest: Auto-Generating Knitting Patterns with JavaScript

Taking something real-world and math-based, like knitting, and turning into a program is actually super easy (no one is surprised here). But! What happens when you combine that with best practices and expected conventions, along with industry-wide standards for design and presentation? Things get a lot more complicated than just crunching numbers, especially when your output will result in lots of manual hours creating a real object. This is the story of how I built a customizable knitting pattern generator in JavaScript (after I built it in Ruby first, of course), and the lessons learned when you try and do math for more than just math's sake.

June 20, 2016

## Transcript

1. ### LIZ ABINANTE PRESENTS her foolish quest to auto-generate knitting patterns

using JavaScript. @feministy • [email protected] Senior Software Engineer, New Relic

effort.
13. ### Everything you need to know about knitting patterns… in 2

minutes.* * ish
14. ### Important terms Gauge Number of stitches and number of rows

per <MEASUREMENT>

with
16. ### Fun quirks Knitting patterns usually come in both imperial and

metric measurements

for fit
18. ### Fun quirks …that are often distilled down to one measurement

for instructions & size selection

21. ### Orders of magnitude Even the simplest problem is incredibly difficult

to solve and they don’t get any easier

23. ### What even is a knitting pattern? A set of instructions,

schematics, photos, and charts to reproduce a given item with reasonable consistency

29. ### Other garments Any time you add people shaped things into

your math, it needs to be more precise than just math

estimates

40. ### let gauge = 19 let circumference = 19 let rawCastOn

= (gauge / 4) * circumference let roundDown = roundDownToFour(rawCastOn) let roundUp = roundUpToFour(rawCastOn) let castOn =
41. ### let gauge = 19 let circumference = 19 let rawCastOn

= 90.25 let roundDown = 88 let roundUp = 92 let castOn =

92 ||

you can be
44. ### let gauge = 6 let desiredSize = 28 let rawCastOn

= 42 let roundDown = roundDownToFour(rawCastOn) let roundUp = roundDownToFour(rawCastOn) let castOn =

44 ||
46. ### Problems of complexity The more complex your garment or motif,

the harder calculations become
47. ### let gauge = 19 let circumference = 23.5 let rawCastOn

= 111.6 let roundDown = roundDownTo13(rawCastOn) let roundUp = roundUpTo13(rawCastOn) let castOn =
48. ### let gauge = 19 let circumference = 23.5 let rawCastOn

= 111.6 let roundDown = roundDownTo13(rawCastOn) let roundUp = roundUpTo13(rawCastOn) let castOn =

117 ||

52. ### Imperial -> Metric Imperial numbers are larger units that translate

awkwardly to centimeters Rounding comes back to bite you, again

54. ### Metric -> Imperial Metric units are more precise and often

result in difficult-to-measure imperial numbers Rounding comes back to bite you, again

56. ### tl;dr Solve all of your unit conversion problems by doing

2 sets of calculations and simply choosing the best one
57. ### tl;dr An experienced designer can make a judgement call for

which results to use JAVASCRIPT CAN’T DO THAT

59. ### JavaScript can’t even Take the integrity of a design into

account when scaling up or down for different sizes
60. ### JavaScript can’t even Make modifications to large stitch motifs that

are otherwise unnoticeable but will make an awkward number work
61. ### JavaScript can’t even Make a decision about when to break

from an algorithm and do something different to make a size or design work
62. ### JavaScript can’t even Tell you if this is a bad

idea (or can it?)
63. ### If you’re thinking machine learning ok fine i get it

you’re right i’m wrong javascript can do anything yayyyy

71. ### @feministy • [email protected] Senior Software Engineer, New Relic slides ->

tinylinx.biz/nodepdx
72. ### Image credits Knit hat: https://unsplash.com/photos/hAobD5OjoHE Fire: https://www.pexels.com/photo/wood-explosion-fire-hot-8504/ Math equation: https://unsplash.com/photos/5mZ_M06Fc9g

Stack of knit hats: http://www.purlsoho.com/create/2014/10/22/ classic-cuffed-hat/ All other images are mine (insert evil laughter here)