worked through some synchronous / asynchronous issues with toDataURL and toImage(). stage toDataURL() is now asynchronous, and all other node toDataURLs is still synchronous. toImage() is now asynchronous. Kinetic.Image once again only accepts image objects, and can no longer be instantiated with a data URL or image data due to asynchronous issues. It's much cleaner for the developer to load an image however they like, and then instantiate a Kinetic.Image shape synchronously

This commit is contained in:
Eric Rowell
2012-07-15 20:12:18 -07:00
parent acc6b6e14e
commit 384a686740
11 changed files with 233 additions and 267 deletions

139
dist/kinetic-core.js vendored
View File

@@ -277,21 +277,28 @@ Kinetic.Type = {
/*
* arg can be an image object or image data
*/
_getImage: function(arg) {
// if arg is already an image object, just return it
if(this._isElement(arg)) {
return arg;
_getImage: function(arg, callback) {
// if arg is null or undefined
if(!arg) {
callback(null);
}
// if arg is already an image object
else if(this._isElement(arg)) {
callback(arg);
}
// if arg is a string, then it's a data url
if(this._isString(arg)) {
else if(this._isString(arg)) {
var imageObj = new Image();
imageObj.onload = function() {
callback(imageObj);
}
imageObj.src = arg;
return imageObj;
}
//if arg is an object that contains the data property, it's an image object
if(arg.data) {
else if(arg.data) {
var canvas = document.createElement('canvas');
canvas.width = arg.width;
canvas.height = arg.height;
@@ -299,12 +306,15 @@ Kinetic.Type = {
context.putImageData(arg, 0, 0);
var dataUrl = canvas.toDataURL();
var imageObj = new Image();
imageObj.onload = function() {
callback(imageObj);
}
imageObj.src = dataUrl;
return imageObj;
}
// default
return null;
else {
callback(null);
}
}
};
@@ -717,10 +727,6 @@ Kinetic.Node = Kinetic.Class.extend({
that._setAttr(obj[key], 'width', size.width);
that._setAttr(obj[key], 'height', size.height);
break;
case 'image':
var img = type._getImage(val);
that._setAttr(obj, key, img);
break;
default:
that._setAttr(obj, key, val);
break;
@@ -1320,10 +1326,15 @@ Kinetic.Node = Kinetic.Class.extend({
}
},
/**
* to image
* converts node into an image. Since the toImage
* method is asynchronous, a callback is required
* @name toImage
* @methodOf Kinetic.Stage.prototype
*/
toImage: function() {
return Kinetic.Type._getImage(this.toDataURL());
toImage: function(callback) {
Kinetic.Type._getImage(this.toDataURL(), function(img) {
callback(img);
});
},
_setImageData: function(imageData) {
if(imageData && imageData.data) {
@@ -2190,25 +2201,37 @@ Kinetic.Stage = Kinetic.Container.extend({
return this.content;
},
/**
* Creates a composite data URL. If MIME type is not
* specified, then "image/png" will result. For "image/jpeg", specify a quality
* level as quality (range 0.0 - 1.0). Note that this method works
* differently from toDataURL() for other nodes because it generates an absolute dataURL
* based on what's draw onto the canvases for each layer, rather than drawing
* the current state of each node
* Creates a composite data URL and requires a callback because the stage
* toDataURL method is asynchronous. If MIME type is not
* specified, then "image/png" will result. For "image/jpeg", specify a quality
* level as quality (range 0.0 - 1.0). Note that this method works
* differently from toDataURL() for other nodes because it generates an absolute dataURL
* based on what's draw onto the canvases for each layer, rather than drawing
* the current state of each node
* @name toDataURL
* @methodOf Kinetic.Stage.prototype
* @param {Function} callback
* @param {String} [mimeType]
* @param {Number} [quality]
*/
toDataURL: function(mimeType, quality) {
var bufferLayer = this.bufferLayer;
var bufferCanvas = bufferLayer.getCanvas();
var bufferContext = bufferLayer.getContext();
toDataURL: function(callback, mimeType, quality) {
/*
* we need to create a temp layer rather than using
* the bufferLayer because the stage toDataURL method
* is asynchronous, which means that other parts of the
* code base could be updating or clearing the bufferLayer
* while the stage toDataURL method is processing
*/
var tempLayer = new Kinetic.Layer();
tempLayer.getCanvas().width = this.attrs.width;
tempLayer.getCanvas().height = this.attrs.height;
tempLayer.parent = this;
var tempCanvas = tempLayer.getCanvas();
var tempContext = tempLayer.getContext();
var layers = this.children;
bufferLayer.clear();
for(var n = 0; n < layers.length; n++) {
function drawLayer(n) {
var layer = layers[n];
var layerUrl;
try {
@@ -2221,18 +2244,40 @@ Kinetic.Stage = Kinetic.Container.extend({
}
var imageObj = new Image();
imageObj.src = layerUrl;
bufferContext.drawImage(imageObj, 0, 0);
}
imageObj.onload = function() {
tempContext.drawImage(imageObj, 0, 0);
try {
// If this call fails (due to browser bug, like in Firefox 3.6),
// then revert to previous no-parameter image/png behavior
return bufferLayer.getCanvas().toDataURL(mimeType, quality);
}
catch(e) {
return bufferLayer.getCanvas().toDataURL();
if(n < layers.length - 1) {
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(tempLayer.getCanvas().toDataURL(mimeType, quality));
}
catch(e) {
callback(tempLayer.getCanvas().toDataURL());
}
}
};
imageObj.src = layerUrl;
}
drawLayer(0);
},
/**
* converts stage into an image. Since the stage toImage() method
* is asynchronous, a callback function is required
* @name toImage
* @methodOf Kinetic.Stage.prototype
* @param {Function} callback
*/
toImage: function(callback) {
this.toDataURL(function(dataUrl) {
Kinetic.Type._getImage(dataUrl, function(img) {
callback(img);
});
});
},
_resizeDOM: function() {
var width = this.attrs.width;
@@ -3864,20 +3909,6 @@ Kinetic.Image = Kinetic.Shape.extend({
};
// call super constructor
this._super(config);
/*
* if image property is ever changed, check and see
* if it was set to image data, and if it was, go ahead
* and save it
*/
this.on('beforeImageChange.kinetic', function(evt) {
this._setImageData(evt.newVal);
});
/*
* if image property was set with image data,
* go ahead and save it
*/
this._setImageData(config.image);
},
/**
* set width and height

File diff suppressed because one or more lines are too long