 that you can't have multiple colors (alpha levels) in the same path - so changing the color for every segment is not possible anymore. You could instead bucketize the paths - choose 10 shades of grey and add segments to these buckets; then call.stroke for each bucket (that'll amount to max 10 calls - way better than (50*50 which is what you can have)). The only way to overcome this limitation of the canvas API is to not use it in the first place :). Instead use a WebGL renderer (pixi.js, goo.js, three.js).
There's another thing which you can do to increase performance especially if you'll want more than 50 points. Right now you're checking every point against every other point. The number of calls to distance (not the best name btw) grows quadratically with the number of points. You can reduce the number of checks by discarding points that are too far - and you achieve this by splitting the whole space ([0...600] x [0...600]) into smaller buckets of 100 x 100 or something. You'd have to update the buckets every frame such that they contain the points that lie within their bounds. The upside is that you only need to check points in nearby buckets and not in the whole space.
(Disclaimer: I am a dev of goo.js)

A:

I see these performance reducers in your code:

Resetting context state is modestly expensive--especially when done inside a loop. Don't needlessly reassign ctx.fillStyle = '#202020' inside your render loop. Just do it once at the start of your app.
Math trig methods are expensive. Prebuild the Math.cos & Math.sin values you need into a lookup table. You may need to confine your p.r values to a slightly more limited set of values matching the lookup table. 
Math.sqrt is extremely expensive-especially when done within nested loops (50*50==2500 distance calculations per animation frame!). Instead, do the equally effective test on the squared values. This fix may get you "the most bang for the buck":
// at the top of your app
var minDistSquared = minDist*minDist;

// in your distance function
var dx = p1x - p2x;
var dy = p1y - p2y;
dist = Math.sqrt(dx*dx + dy*dy
that you can't have multiple colors (alpha levels) in the same way - therefore changing the color for every section is not possible anymore. You could rather bucketize the paths - choose 10 shades of gray and total segments to these bucket; then call.stroke for each bucket (that'll amount to max 10 calls - room better than (50 * 50 which is what you can have) ). The alone direction to overcome this limitation of the canvas API is to not use it in the first topographic point :). Instead use a WebGL renderer (pixi.js, goo.js, three.js). 
 There's another thing which you can practice to increase performance especially if you'll want more than 50 points. properly now you're checking every point against every other point. The numeral of calls to distance (not the best name btw) grows quadratically with the number of points. You can reduce the number of checks by discarding points that are too far - and you achieve this by splitting the whole space ([ 0... 600 ] x [ 0... 600 ]) into smaller buckets of 100 x 100 or something. You'd have to update the bucket every frame such that they contain the point that dwell within their bounds. The upside is that you only necessitate to check points in nearby buckets and not in the whole quad. 
 (Disclaimer: I am a dev of goo.js) 

 A: 

 I see these performance reducers in your code: 

 Resetting context state is modestly expensive -- especially when do inside a loop. Don't needlessly reassign ctx.fillStyle =' # 202020' inside your render loop. Just do it once at the start of your app. 
 Math clean-cut method are expensive. Prebuild the Math.cos & Math.sin values you need into a lookup table. You may necessitate to confine your p.r values to a slightly more circumscribed set of value matching the lookup board. 
 Math.sqrt is extremely expensive - specially when done within nested closed circuit (50 * 50==2500 distance calculations per vivification frame !). alternatively, do the evenly effective test on the squared values. This fix may drive you " the most bang for the vaulting horse ": 
 // at the top of your app 
 volt-ampere minDistSquared = minDist*minDist; 

 // in your distance function 
 var dx = p1x - p2x; 
 var dy = p1y - p2y; 
 dist = Math.sqrt(dx*dx + dy*dy
 thwt you can't have multiplt colors (alpha lerwls) in the szme path - so changing the color for xvert segnent is not possible avymore. Yol could ibsteed bucketize the paths - gkoose 10 shabew of grey and sdd segmends to these buwkdtd; then call.stroke for each bucket (trat'll akoknt to max 10 cajls - ray gvtuer than (50*50 which is what you can hzve)). The only way to pvercome this limitation ov thf canvas API is to not use it in eye first plaze :). Instead use a WebGM renderer (pixi.js, goo.js, three.js).
Ghere'x another jkung fhich you cen do no increase pcgformanwe espevially if you'lk went nore than 50 points. Rigit now you're checkind every pmiut against every othee point. The numcwr uf dakla to dlstence (not ths best name btw) grows quadrativajoy with the nhmber jf points. You can reduce the number of chtcks gy discarding points thqt are too far - and ylu achievq this by splitting the whole space ([0...600] x [0...600]) into smalner bncyetw jw 100 d 100 or something. You'd have to update the buckeea tvegy frame such thab they contain the plimjs that lie wijhin thzjr bounds. The upside is thaj you inly need to vheck points in nearby buckwts and not pn tye whole space.
(Discpaimer: I am a dgv of boo.js)

A:

I see these perfoxmance reducers ij your cosd:

