Skip to content

Commit 986c291

Browse files
author
Lanny McNie
committed
Merge pull request #735 from trevordunn/FauxCanvasWebGL
FauxCanvas.js » added WebGL support
2 parents cf3c1d2 + a7df7eb commit 986c291

1 file changed

Lines changed: 36 additions & 8 deletions

File tree

extras/FauxCanvas/FauxCanvas.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
// gecko / mozilla:
5353
"mozCurrentTransform","mozCurrentTransformInverse","mozDash","mozDashOffset","mozFillRule","mozImageSmoothingEnabled","mozTextStyle",
5454
// webkit:
55-
"webkitLineDash", "webkitLineDashOffset"
55+
"webkitLineDash", "webkitLineDashOffset",
56+
// canvas:
57+
"style"
5658
];
5759
FauxCanvas.METHODS = [
5860
// all browsers:
@@ -62,7 +64,12 @@
6264
// webkit:
6365
"clearShadow","drawImageFromRect","setAlpha","setCompositeOperation","setLineWidth","setLineCap","setLineJoin","setMiterLimit","setStrokeColor","setFillColor","setShadow",
6466
// canvas:
65-
"addEventListener","removeEventListener"
67+
"addEventListener","removeEventListener",
68+
// webgl:
69+
"enable","blendFuncSeparate","pixelStorei","clearColor","viewport","attachShader","linkProgram","getProgramParameter","getProgramInfoLog","getAttribLocation","getUniformLocation","enableVertexAttribArray","useProgram","createShader","shaderSource","compileShader","getShaderParameter","createBuffer","bindBuffer","vertexAttribPointer","bufferData","bindTexture","texImage2D","texParameteri","activeTexture","uniform1i","uniformMatrix3fv","bufferSubData","drawElements","bindFrameBuffer","createFrameBuffer","uniform1iv","getShaderInfoLog","uniformMatrix4fv","uniformMatrix1i","createProgram","drawArrays","createTexture"
70+
];
71+
FauxCanvas.WEBGL_PROPERTIES = [
72+
"ACTIVE_TEXTURE","ALIASED_LINE_WIDTH_RANGE","ALIASED_POINT_SIZE_RANGE","ALPHA_BITS","ARRAY_BUFFER_BINDING","BLEND","BLEND_COLOR","BLEND_DST_ALPHA","BLEND_DST_RGB","BLEND_EQUATION_ALPHA","BLEND_EQUATION_RGB","BLEND_SRC_ALPHA","BLEND_SRC_RGB","BLUE_BITS","COLOR_CLEAR_VALUE","COLOR_WRITEMASK","COMPRESSED_TEXTURE_FORMATS","CULL_FACE","CULL_FACE_MODE","CURRENT_PROGRAM","DEPTH_BITS","DEPTH_CLEAR_VALUE","DEPTH_FUNC","DEPTH_RANGE","DEPTH_TEST","DEPTH_WRITEMASK","DITHER","ELEMENT_ARRAY_BUFFER_BINDING","FRAMEBUFFER_BINDING","FRONT_FACE","GENERATE_MIPMAP_HINT","GREEN_BITS","LINE_WIDTH","MAX_TEXTURE_MAX_ANISOTROPY_EXT","MAX_COMBINED_TEXTURE_IMAGE_UNITS","MAX_CUBE_MAP_TEXTURE_SIZE","MAX_FRAGMENT_UNIFORM_VECTORS","MAX_RENDERBUFFER_SIZE","MAX_TEXTURE_IMAGE_UNITS","MAX_TEXTURE_SIZE","MAX_VARYING_VECTORS","MAX_VERTEX_ATTRIBS","MAX_VERTEX_TEXTURE_IMAGE_UNITS","MAX_VERTEX_UNIFORM_VECTORS","MAX_VIEWPORT_DIMS","PACK_ALIGNMENT","POLYGON_OFFSET_FACTOR","POLYGON_OFFSET_FILL","POLYGON_OFFSET_UNITS","RED_BITS","RENDERBUFFER_BINDING","RENDERER","SAMPLE_BUFFERS","SAMPLE_COVERAGE_INVERT","SAMPLE_COVERAGE_VALUE","SAMPLES","SCISSOR_BOX","SCISSOR_TEST","SHADING_LANGUAGE_VERSION","STENCIL_BACK_FAIL","STENCIL_BACK_FUNC","STENCIL_BACK_PASS_DEPTH_FAIL","STENCIL_BACK_PASS_DEPTH_PASS","STENCIL_BACK_REF","STENCIL_BACK_VALUE_MASK","STENCIL_BACK_WRITEMASK","STENCIL_BITS","STENCIL_CLEAR_VALUE","STENCIL_FAIL","STENCIL_FUNC","STENCIL_PASS_DEPTH_FAIL","STENCIL_PASS_DEPTH_PASS","STENCIL_REF","STENCIL_TEST","STENCIL_VALUE_MASK","STENCIL_WRITEMASK","SUBPIXEL_BITS","TEXTURE_BINDING_2D","TEXTURE_BINDING_CUBE_MAP","UNPACK_ALIGNMENT","UNPACK_COLORSPACE_CONVERSION_WEBGL","UNPACK_FLIP_Y_WEBGL","UNPACK_PREMULTIPLY_ALPHA_WEBGL","VENDOR","VERSION","VIEWPORT"
6673
];
6774

6875
// initialization:
@@ -74,12 +81,12 @@
7481
p.initialize = function (width, height, replaceInternal) {
7582
var methods = FauxCanvas.METHODS;
7683
for (var i= 0, l=methods.length; i<l; i++) {
77-
this[methods[i]] = function() {};
84+
this[methods[i]] = function() { return {}; };
7885
}
7986

8087
var props = FauxCanvas.PROPERTIES;
8188
for (i= 0, l=props.length; i<l; i++) {
82-
this[props[i]] = null;
89+
this[props[i]] = {};
8390
}
8491

8592
this.width = width||300;
@@ -101,15 +108,36 @@
101108

102109
/**
103110
* Enables or disables logging by overriding (or restoring) the getContext method on the target canvas to return
104-
* the FauxCanvas instance as a proxy for the "2d" context.
105-
* @method setEnabled
106-
* @param {Boolean} val True or false.
111+
* the FauxCanvas instance as a proxy for the "2d", "webgl", or "experimental-webgl" contexts.
112+
* @method getContext
113+
* @param {String} type
114+
* @param {Object} options
107115
**/
108-
p.getContext = function(type) {
116+
p.getContext = function(type, options) {
109117
if (type == "2d") { return this; }
118+
119+
if (type === "webgl" || type === "experimental-webgl"){
120+
var canvas = document.createElement("canvas");
121+
this._glCtx = canvas.getContext(type, options);
122+
123+
var props = FauxCanvas.WEBGL_PROPERTIES;
124+
for (var i=0, l=props.length; i<l; i++) {
125+
this[props[i]] = this._glCtx[props[i]];
126+
}
127+
return this;
128+
}
129+
110130
throw("Context not supported: "+type);
111131
};
112132

133+
/**
134+
* Mimics the return values of WebGl's context.
135+
* @param {Number} type The parameter to look up.
136+
*/
137+
p.getParameter = function(type) {
138+
return this._glCtx.getParameter(type);
139+
};
140+
113141
// private methods:
114142

115143
window.FauxCanvas = FauxCanvas;

0 commit comments

Comments
 (0)