Um, mynameisJeremyWagner, and I'm a wenperformanceconsultantforsightimproved.
Myjobistodoall I cantomakethewebfasterforourclients.
And I alsowriteandtalkaboutperformance a bit.
I'm justrepeatingeverything.
Uh, thatarewonderful.
EMChasalreadysaid, butthistalkisaboutpaintworklips, whichis a littleweirdformebecause I wastalkingaboutperformancewhichhascomparablylittletodoperformance.
Andwhenthisallcomestogether, a paintworklitfinallybecomesusablebyCSS.
Buthowdowedrawstuff?
Whatdoesthatevenlooklike?
Well, theAP I youuseconvenientlytodrawstuffisreallyjustthecampusAPeyestwo D drawingcontext, somethingthat's beenaroundfor a verylongtimethatsomeofushavegoodfamiliaritywith, andthatmakessense.
Why?
Whydevelop a separate a p I fordrawingintwo D when a perfectlygoodoneislikerightthere?
Andinthismadlyresponsiveworldthatwelivein, wewanttomakesurethatourartworkadaptstothecontainerthatit's in.
Sohere's anexampleof a minimallyviablepaintmethod, whichdraws a solidblackcirclewith a 64 pixelradiussmackdabinthemiddleofthecanvas, nomatterwhatthesizeofthatcanvases.
AndsoifweassumethissuperbasicpaintworklitgetsregisteredunderthenotatallironicnameofAmazingandweApplyittoanelementwillgetthisthistotallyamazingthingthatinnowaycouldbereplacedby a staticimagewhatsoever.
Sothismightbeyourightaboutnow.
Butthat's simplistic.
ExamplewasmeanttoillustratehowwegetfromonelineofCSStosomethingthatletsyougeneratebackgroundimagesusing a familiarAP I thereissomuchmorethatthistechnologyiscapableof, solet's take a lookatitandjustthinkwhatifyoucouldusethis a p I tocreaterandomlygeneratedartworkthatcouldenhanceyourdesignsandpracticalyetspontaneouswaysthatarejust a littlebitdifferent.
Youknow, a goodframeofmindtogetintoistothinkofthecanvaseslike a setoftileswhereallthetilesareseparatebutpotentiallyoverlappingorconnectedpieceswhicharerandomlygenerated.
Andthat, tome, islikethemagicalpartofWebdevelopmentwithweirdlypracticalstuff.
Now, someofwhatyou'regoingtoseehereand I feelobligedtocallthisoutisverymuchinspiredby a wonderfultalkthatTimHolmangave A J S conyoulastyearwherehetalkedaboutgenerativearen't usingthecanvas a P I.
I highlyrecommendyoucheckitoutonYouTubebecauseheiswherebetteratthisstuffthan I am, andprettymucheverythinginthattalkissomethingthatyouwillthenbeabletoapplytopaintwork.
Let's intheCSSpaint a p I.
Sodocheckitout.
Okay, let's revisitthatpaintmethodintheamazingpaintworklitclassandjustmaybechangethingsup a littlebit.
There's moregoingoninthisexample.
Then, whenwewerejustdrawingthismundane, lonelycircleinthemiddleof a bleakcanvas.
Soineveryiteration, I callthecontextobjectsbeginpathmethodtobegindrawing a line, andthen I leaveittomassdotrandomormathdotpseudorandominmyrighttodecideifthelinewillcrossthetilespacefromtheupperleftcornertothelowerlowerrightcornerordraw a linestraightlinefromtheupperleftcornertothelowerleftcorner.
Andafterthat, I finishupHillaryinorationbytellingthecontextobjecttodrawthestroke.
Sowhatdoesthatlooklike?
Turnsout, itlookslike a kindofcoolthingthatchangeseverysingletime.
Andthat's justwhatyoucandoifyouonlyknow, likejust a teenylittlebitoftheofthecanvas, a p I.
Butwith a littleingenuity, thatlittleteenybitofingenuitycangoextremelyfarlikewhatifwejustdecidedtothrowin a littlesomethingextraandrandomlydecidewhetherornottodraw a dotintheupperlefthandcornerofeachtile.
Thisgivesthefinalartwork a sortofcircuitboardappearancewhich I thinkiskindofmeat, andwecanalsomakethetilesize a bitbiggerandwecanchangethecolor.
Andbecause a campusbackgroundistransparentbydefault, wecanspecify a CSSbackgroundcolorpropertytosettosetthisartworkonwhatevercoloritiswewant.
So I'vekindofdonelikethisoffblackgreyishthing.
Nowimaginethispatterninthepagesmastheadbackground, butmaybeat a moresubtleandloweropacitythatjustgivesthedesignjust a littlebitofextrahoofandlikechaosbutwithoutoverpoweringtheforegroundandlikemakingthingslike, youknow, justkindofsubtle, likebeingcool.
Uh, sothatwascool.
Butlet's do a differenttakeonthisideaandlet's let's experiment a littlebitmorelikewhatelsecanwedo?
Sohere's thepaintwork I callblotto, whichdrawscirclesofvaryingsizeandcapacityandeverytilelike, kindofaninkblotkindofthing.
Andsothere's a bitofcodehere, solet's justwalkthroughitattheverybeginning.
Andthenwedefinethisamplitudevariable, whichaidsusinsortofgeneratingrandomlysizecircleslateron, andthenwecalculatethenumberoftilesonbothaxesandthenwestoretheresultofpitimestwo, whichmeansthatthecirclethatwedrawwillbe a fullcircle a 360 degrees.
Butlet's, like, stepintotheworldoftrigonometry, and I'm suremostofyouareprettycomfortablewithTrig.
Butifnot, don't worry, it's it's nottoohard.
And I neverwas a goodmathstudent.
Solike I wasbrushinguponmyTriglastweek, here's anotherpaintworklit I callflashy, whichrandomlydrawstylizedraysoflightsorryaround a circlewhichflareoutward, whichmakesitsortoflookedlike a stylizedrenderingofthesun, which I thinkiskindofcool.
Representedbyareincrementaler I toradiance, andfromtherewillthendrawthispolylineshapethatuses a trigonometryliketotrigonometryfunctionstofigureoutthe X and Y coordinatestodrawthelinesfor E trade.
Weregisterthatflashylittlefellaandthen, likebeforeweuseitintheCSSlikethisandgiveit a nicepinkishbackgroundcolortositon.
Andwhatdoyouthinkthatlookslike?
Somethingprettycool?
I thinkright.
Itlookskindofneat.
And a littlebitofMaskellwas a longwayintomakingthislookreallycoolinstylishandlikeBeforewestillgetthatspontaneitythatgivesitthatfamiliarbutslightlydifferentappearanceeachandeverytimethatitdraws.
SoAndasanaside, I suckedatmathinschoollikejust, like, barelyscrapedbythroughalgebratosucked.
Soif I couldmakestufflikethisprettymuchanyonecanmakestuffthatlooksreallycoolwithjust a littlebitoflike, youknow, evenjustsomebasicarithmeticcanhelpyoumakesomereallycoolstuffinthis.
Andsocaseinpointlikethispaintworkwhat I liketocallSlapDash, thisoneiskindofoneoftheuserwantstomake, and I useitonmywebsitecurrently.
Butthat's okay, like, because I'vegotsomeothersupmysleevelikethisone, which I liketocallBumpybecauseit's getit.
It's bumpy.
Andthenthere's thisalternaterenderingoftheblottopaintworklit, which I showedyouearlier, butwithjustsortof a largerradiusonthecircleswith a blendmodeappliedtogiveit a sortofinkblockgonewildeffect.
Thefirstistoadd a staticgettermethodtoyourworklitclasscalledInputProperties, whichreturnsanarrayofallthecustompropertiesthatwe'veregisteredandthenintheworklit.
It's anentirelydifferentthingtocontrolitinthisfashion, andyoucandoitwithouthaving a tweakmorethan a coupleproperties.
Sonowthat I'veshowedyouallthecoolstuff, it's time I informyouoftheunfortunateyetentirelypredictablerealitythatyoumusteatyourvegetables.
Bywhich I meanit's timetotellyourpainttotellyouthatpaintworklet's don't havesolidbrowserssupportandthattherefore, thatmeansyoumusttreatthemas a progressiveenhancementandyou'llneed a backupplan, right?
Sothat's a lotofwhatwedoontheWebisfeellikemyofficialtitleislike a backupplanguyforeverything.
Thisismypersonalwebsiteinchromewiththepaintworkletusedinthebackgroundsofdifferentsections, justlikethatkindofslapdashpaintworklookthat I had.
Andnowhereitisn't a browserthatdoesn't andit's notexactlythesame, butit's a reasonable, likekindofapproximation, anditworkswellenoughandjustbesurethatyou'reoptimizingyourfallbackimages.
Youmightrecallthatinordertolowerthepaintworkleft, wehaveaccessto a promisewhenitcompletelyloadsorconversely, whenitfailstoload.
AndinthiscasewecancompensateforthisinthePromisescatchmethod, whichexecutesifthepromisewasrejectedinthiscase, ifthepaintworkfailedtoloadforsomereason, andifitdoesfail, wecanadd a classthebodyelement, whichwecanthentargetwithCSStoprovide a fallback.
That's theSSwouldlooksomethinglikethis, althoughthat's a littleweird.
Instead, intheeventthatthepaintworklookfailstoload, wewouldstillget a stallbackBrackeronimagethatwecanstilluse.
Andit's importanttonotethat, atleastinchrome, if a paintworklitfailstoload, theCSSonLeefallbackfornonsupportingbrowsersdescribedearlierwillnotkickinbecausethatit's stillconsidersthatpaintsintaxtobe a validvalue.
Butthatworklethasn't loaded.
Soyouhavetohave a morespecificruletomakethatwork s soyou'llneedtoprovideanexplicitfallback.
Otherwise, youjustyoujustwon't have a backgroundatall.
Okay, Mostofthetalks I giveareaboutperformance, and I thoughtmaybe I couldgetthroughthisonewithoutanyperftalk, but I waswrong.
Itturnsoutthatthere's a couplethingstokeepinmindwhenitcomestokeepingyourpaintwork.
Let's fastaswellasbeingconsidered, whatwithwhatcouldbe a rathertaxing A P I ifit's notusedcarefully, becauseifyoudon't youriskmakingtheInternetmorelikethisforpeople.
Sopleaseresistthetemptationto, likedostufflikemakethemredrawconstantlyto, like, makethemlike, animateandefficientlyorotherwisefornorealsensiblereasonand, additionally, make a callonwhenit's notappropriatetousethesthings.
A p I isnolonger a thing, butwedohaveanothersignalwecanlookat, andthatisthedevicememory, FBI, theamountofmemorythattheapproximateamountofmemorythatthedevicehas, andasusual, it's notsupportedeverywhere.
Andherewehavedecidedthatif a devicehasatleastfourgigabytesormoreofmemory, willregisterthatpaintworklitandthenweakenorotherwisewecan, like, dotheclasshookwherewedecidedtogoto a fallback.