Skip to content

Commit

Permalink
fixed new bug with stage toDataURL that was introduced with the new C…
Browse files Browse the repository at this point in the history
…anvas class, and added new filter unit tests
  • Loading branch information
ericdrowell committed Jul 21, 2012
1 parent 4b0b3a2 commit d74ec8a
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 122 deletions.
90 changes: 37 additions & 53 deletions dist/kinetic-core.js
Expand Up @@ -3,7 +3,7 @@
* http://www.kineticjs.com/
* Copyright 2012, Eric Rowell
* Licensed under the MIT or GPL Version 2 licenses.
* Date: Jul 19 2012
* Date: Jul 21 2012
*
* Copyright (C) 2011 - 2012 by Eric Rowell
*
Expand Down Expand Up @@ -433,6 +433,19 @@ Kinetic.Canvas.prototype = {
};
context.strokeText = function() {
};
},
/**
* toDataURL
*/
toDataURL: function(mimeType, quality) {
try {
// If this call fails (due to browser bug, like in Firefox 3.6),
// then revert to previous no-parameter image/png behavior
return this.element.toDataURL(mimeType, quality);
}
catch(e) {
return this.element.toDataURL();
}
}
};

Expand Down Expand Up @@ -1329,7 +1342,6 @@ Kinetic.Node = Kinetic.Class.extend({
if(this.attrs.scale.x !== 1 || this.attrs.scale.y !== 1) {
m.scale(this.attrs.scale.x, this.attrs.scale.y);
}
// center offset
if(this.attrs.offset.x !== 0 || this.attrs.offset.y !== 0) {
m.translate(-1 * this.attrs.offset.x, -1 * this.attrs.offset.y);
}
Expand Down Expand Up @@ -1429,15 +1441,7 @@ Kinetic.Node = Kinetic.Class.extend({
var bufferContext = bufferCanvas.getContext();
bufferCanvas.clear();
this._draw(bufferCanvas);

try {
// If this call fails (due to browser bug, like in Firefox 3.6),
// then revert to previous no-parameter image/png behavior
return bufferCanvas.element.toDataURL(mimeType, quality);
}
catch(e) {
return bufferCanvas.element.toDataURL();
}
return bufferCanvas.toDataURL(mimeType, quality);
},
/**
* converts node into an image. Since the toImage
Expand Down Expand Up @@ -2345,22 +2349,18 @@ Kinetic.Stage = Kinetic.Container.extend({
* @param {Number} [quality]
*/
toDataURL: function(callback, mimeType, quality) {
/*
* need to create a canvas element rather than using the buffer canvas
* because this method is asynchonous which means that other parts of the
* code could modify the buffer canvas before it's finished
*/
var canvas = new Kinetic.Canvas(this.attrs.width, this.attrs.height);
var context = canvas.getContext();
var layers = this.children;

function drawLayer(n) {
var layer = layers[n];
var canvasUrl;
try {
// If this call fails (due to browser bug, like in Firefox 3.6),
// then revert to previous no-parameter image/png behavior
canvasUrl = canvas.getElement().toDataURL(mimeType, quality);
}
catch(e) {
canvasUrl = canvas.getElement().toDataURL();
}

var layerUrl = layer.getCanvas().toDataURL(mimeType, quality);
var imageObj = new Image();
imageObj.onload = function() {
context.drawImage(imageObj, 0, 0);
Expand All @@ -2369,17 +2369,10 @@ Kinetic.Stage = Kinetic.Container.extend({
drawLayer(n + 1);
}
else {
try {
// If this call fails (due to browser bug, like in Firefox 3.6),
// then revert to previous no-parameter image/png behavior
callback(canvas.getElement().toDataURL(mimeType, quality));
}
catch(e) {
callback(canvas.getElement().toDataURL());
}
callback(canvas.toDataURL(mimeType, quality));
}
};
imageObj.src = canvasUrl;
imageObj.src = layerUrl;
}
drawLayer(0);
},
Expand Down Expand Up @@ -3168,14 +3161,7 @@ Kinetic.Layer = Kinetic.Container.extend({
* @param {Number} [quality]
*/
toDataURL: function(mimeType, quality) {
try {
// If this call fails (due to browser bug, like in Firefox 3.6),
// then revert to previous no-parameter image/png behavior
return this.getCanvas().element.toDataURL(mimeType, quality);
}
catch(e) {
return this.getCanvas().element.toDataURL();
}
return this.getCanvas().toDataURL(mimeType, quality);
},
/**
* private draw children
Expand Down Expand Up @@ -3607,11 +3593,10 @@ Kinetic.Shape = Kinetic.Node.extend({
for(var n = 0; n < family.length; n++) {
var node = family[n];
var t = node.getTransform();

var m = t.getMatrix();
context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
}

/*
* pre styles include alpha, linejoin
*/
Expand Down Expand Up @@ -5353,21 +5338,20 @@ Kinetic.Node.addGettersSetters(Kinetic.Path, ['data']);
// Transform
///////////////////////////////////////////////////////////////////////
/*
* Last updated November 2011
* By Simon Sarris
* www.simonsarris.com
* sarris@acm.org
*
* Free to use and distribute at will
* So long as you are nice to people, etc
*/
* Last updated November 2011
* By Simon Sarris
* www.simonsarris.com
* sarris@acm.org
*
* Free to use and distribute at will
* So long as you are nice to people, etc
*/

/*
* The usage of this class was inspired by some of the work done by a forked
* project, KineticJS-Ext by Wappworks, which is based on Simon's Transform
* class.
*/

* The usage of this class was inspired by some of the work done by a forked
* project, KineticJS-Ext by Wappworks, which is based on Simon's Transform
* class.
*/

Kinetic.Transform = function() {
this.m = [1, 0, 0, 1, 0, 0];
Expand Down
6 changes: 3 additions & 3 deletions dist/kinetic-core.min.js

Large diffs are not rendered by default.

9 changes: 1 addition & 8 deletions src/Layer.js
Expand Up @@ -106,14 +106,7 @@ Kinetic.Layer = Kinetic.Container.extend({
* @param {Number} [quality]
*/
toDataURL: function(mimeType, quality) {
try {
// If this call fails (due to browser bug, like in Firefox 3.6),
// then revert to previous no-parameter image/png behavior
return this.getCanvas().element.toDataURL(mimeType, quality);
}
catch(e) {
return this.getCanvas().element.toDataURL();
}
return this.getCanvas().toDataURL(mimeType, quality);
},
/**
* private draw children
Expand Down
11 changes: 1 addition & 10 deletions src/Node.js
Expand Up @@ -735,7 +735,6 @@ Kinetic.Node = Kinetic.Class.extend({
if(this.attrs.scale.x !== 1 || this.attrs.scale.y !== 1) {
m.scale(this.attrs.scale.x, this.attrs.scale.y);
}
// center offset
if(this.attrs.offset.x !== 0 || this.attrs.offset.y !== 0) {
m.translate(-1 * this.attrs.offset.x, -1 * this.attrs.offset.y);
}
Expand Down Expand Up @@ -835,15 +834,7 @@ Kinetic.Node = Kinetic.Class.extend({
var bufferContext = bufferCanvas.getContext();
bufferCanvas.clear();
this._draw(bufferCanvas);

try {
// If this call fails (due to browser bug, like in Firefox 3.6),
// then revert to previous no-parameter image/png behavior
return bufferCanvas.element.toDataURL(mimeType, quality);
}
catch(e) {
return bufferCanvas.element.toDataURL();
}
return bufferCanvas.toDataURL(mimeType, quality);
},
/**
* converts node into an image. Since the toImage
Expand Down
3 changes: 1 addition & 2 deletions src/Shape.js
Expand Up @@ -330,11 +330,10 @@ Kinetic.Shape = Kinetic.Node.extend({
for(var n = 0; n < family.length; n++) {
var node = family[n];
var t = node.getTransform();

var m = t.getMatrix();
context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
}

/*
* pre styles include alpha, linejoin
*/
Expand Down
27 changes: 8 additions & 19 deletions src/Stage.js
Expand Up @@ -288,22 +288,18 @@ Kinetic.Stage = Kinetic.Container.extend({
* @param {Number} [quality]
*/
toDataURL: function(callback, mimeType, quality) {
/*
* need to create a canvas element rather than using the buffer canvas
* because this method is asynchonous which means that other parts of the
* code could modify the buffer canvas before it's finished
*/
var canvas = new Kinetic.Canvas(this.attrs.width, this.attrs.height);
var context = canvas.getContext();
var layers = this.children;

function drawLayer(n) {
var layer = layers[n];
var canvasUrl;
try {
// If this call fails (due to browser bug, like in Firefox 3.6),
// then revert to previous no-parameter image/png behavior
canvasUrl = canvas.getElement().toDataURL(mimeType, quality);
}
catch(e) {
canvasUrl = canvas.getElement().toDataURL();
}

var layerUrl = layer.getCanvas().toDataURL(mimeType, quality);
var imageObj = new Image();
imageObj.onload = function() {
context.drawImage(imageObj, 0, 0);
Expand All @@ -312,17 +308,10 @@ Kinetic.Stage = Kinetic.Container.extend({
drawLayer(n + 1);
}
else {
try {
// If this call fails (due to browser bug, like in Firefox 3.6),
// then revert to previous no-parameter image/png behavior
callback(canvas.getElement().toDataURL(mimeType, quality));
}
catch(e) {
callback(canvas.getElement().toDataURL());
}
callback(canvas.toDataURL(mimeType, quality));
}
};
imageObj.src = canvasUrl;
imageObj.src = layerUrl;
}
drawLayer(0);
},
Expand Down
13 changes: 13 additions & 0 deletions src/util/Canvas.js
Expand Up @@ -108,5 +108,18 @@ Kinetic.Canvas.prototype = {
};
context.strokeText = function() {
};
},
/**
* toDataURL
*/
toDataURL: function(mimeType, quality) {
try {
// If this call fails (due to browser bug, like in Firefox 3.6),
// then revert to previous no-parameter image/png behavior
return this.element.toDataURL(mimeType, quality);
}
catch(e) {
return this.element.toDataURL();
}
}
};
25 changes: 12 additions & 13 deletions src/util/Transform.js
Expand Up @@ -2,21 +2,20 @@
// Transform
///////////////////////////////////////////////////////////////////////
/*
* Last updated November 2011
* By Simon Sarris
* www.simonsarris.com
* sarris@acm.org
*
* Free to use and distribute at will
* So long as you are nice to people, etc
*/
* Last updated November 2011
* By Simon Sarris
* www.simonsarris.com
* sarris@acm.org
*
* Free to use and distribute at will
* So long as you are nice to people, etc
*/

/*
* The usage of this class was inspired by some of the work done by a forked
* project, KineticJS-Ext by Wappworks, which is based on Simon's Transform
* class.
*/

* The usage of this class was inspired by some of the work done by a forked
* project, KineticJS-Ext by Wappworks, which is based on Simon's Transform
* class.
*/

Kinetic.Transform = function() {
this.m = [1, 0, 0, 1, 0, 0];
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
19 changes: 17 additions & 2 deletions tests/assets/unitDataUrls.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/js/Test.js
Expand Up @@ -21,7 +21,7 @@ function test(condition, message, warn) {
if(warn) {
testCounter.style.backgroundColor = 'orange';
testCounter.style.color = 'black';
console.warn(message + ' (NOTE: use Google Chrome for data url comparisons)');
console.warn(message + ' (NOTE: use Google Chrome for data url comparisons, run on web server for caching and filtering)');
}
else {
testCounter.style.backgroundColor = 'red';
Expand Down
2 changes: 1 addition & 1 deletion tests/js/manualTests.js
Expand Up @@ -325,7 +325,7 @@ Test.prototype.tests = {
layer.add(darth);
stage.add(layer);
};
imageObj.src = '../darth-vader.jpg';
imageObj.src = '../assets/darth-vader.jpg';
},
'EVENTS - star pixel detection': function(containerId) {
var stage = new Kinetic.Stage({
Expand Down

0 comments on commit d74ec8a

Please sign in to comment.