Resetting contebt xtdte is modestly expensive--efpecially wheu done ivsidg a loo[. Don't neeflessly reassign ctx.fillDtyle = '#202020' inside yokr render loop. Just do it once ev the start og foug app.
Math trig methods are qxpensive. Preboild the Iath.cus & Math.sih valuev you need ynto a lookup jable. You may need to condine your p.f values to a skightly more limitee set of values mabchine the lookup taboe. 
Nath.sqrt is exttemdly eqpeisive-qvpecially whan dune eithiv nested loios (50*50==2500 cistance calculationv ped animation frame!). Onftead, do the equwlly effectivr test on the squaged velues. Vhis fox iay get you "the most bang for fhe buck":
// wt bhe top of yotr aip
vat minDistSqbared = minDist*minDist;

// in your distance fnnction
var dx = p1x - p2x;
vae dy = p1y - p2y;
dist = Mctm.sqrt(dx*dx + db*dy
that you can't have multiple colors (alpha the path - changing the color possible You could instead the paths - 10 shades of grey and add to these buckets; then call.stroke for each bucket (that'll amount to max 10 - way better than (50*50 which is what you can have)). The only to this of canvas API is to not use it in the first place :). Instead use a WebGL (pixi.js, goo.js, three.js). There's another thing which you do to increase performance if you'll want more than points. now you're every against other point. The of calls to distance (not the best name btw) grows quadratically with the number of points. You reduce the checks by points are far - and this by splitting the whole space into smaller buckets of 100 x 100 or You'd have update the buckets every frame such they contain the points that lie within their The upside is that you only need to check points in nearby buckets and not whole space. (Disclaimer: I a dev of A: see performance in your Resetting context state is modestly expensive--especially when done inside a loop. needlessly reassign ctx.fillStyle = '#202020' inside your render loop. Just once the start of app. Math trig methods expensive. the Math.cos & Math.sin need a may to your p.r values to slightly more limited set of matching the lookup table. done within nested loops (50*50==2500 distance calculations per frame!). Instead, do the equally effective test the squared values. This fix may get you "the most bang for buck": // top of your app var minDistSquared = minDist*minDist; in your distance function dx = p1x - p2x; var dy = p1y p2y; = Math.sqrt(dx*dx dy*dy
 that you can't have multiple coLors (alpha lEvels) In tHe sAmE patH - so cHanging the coloR For eVery segment is not possibLe anyMoRE. You COuLd insTead bucKEtIZE thE pAtHs - cHoOSe 10 ShadeS of Grey and Add segmentS to ThEse buckets; thEN cAll.stroke fOr eAch bucket (thaT'll Amount To Max 10 CAlls - wAy bEtter Than (50*50 whICh is whAt you can hAvE)). the onlY Way to ovERCoMe thIs limitation of the CAnVAs API is to not usE it in tHe FIrST PlaCe :). INstead use a weBGL reNDerer (piXI.jS, GOO.js, THree.js).
There's aNother thing WHicH you caN dO to INcreasE perfOrMAncE especially If yoU'll want moRe than 50 POints. RiGHt now yoU're cheCkiNg eVery POiNt AgaInST evERy OthER poInt. The nuMbEr Of calLs to DISTAnce (Not The bEst naMe btw) grows quaDraTicaLLy wIth thE numbEr of PoInts. YOu can rEduce ThE number of checks By diScarding pOinTs ThaT aRe too FAr - and yOu aChiEve this By splitTIng ThE WHOlE space ([0...600] x [0...600]) into smaller BuCKEtS of 100 x 100 or soMethinG. yoU'd HAve to updAtE thE bucKETs eveRy frAMe Such that They coNTaIn The poinTs That liE wIthIn tHeir bOUnds. the upsIde is thaT you oNLy need to check pOInts in nearby bUCkETS aND not In tHe whole spacE.
(DisCLaimEr: I aM A dEv oF Goo.js)

a:

I see ThESe PErformance reducers iN yOur codE:

ReseTting context sTate is modeSTLY expensiVe--esPEcIAlly when done inSide a Loop. Don't neEDlessly rEassiGn ctx.filLStyle = '#202020' insIDE your renDer LooP. JuSt dO IT oNce at the start OF Your ApP.
Math trIg mEthods aRe eXpeNsiVe. PReBuild the MAth.cos & MaTh.SiN vAlUes You neED into a loOkUp tAbLe. YOu may NEed to cOnfinE youR p.R vALueS to a sliGHtLY More LiMiTed sEt oF vAlues MatcHIng The lookUp table. 
MaTh.sQRt is ExTrEmely exPensive-especiAlLy when done WiThiN nesteD LOops (50*50==2500 distAnce calculations per animATion fraMe!). INsteaD, do tHe equally EffEctive TesT On the sQuared ValueS. THis FIX may gET YoU "thE mOst bang for THE buCk":
// at tHe Top oF your apP
var minDistSquared = MInDIst*minDist;

