Permalink
Browse files

Merge pull request #846 from jviereck/feature_img_data

Add feature detection for using Uint8Array as imageData
  • Loading branch information...
2 parents 759e839 + cf7d450 commit 7edefbbe418acfb56afcb8b3e381cce19f6502a6 @arturadib arturadib committed Dec 6, 2011
Showing with 42 additions and 17 deletions.
  1. +40 −15 src/canvas.js
  2. +2 −0 src/core.js
  3. +0 −2 src/fonts.js
View
@@ -1114,26 +1114,16 @@ var CanvasGraphics = (function canvasGraphics() {
var tmpCanvas = new this.ScratchCanvas(w, h);
var tmpCtx = tmpCanvas.getContext('2d');
- var tmpImgData;
+ this.putBinaryImageData(tmpCtx, imgData, w, h);
- // Some browsers can set an UInt8Array directly as imageData, some
- // can't. As long as we don't have proper feature detection, just
- // copy over each pixel and set the imageData that way.
- tmpImgData = tmpCtx.getImageData(0, 0, w, h);
-
- // Copy over the imageData.
- var tmpImgDataPixels = tmpImgData.data;
- var len = tmpImgDataPixels.length;
-
- while (len--) {
- tmpImgDataPixels[len] = imgData.data[len];
- }
-
- tmpCtx.putImageData(tmpImgData, 0, 0);
ctx.drawImage(tmpCanvas, 0, -h);
this.restore();
},
+ putBinaryImageData: function canvasPutBinaryImageData() {
+ //
+ },
+
// Marked content
markPoint: function canvasGraphicsMarkPoint(tag) {
@@ -1194,3 +1184,38 @@ var CanvasGraphics = (function canvasGraphics() {
return constructor;
})();
+if (!isWorker) {
+ // Feature detection if the browser can use an Uint8Array directly as imgData.
+ var canvas = document.createElement('canvas');
+ canvas.width = 1;
+ canvas.height = 1;
+ var ctx = canvas.getContext('2d');
+
+ try {
+ ctx.putImageData({
+ width: 1,
+ height: 1,
+ data: new Uint8Array(4)
+ }, 0, 0);
+
+ CanvasGraphics.prototype.putBinaryImageData =
+ function CanvasGraphicsPutBinaryImageDataNative(ctx, imgData) {
+ ctx.putImageData(imgData, 0, 0);
+ };
+ } catch (e) {
+ CanvasGraphics.prototype.putBinaryImageData =
+ function CanvasGraphicsPutBinaryImageDataShim(ctx, imgData, w, h) {
+ var tmpImgData = ctx.getImageData(0, 0, w, h);
+
+ // Copy over the imageData pixel by pixel.
+ var tmpImgDataPixels = tmpImgData.data;
+ var len = tmpImgDataPixels.length;
+
+ while (len--) {
+ tmpImgDataPixels[len] = imgData.data[len];
+ }
+
+ ctx.putImageData(tmpImgData, 0, 0);
+ };
+ }
+}
View
@@ -5,6 +5,8 @@
var globalScope = (typeof window === 'undefined') ? this : window;
+var isWorker = (typeof window == 'undefined');
+
var ERRORS = 0, WARNINGS = 1, TODOS = 5;
var verbosity = WARNINGS;
View
@@ -3,8 +3,6 @@
'use strict';
-var isWorker = (typeof window == 'undefined');
-
/**
* Maximum time to wait for a font to be loaded by font-face rules.
*/

0 comments on commit 7edefbb

Please sign in to comment.