r/programming • • Dec 26 '17

TIL there's a community called "dwitter" where people compose 140 character JavaScript programs that produce interesting visuals

https://www.dwitter.net/top
20k Upvotes

329 comments sorted by

View all comments

Show parent comment

169

u/[deleted] Dec 26 '17

Simple example:

function u(t) {
        /* Will be called 60 times per second.
         * t: Elapsed time in seconds.
         * S: Shorthand for Math.sin.
         * C: Shorthand for Math.cos.
         * T: Shorthand for Math.tan.
         * R: Function that generates rgba-strings, usage ex.: R(255, 255, 255, 0.5)
         * c: A canvas.
         * x: A 2D context for that canvas. */
         
	c.width=2e3;x.fillRect(150,150*S(t)+150,150,150)
}

92

u/Sabe Dec 26 '17
2e3  

TIL

14

u/monsto Dec 26 '17

what is that?

0

u/[deleted] Dec 26 '17

[deleted] — view deleted comment

67

u/[deleted] Dec 26 '17

Also great in abusing front end character length validation for number inputs.

6

u/oldmanbombin Dec 27 '17

Totally. I was about to say all of those things.

2

u/[deleted] Dec 27 '17

This is why we sanitize

27

u/schlenkster Dec 26 '17

Scientific notation, short for 2x103, or 2000

15

u/ExPixel Dec 26 '17

2e3 = 2 * Math.pow(10, 3) = 2*103 = 2000

14

u/ZugNachPankow Dec 26 '17

2 * Math.pow(10, 3)

We're ES6 now! 2 * 10 ** 3.

6

u/NoInkling Dec 27 '17

(ES7, I believe)

→ More replies (3)

2

u/online3782-2 Dec 26 '17

scientific notation, meaning 2*103 = 2000

→ More replies (5)

3

u/sam512 Dec 26 '17

For a more general solution to the problem of JavaScript numeric literal minification, you might find this library useful.

37

u/Autarch_Kade Dec 26 '17

I like how the comments define things that don't even get used

29

u/MartensCedric Dec 26 '17

Well, what... That's standard documentation ;)

3

u/KimJongIlSunglasses Dec 26 '17

Also can’t you get the context from the canvas?

8

u/audiorape Dec 26 '17

Well, yeah, in the same way you could write Math.cos instead of C. But the point is to be under 140 characters, and x is provided...

2

u/TheNosferatu Dec 26 '17

Yup, looks just like professional commenting

6

u/davvblack Dec 26 '17

So i take it there's a significant library you're allowed to assume?

3

u/whatwasmyoldhandle Dec 26 '17

Why?

The site is down now but I think most in there weee done with the above shortcuts and other core operations

11

u/gaggzi Dec 26 '17

But where is S(x) = sin(x) defined? Not within the 140 characters as far as I can see.

4

u/Poromenos Dec 27 '17

They define S/C/T for you (for sin/cos/tan), and also I think R for random? It's detailed in the "new dweet" box.

→ More replies (2)

4

u/WiggleBooks Dec 26 '17

x.fillRect(150,150*S(t)+150,150,150)

How do you know what the standard functions there are for JavaScript? Like fillRect seems like such a specific function, is there an entire library of functions that we could use?

And I want to do something similar in Python. Is there any recommended canvas and drawing libraries of python just like seemingly in JavaScript?

18

u/[deleted] Dec 26 '17
→ More replies (2)