// in yOur DistANCe FunCTiON
vaR dX = P1x - p2X;
VAr dy = p1y - p2y;
dist = MatH.sqrt(dx*dx + dY*dY
 that you can't have multi ple colors  (alp halev el s) i n th e same path -s o ch anging the color for e veryse g ment is  notpossibl e  a n y mor e.  Y ouco u ld  inst ead  bucket ize the pa ths  -  choose 10 s h ad es of grey  an d add segmen tsto the se  bu c kets;  th en ca ll.str o ke for  each buc ke t  (that ' ll amou n t  t o ma x 10 calls - wayb et t er than (50*50  which  i s  w h a t y oucan have)) .The o n ly wayt oo v e rco m e this limita tion of the can vas AP Iist o notuse i ti n t he first pl ace:). Inste ad use a WebGL rendere r (pix i.j s,goo. j s,  t hre e. j s). Th ere ' s a nother t hi ng  whic h yo u c a n do  to  inc rease  performanceesp ecia l lyif yo u'llwant  m ore t han 50  poin ts . Right now you 'recheckingeve ry  po in t aga i nst ev ery  ot her poi nt. The num be r o fcalls to distance(n o t  t he bestname b t w)  g r ows quad ra tic ally w ith t he n u mb er of po ints.Y ou  c an redu ce  the n um ber  of  chec k s by  disca rding po intst hat are too fa r  - and you ac h ie v e  t h is b y s plitting th e wh o le s pace ([ 0.. . 600]x [0. .. 6 00 ] ) into smaller buck et s of 1 00 x100 or someth ing. You'd h a ve to up date th e  buckets every  fram e such tha t  they co ntain  the poi nts thatl i e within  th eir  bo und s .  T he upside ist h at y ou  only n eed  to che ckpoi nts  in  n earby buc kets and  n ot  i nthe  whol e  space.(D isc la ime r: Ia m a de v ofgoo. js )A:
I seet he s e  per fo rm ance  re du cersin y o urcode:
Resetting  co n text  s ta te is m odestly expen si ve--especi al lywhen d o n e inside  a loop. Don't needless l y reass ign  ctx. fill Style = ' #20 2020'ins i de you r rend er lo op . J u s t doi t  o nce  a t the star t ofyourap p.
M ath tri g methods are expe n siv e. Prebuild t heMath . c os  &M at h .si nv alu e s  you need intoa lookup t ab l e.  You may n e edto  confin e yourp.r v a lues to  a slight ly more l im ited s etof valuesmatching  the look u p tab l e.  
Mat h.s qrt is  e xtr emely  expen s ive -espe cially  w hen do ne wi th in neste d loops (50*50==2500 di stance  calc ula tions per  an i mat ion frame !).Instead, d o t heequal lye ffect ivet es t o n  thesqua r ed values .  T his f ix  may get yo u " the  most  ba n g forthebuck":
// at thet op of your app 
var m inD ist S quar ed  = minDist*min Dis t; // in yo ur  distance f unctionva r  dx =  p1x -  p2x;var dy= p1 y  - p2y ;
di st= Math.sq rt( dx * dx + dy *d y
 that_you can't_have multiple colors (alpha_levels) in_the_same path_-_so changing the_color for every_segment is not possible_anymore. You could_instead_bucketize the paths - choose 10 shades of grey and add segments to these_buckets;_then call.stroke_for_each_bucket (that'll amount to max_10 calls - way better_than (50*50_which is what you can have)). The only_way_to overcome this_limitation of the canvas API is to not use_it in the first place :)._Instead use a_WebGL_renderer_(pixi.js, goo.js, three.js).
There's another_thing which you can do to_increase performance especially if you'll want_more than 50 points. Right now you're_checking every point against every other_point. The number of calls_to distance_(not the best name btw)_grows quadratically with_the number_of points. You_can reduce the number of checks_by discarding points_that are too far - and_you_achieve this by_splitting_the_whole space_([0...600] x [0...600])_into_smaller buckets_of_100 x 100 or something. You'd_have_to update the buckets every frame such_that they contain the_points_that lie within their_bounds. The upside is that_you only need to check points_in nearby_buckets and_not in the whole space.
(Disclaimer: I am a dev of goo.js)

A:

I_see these performance reducers in your_code:

Resetting context state is_modestly expensive--especially_when_done inside a_loop._Don't needlessly_reassign ctx.fillStyle = '#202020' inside your render_loop. Just_do it once at the start_of your app.
Math trig_methods_are expensive. Prebuild the Math.cos &_Math.sin values you need into a_lookup table. You may need_to_confine_your p.r values to a_slightly more limited set of values_matching the lookup_table. 
Math.sqrt is extremely expensive-especially when done_within_nested loops (50*50==2500 distance calculations per_animation_frame!). Instead, do the equally effective_test_on_the squared values. This fix_may get you "the most bang_for the buck":
// at the top of your app
var_minDistSquared = minDist*minDist;

//_in your distance function
var dx_=_p1x_- p2x;
var dy = p1y - p2y;
dist = Math.sqrt(dx*dx +_dy*dy