Skip to content

Instantly share code, notes, and snippets.

@bga
Created March 4, 2017 12:05
Show Gist options
  • Save bga/030075cdad2c5d8a2909d77e53d14be7 to your computer and use it in GitHub Desktop.
Save bga/030075cdad2c5d8a2909d77e53d14be7 to your computer and use it in GitHub Desktop.
canvas+Math api versioning
var demoSeasonDate = "2015/02"
(function() {
var whiteListApi = function(methodNames) {
var C = function(nativeObj) {
this._nativeObj = nativeObj
}
methodNames.forEach(function(methodName) {
C.prototype[methodName] = function() {
return this._nativeObj[methodName].apply(this._nativeObj, arguments)
}
})
return C
}
var Canvas2D2015_02 = whiteListApi([
"canvas",
"currentTransform",
"direction",
"fillStyle",
"filter",
"font",
"globalAlpha",
"globalCompositeOperation",
"imageSmoothingEnabled",
"lineCap",
"lineDashOffset",
"lineJoin",
"lineWidth",
"miterLimit",
"shadowBlur",
"shadowColor",
"shadowOffsetX",
"shadowOffsetY",
"strokeStyle",
"textAlign",
"textBaseline",
"addHitRegion",
"arc",
"arcTo",
"asyncDrawXULElement",
"beginPath",
"bezierCurveTo",
"clearHitRegions",
"clearRect",
"clip",
"closePath",
"createImageData",
"createLinearGradient",
"createPattern",
"createRadialGradient",
"drawFocusIfNeeded",
"drawImage",
"drawWindow",
"ellipse",
"fill",
"fillRect",
"fillText",
"getImageData",
"getLineDash",
"isPointInPath",
"isPointInStroke",
"lineTo",
"measureText",
"moveTo",
"putImageData",
"quadraticCurveTo",
"rect",
"removeHitRegion",
"resetTransform",
"restore",
"rotate",
"save",
"scale",
"scrollPathIntoView",
"setLineDash",
"setTransform",
"stroke",
"strokeRect",
"strokeText",
"transform",
"translate",
])
var nativeGetContext = HTMLCanvasElement.prototype.getContext
HTMLCanvasElement.prototype.getContext = function(name) {
if(name == "2d") {
var nativeCanvas2dContext = nativeGetContext.apply(this, [name])
return new ({
"2010": Canvas2d2010Original,
"2010XMas": Canvas2d2010XMas,
"2011XMas": Canvas2d2010XMas,
"2013/02": Canvas2D2013_02
"2015/02": Canvas2D2015_02
}[demoSeasonDate] || console.assert(false, "Unsupported demoSeasonDate", demoSeasonDate))(nativeCanvas2dContext)
}
}
})()
var genSetFromList = function(list) {
return list
}
var excludeSet = function(from, what) {
return from.filter(function(v) {
return what.indexOf(v) != -1
})
}
if(null) {
}
else if(demoSeasonDate == "2010/05") {
excludeSet(genSetFromList(Object.getOwnPropertyNames(Math)), genSetFromList([
"E",
"LN10",
"LN2",
"LOG10E",
"LOG2E",
"PI",
"SQRT1_2",
"SQRT2",
"abs",
"acos",
"asin",
"atan",
"atan2",
"ceil",
"cos",
"exp",
"floor",
"log",
"max",
"min",
"pow",
"random",
"round",
"sin",
"sqrt",
"tan",
])).forEach(function(i) {
delete(Math[i])
})
}
with((function() {
var getNativeObject = function(v) {
return v.toString._nativeObj
}
return hideNewApi({
"2010/05": {
//# [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math$revision/89519]
Math: {
E: Math.E,
LN10: Math.LN10,
LN2: Math.LN2,
LOG10E: Math.LOG10E,
LOG2E: Math.LOG2E,
PI: Math.PI,
SQRT1_2: SQRT1_2,
SQRT2: SQRT2,
abs: Math.abs,
acos: Math.acos,
asin: Math.asin,
atan: Math.atan,
atan2: Math.atan2,
ceil: Math.ceil,
cos: Math.cos,
exp: Math.exp,
floor: Math.floor,
log: Math.log,
max: Math.max,
min: Math.min,
pow: Math.pow,
random: Math.random,
round: Math.round,
sin: Math.sin,
sqrt: Math.sqrt,
tan: Math.tan
}
},
"2010/11": {
//# [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math$revision/89519]
Math: {
E: Math.E,
LN10: Math.LN10,
LN2: Math.LN2,
LOG10E: Math.LOG10E,
LOG2E: Math.LOG2E,
PI: Math.PI,
SQRT1_2: SQRT1_2,
SQRT2: SQRT2,
abs: Math.abs,
acos: Math.acos,
asin: Math.asin,
atan: Math.atan,
atan2: Math.atan2,
ceil: Math.ceil,
cos: Math.cos,
exp: Math.exp,
floor: Math.floor,
log: Math.log,
max: Math.max,
min: Math.min,
pow: Math.pow,
random: Math.random,
round: Math.round,
sin: Math.sin,
sqrt: Math.sqrt,
tan: Math.tan
}
},
"2011/03": {
//# [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math$revision/89519]
Math: {
E: Math.E,
LN10: Math.LN10,
LN2: Math.LN2,
LOG10E: Math.LOG10E,
LOG2E: Math.LOG2E,
PI: Math.PI,
SQRT1_2: SQRT1_2,
SQRT2: SQRT2,
abs: Math.abs,
acos: Math.acos,
asin: Math.asin,
atan: Math.atan,
atan2: Math.atan2,
ceil: Math.ceil,
cos: Math.cos,
exp: Math.exp,
floor: Math.floor,
log: Math.log,
max: Math.max,
min: Math.min,
pow: Math.pow,
random: Math.random,
round: Math.round,
sin: Math.sin,
sqrt: Math.sqrt,
tan: Math.tan
}
},
"2012/02": {
//# [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math$revision/89523]
Math: {
E: Math.E,
LN10: Math.LN10,
LN2: Math.LN2,
LOG10E: Math.LOG10E,
LOG2E: Math.LOG2E,
PI: Math.PI,
SQRT1_2: SQRT1_2,
SQRT2: SQRT2,
abs: Math.abs,
acos: Math.acos,
asin: Math.asin,
atan: Math.atan,
atan2: Math.atan2,
ceil: Math.ceil,
cos: Math.cos,
exp: Math.exp,
floor: Math.floor,
log: Math.log,
max: Math.max,
min: Math.min,
pow: Math.pow,
random: Math.random,
round: Math.round,
sin: Math.sin,
sqrt: Math.sqrt,
tan: Math.tan
}
},
"2013/02": {
//# [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math$revision/340119]
Math: {
E: Math.E,
LN10: Math.LN10,
LN2: Math.LN2,
LOG10E: Math.LOG10E,
LOG2E: Math.LOG2E,
PI: Math.PI,
SQRT1_2: SQRT1_2,
SQRT2: SQRT2,
abs: Math.abs,
acos: Math.acos,
asin: Math.asin,
atan: Math.atan,
atan2: Math.atan2,
ceil: Math.ceil,
cos: Math.cos,
exp: Math.exp,
floor: Math.floor,
log: Math.log,
max: Math.max,
min: Math.min,
pow: Math.pow,
random: Math.random,
round: Math.round,
sin: Math.sin,
sqrt: Math.sqrt,
tan: Math.tan
}
},
"2014/02": {
//# [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math$revision/485197]
Math: {
E: Math.LN2,
LN2: Math.LN2,
LN10: Math.LN10,
LOG2E: Math.LOG2E,
LOG10E: Math.LOG10E,
PI: Math.PI,
SQRT1_2: Math.SQRT1_2,
SQRT2: Math.SQRT2,
abs: Math.abs,
acos: Math.acos,
acosh: Math.acosh,
asin: Math.asin,
asinh: Math.asinh,
atan: Math.atan,
atanh: Math.atanh,
atan2: Math.atan2,
cbrt: Math.cbrt,
ceil: Math.ceil,
cos: Math.cos,
cosh: Math.cosh,
exp: Math.exp,
expm1: Math.expm1,
floor: Math.floor,
fround: Math.fround,
hypot: Math.hypot,
imul: Math.imul,
log: Math.log,
log1p: Math.log1p,
log10: Math.log10,
log2: Math.log2,
max: Math.max,
min: Math.min,
pow: Math.pow,
random: Math.random,
round: Math.round,
sign: Math.sign,
sin: Math.sin,
sinh: Math.sinh,
sqrt: Math.sqrt,
tan: Math.tan,
tanh: Math.tanh,
trunc: Math.trunc
}
},
"2015/02": {
//# [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math$revision/719777]
Math: {
E: Math.E,
LN10: Math.LN10,
LN2: Math.LN2,
LOG10E: Math.LOG10E,
LOG2E: Math.LOG2E,
PI: Math.PI,
SQRT1_2: Math.SQRT1_2,
SQRT2: Math.SQRT2,
abs: Math.abs,
acos: Math.acos,
acosh: Math.acosh,
asin: Math.asin,
asinh: Math.asinh,
atan: Math.atan,
atanh: Math.atanh,
atan2: Math.atan2,
cbrt: Math.cbrt,
ceil: Math.ceil,
clz32: Math.clz32,
cos: Math.cos,
cosh: Math.cosh,
exp: Math.exp,
expm1: Math.expm1,
floor: Math.floor,
fround: Math.fround,
hypot: Math.hypot,
imul: Math.imul,
log: Math.log,
log1p: Math.log1p,
log10: Math.log10,
log2: Math.log2,
max: Math.max,
min: Math.min,
pow: Math.pow,
random: Math.random,
round: Math.round,
sign: Math.sign,
sin: Math.sin,
sinh: Math.sinh,
sqrt: Math.sqrt,
tan: Math.tan,
tanh: Math.tanh,
trunc: Math.trunc
}
}
})
})())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment