Skip to content

Instantly share code, notes, and snippets.

@lewdev
Created August 29, 2023 22:23
Show Gist options
  • Select an option

  • Save lewdev/4dab10b8d6a6cd18682cb106ce3dc071 to your computer and use it in GitHub Desktop.

Select an option

Save lewdev/4dab10b8d6a6cd18682cb106ce3dc071 to your computer and use it in GitHub Desktop.
⛳ Code Golf Notes

⛳ Code Golf Notes

Techniques used to shorten code.

Shorten fillStyle

If fillStyle is going to be used more than once, turn it into a reusable function.

j=l=>x.fillStyle=l

Use the back-tick character to call methods with a single string argument:

j`lime`
j`red`
j`#0F0`
j`rgba(0,0,0,.05)`
j`#00000005`

For brevity you could use color names instead of the 3 or 6 hex values.

You can add 2 characters to the 6-hex value for the alpha argument, which is shorter than using rgba.

Declare values in parenthesis or square brackets

p=[f=50]
Math.random(f=50)

If you need to declare or set a value, you can save a ; semi-colon to do it inside a declared array or as a method argument.

Set the font size

x.font=f+'px A'

Self-explained code

r=Math.random
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment