A collection of tips when writing tinycode or dweets. It's getting hard for me to find the dweets that have neat tricks. I'll add more as I find more.
For example when rounding down a value multiplied by Math.random, you can add |0.
Example getting a random integer from 0 to 254 (inclusive):
r=Math.random()*255|0Using ~~(value) is better when other operators are involved:
~~(t*9+20)Instead of 1000 you can save one character by typing 1e3 instead.
Also instead of using specific numbers like 1920 it might be better to round up to 2e3.
If you use fillStyle or other context method more than once, you can set it to a variable.
x[f="fillStyle"]="red"
x[f]="blue" //on following uses
x[f]="#00F"
or
f=s=>x.fillStyle=s
f`red`
f`blue`
f`lime` //or "#00F" or "green"
*Using the 3-hex format for colors is at least one shorter than most color names.
Like in the above tip, if you're passing in one string argument you can use the ` around your string value without parenthesis.
You can execute code between backticks in a setInterval but it will run without pause.
setInterval`code will run here infinitely`
This isn't common in Dweets since OOP is hardly practical there but you can create new objects without ().
o=new Array
If you need random numbers that use less than r=Math.random (13b), then here are two methods.
You can see what kind of values they produce in a dweet.
Method A returns values between -1 and 1 (16b):
A=_=>T=S(T*7)||1Method B returns values between 0 and 1:
B=_=>R=(R*9||.9)%1The general means for clearing a canvas is using fillRect to color the background over everything, but it doesn't have to be that way.
On Dwitter, the default width of a canvas is 1920 and adding 0 will set the c.width which clears the canvas. A very cool technique I found in u/danny@hille.dk's dweet.
y=c.width+=0
//usage example:
for(y=c.width+=0;y--;)Using x.reset is great because you can set values within the parenthesis and saves a byte on a , or ;:
x.reset(n=9)The classic c.width|=0 works well if you want to use the width as its value as well.
A=c.width|=0i&127
i&255
2**9
// is the same as
Math.pow(2, 9)