Skip to content

Instantly share code, notes, and snippets.

@getify
Created September 6, 2010 20:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/567511 to your computer and use it in GitHub Desktop.
Save getify/567511 to your computer and use it in GitHub Desktop.
// shorten/alias long canvas method names
// adapted from: http://marijn.haverbeke.nl/js1k.html
var Canv = document.body.children.c,
Gr = Canv.getContext("2d"),
prop
;
for (prop in Gr) {
Gr[ prop[0] + (prop[3]||"") + (prop[6]||"") ] = Gr[prop];
}
/*
arc => a
arcTo => aT
beginPath => bia
bezierCurveTo => biC
clearRect => cae
clearShadow => cah
createImageData => caI
createLinearGradient => caL
createPattern => caP
createRadialGradient => caR
clip => cp
closePath => csa
canvas => cv
drawImage => dwa ********
drawImageFromRect => dwa ********
fill => fl
fillRect => flc
fillText => flx
fillStyle => fly
font => ft
globalAlpha => gbA
globalCompositeOperation => gbC
getImageData => gIg
isPointInPath => iot
lineTo => le
lineWidth => led
lineJoin => lei
lineCap => lep
moveTo => me
miterLimit => mei
measureText => mse
putImageData => pIg
quadraticCurveTo => qdt
rotate => ra
rect => rt
restore => rte
setAlpha => sAh
setCompositeOperation => sCp
shadowBlur => sdB
shadowColor => sdC
shadowOffsetY => sdO ********
shadowOffsetX => sdO ********
save => se
setFillColor => sFl
scale => sl
setLineWidth => sLe ********
setLineCap => sLe ********
setLineJoin => sLe ********
setMiterLimit => sMe
stroke => so
strokeRect => soR
strokeStyle => soS
strokeText => soT
setShadow => sSd
setStrokeColor => sSo
setTransform => sTn
translate => tna
transform => tno
textAlign => tti
textBaseline => tts
*/
@bgerrissen
Copy link

I am sure that will leave readable, maintainable code...

@getify
Copy link
Author

getify commented Sep 7, 2010

it's not for "readable, maintainable code".

it's for reducing the size of your code dramatically for size-limited competitions or tasks. Also, it's conceivable such a technique could be honed and used by a JS minifier/compressor to automatically shrink your code.

@bgerrissen
Copy link

Ah.. Then I don't get this way of doing it. Obfuscation tools basically do the same without duplicate risks and afaik, they use the same algorithms used for variable minification preserving scope and whatnot. ;)

@getify
Copy link
Author

getify commented Sep 8, 2010

clearly this particular hashing algorithm, with collisions, is not acceptable. but maybe one similar to it would be good enough to not create collisions, and therefore be useful in the task.

@marijnh
Copy link

marijnh commented Sep 9, 2010

I'd say this is also useless for minifiers. Browser-specific extensions can easily cause conflicts and break this. Gimmicky contests seem to be the only real use ;)

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