r/tinycode • • Jun 01 '16

Time Dependant Graphing on an Canvas in 307 chars of JS

Hey! I'm the guy who posted the "Time dependant graphing in JS using only 144 chars" a few days ago. I decided to see if I could make a similar thing, but this time, render the graph on a canvas element.

After a long train ride home, this is what I came up with this.

It appends a canvas onto the bottom of the body of any page, and starts graphing :D

       ((f,d,a,n,i,e=0,l,p)=>setInterval
        (c=>{for(l=c.createImageData(a,
         n),e+=p=1;p<a*n;l.data[p*4-1]
         =i(p/a,-f(p++%a-a/2,e)+n/2)?a
           :p/a);c.putImageData(l,0,
           0)},17,d.body.appendChild
          (d.createElement('canvas'))
          .getContext("2d")))((x,t)=>

Math.cos(x/60*Math.PI-t/10)*(Math.sin(t/20)*50)

                   ,document
                   ,301,150,
                    (d,p)=>
                    d>p-1&d
                     <p+1)

You can graph whatever you like, just change the equation!

By the way, the equation has access to both x and t, where t is an incrementing variable corresponding to time. You can use these two vars to make moving graphs (like the one I put)

2 Upvotes

2 comments sorted by

1

u/nexe mod Jun 01 '16

Next step: Make it consume less CPU and make a bookmarklet out of it ;)

1

u/daniel5151 Jun 02 '16 edited Feb 14 '17

It was CPU vs Code Length, and I chose poorly :D

Canvas drawing functions, so unwieldy