-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from Rayman/remove-canvas-dependency
Remove canvas dependency
- Loading branch information
Showing
8 changed files
with
129 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @fileOverview | ||
* @author Ramon Wijnands - rayman747@hotmail.com | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var pngparse = require('pngparse'); | ||
|
||
/** | ||
* If a message was compressed as a PNG image (a compression hack since | ||
* gzipping over WebSockets * is not supported yet), this function decodes | ||
* the "image" as a Base64 string. | ||
* | ||
* @private | ||
* @param data - object containing the PNG data. | ||
* @param callback - function with params: | ||
* * data - the uncompressed data | ||
*/ | ||
function decompressPng(data, callback) { | ||
var buffer = new Buffer(data, 'base64'); | ||
|
||
pngparse.parse(buffer, function(err, data) { | ||
if(err) { | ||
console.warn('Cannot process PNG encoded message '); | ||
} else { | ||
var jsonData = data.data.toString(); | ||
callback(JSON.parse(jsonData)); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = decompressPng; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @fileOverview | ||
* @author Graeme Yeates - github.com/megawac | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var Canvas = require('canvas'); | ||
var Image = Canvas.Image || global.Image; | ||
|
||
/** | ||
* If a message was compressed as a PNG image (a compression hack since | ||
* gzipping over WebSockets * is not supported yet), this function places the | ||
* "image" in a canvas element then decodes the * "image" as a Base64 string. | ||
* | ||
* @private | ||
* @param data - object containing the PNG data. | ||
* @param callback - function with params: | ||
* * data - the uncompressed data | ||
*/ | ||
function decompressPng(data, callback) { | ||
// Uncompresses the data before sending it through (use image/canvas to do so). | ||
var image = new Image(); | ||
// When the image loads, extracts the raw data (JSON message). | ||
image.onload = function() { | ||
// Creates a local canvas to draw on. | ||
var canvas = new Canvas(); | ||
var context = canvas.getContext('2d'); | ||
|
||
// Sets width and height. | ||
canvas.width = image.width; | ||
canvas.height = image.height; | ||
|
||
// Prevents anti-aliasing and loosing data | ||
context.imageSmoothingEnabled = false; | ||
context.webkitImageSmoothingEnabled = false; | ||
context.mozImageSmoothingEnabled = false; | ||
|
||
// Puts the data into the image. | ||
context.drawImage(image, 0, 0); | ||
// Grabs the raw, uncompressed data. | ||
var imageData = context.getImageData(0, 0, image.width, image.height).data; | ||
|
||
// Constructs the JSON. | ||
var jsonData = ''; | ||
for (var i = 0; i < imageData.length; i += 4) { | ||
// RGB | ||
jsonData += String.fromCharCode(imageData[i], imageData[i + 1], imageData[i + 2]); | ||
} | ||
callback(JSON.parse(jsonData)); | ||
}; | ||
// Sends the image data to load. | ||
image.src = 'data:image/png;base64,' + data; | ||
} | ||
|
||
module.exports = decompressPng; |