-
-
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 | |
*/ |
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.
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. ;)
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.
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 ;)
I am sure that will leave readable, maintainable code...