Skip to content

Dwitter specific Golfing Tricks

adroitwhiz edited this page Dec 22, 2019 · 3 revisions

If you need to use a variable and want to avoid 'undefined' errors before it has been set, consider using T. (It's the tan function provided)

There appear to be vars o and O but I don't know what they are. "output"?

Avoid the R() function for static colors. Just use:

x.fillStyle='#f0f'

Is this an efficient way to set the background black?

c.style.background='#000'

Stats

Magna showed a way to get more stats out of Chrome:

c.width|=0;for(i=3;i--;x.drawImage(stats.dom.children[i],0,0,T=160,80,i%2*960,(i>>1)*540,960,540))x.fillRect(S(t+i)*T+1440,C(t-i)*T+810,9,9)

Framerate

For slow machines, add these lines to reduce the number of frames drawn, and to keep t time-based.

if((60*t|0)%4>0)return
t==0&&(start=Date.now())
t=(Date.now()-start)/1000

Execute a dweet more quickly

Sometime a Dweet executes slowly because each frame only does a small thing. We can run more frames per second like this:

SPEED=9;for(t=t*SPEED,END=t+SPEED/60;t<END;t+=1/60){
...
}

Initialisation

Magna found that we can close the render function and open a separate block to define init.

x.globalAlpha=.03;x.fillRect(r(),r()/2,r()/4,r()/8)}
{r=()=>Math.random()*2e3

Keyboard input

onkeydown=e=>this["YXYX"[g=e.which%4]]-="0220"[g]-1

from Bálint

Shorter ways to call functions

Using a template string

x=c.getContext`2d`

is the same as calling

x=c.getContext(['2d'])

Using a setter, useful if you will call the function very many times in the code (by AnnanFay):

with({set x(v)function_body})
    x={a:1,b:2}

You could add multiple setters in that object, for multiple functions.

Returning the function, for function chaining (by AnnanFay):

x=v=>(function_body,x)
x({a:1,b:2})({a:1,b:2})({a:1,b:2})

Or, if the function returns undefined:

x=v=>function_body||x