mirror of
https://github.com/konvajs/konva.git
synced 2025-11-18 09:07:30 +08:00
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:
File diff suppressed because one or more lines are too long
@@ -38,7 +38,7 @@ Test.prototype.tests = {
|
||||
circle.on('dragend', function() {
|
||||
dragEnd = true;
|
||||
});
|
||||
startDataUrl = stage.toDataURL();
|
||||
startDataUrl = layer.toDataURL();
|
||||
warn(urls[0] === startDataUrl, 'start data url is incorrect');
|
||||
/*
|
||||
* simulate drag and drop
|
||||
@@ -70,7 +70,7 @@ Test.prototype.tests = {
|
||||
test(dragMove, 'dragmove event was not triggered');
|
||||
test(dragEnd, 'dragend event was not triggered');
|
||||
|
||||
var endDataUrl = stage.toDataURL();
|
||||
var endDataUrl = layer.toDataURL();
|
||||
warn(urls[1] === endDataUrl, 'end data url is incorrect');
|
||||
},
|
||||
'DRAG AND DROP - cancel drag and drop by setting draggable to false': function(containerId) {
|
||||
@@ -176,7 +176,7 @@ Test.prototype.tests = {
|
||||
|
||||
stage.add(layer);
|
||||
|
||||
var startDataUrl = stage.toDataURL();
|
||||
var startDataUrl = layer.toDataURL();
|
||||
warn(urls[0] === startDataUrl, 'start data url is incorrect');
|
||||
|
||||
/*
|
||||
@@ -197,7 +197,7 @@ Test.prototype.tests = {
|
||||
clientY: 109
|
||||
});
|
||||
|
||||
var endDataUrl = stage.toDataURL()
|
||||
var endDataUrl = layer.toDataURL()
|
||||
warn(urls[1] === endDataUrl, 'end data url is incorrect');
|
||||
|
||||
},
|
||||
@@ -240,14 +240,15 @@ Test.prototype.tests = {
|
||||
layer.add(circle);
|
||||
stage.add(layer);
|
||||
|
||||
warn(stage.toDataURL() === urls[0], 'start data url is incorrect');
|
||||
warn(layer.toDataURL() === urls[0], 'start data url is incorrect');
|
||||
|
||||
stage._mousemove({
|
||||
clientX: 377,
|
||||
clientY: 101
|
||||
});
|
||||
|
||||
warn(stage.toDataURL() === urls[1], 'mid data url is incorrect');
|
||||
console.log(layer.toDataURL())
|
||||
warn(layer.toDataURL() === urls[1], 'mid data url is incorrect');
|
||||
|
||||
// move mouse back out of circle
|
||||
stage._mousemove({
|
||||
@@ -259,7 +260,7 @@ Test.prototype.tests = {
|
||||
clientY: 138
|
||||
});
|
||||
|
||||
warn(stage.toDataURL() === urls[0], 'end data url is incorrect');
|
||||
warn(layer.toDataURL() === urls[0], 'end data url is incorrect');
|
||||
},
|
||||
'EVENTS - path detection mousedown mouseup mouseover mouseout mousemove click dblclick / touchstart touchend touchmove tap dbltap': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
|
||||
@@ -1468,7 +1468,7 @@ Test.prototype.tests = {
|
||||
});
|
||||
|
||||
Ellipse.on('click', function() {
|
||||
window.open(stage.toDataURL());
|
||||
window.open(layer.toDataURL());
|
||||
});
|
||||
|
||||
layer.add(Ellipse);
|
||||
@@ -1492,7 +1492,7 @@ Test.prototype.tests = {
|
||||
});
|
||||
|
||||
Ellipse.on('click', function() {
|
||||
window.open(stage.toDataURL('image/jpeg', 0));
|
||||
window.open(layer.toDataURL('image/jpeg', 0));
|
||||
});
|
||||
|
||||
layer.add(Ellipse);
|
||||
@@ -1516,7 +1516,7 @@ Test.prototype.tests = {
|
||||
});
|
||||
|
||||
Ellipse.on('click', function() {
|
||||
window.open(stage.toDataURL('image/jpeg', 1));
|
||||
window.open(layer.toDataURL('image/jpeg', 1));
|
||||
});
|
||||
|
||||
layer.add(Ellipse);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
Test.prototype.tests = {
|
||||
'PERFORMANCE - draw rect vs image from image data': function(containerId) {
|
||||
'DRAWING - draw rect vs image from image data': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
@@ -34,7 +34,7 @@ Test.prototype.tests = {
|
||||
endTimer('draw 10,000 images with image object from image data');
|
||||
|
||||
},
|
||||
'PERFORMANCE - draw rect vs image from data url': function(containerId) {
|
||||
'DRAWING - draw rect vs image from data url': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
@@ -57,7 +57,7 @@ Test.prototype.tests = {
|
||||
endTimer('draw 10,000 rects with canvas API');
|
||||
|
||||
startTimer();
|
||||
var url = canvas.toDataURL();
|
||||
var url = layer.toDataURL();
|
||||
endTimer('create data url');
|
||||
|
||||
var imageObj = new Image();
|
||||
@@ -71,7 +71,7 @@ Test.prototype.tests = {
|
||||
}
|
||||
endTimer('draw 10,000 images with image object from data url');
|
||||
},
|
||||
'*ANIMATION - draw 1,000 stars': function(containerId) {
|
||||
'DRAWING - draw 1,000 stars': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
@@ -99,7 +99,7 @@ Test.prototype.tests = {
|
||||
|
||||
endTimer('draw 1,000 stars');
|
||||
},
|
||||
'*ANIMATION - draw 1,000 cached stars': function(containerId) {
|
||||
'DRAWING - draw 1,000 cached stars': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
@@ -107,8 +107,6 @@ Test.prototype.tests = {
|
||||
});
|
||||
var layer = new Kinetic.Layer();
|
||||
|
||||
startTimer();
|
||||
|
||||
var star = new Kinetic.Star({
|
||||
innerRadius: 20,
|
||||
outerRadius: 50,
|
||||
@@ -123,21 +121,23 @@ Test.prototype.tests = {
|
||||
layer.add(star);
|
||||
stage.add(layer);
|
||||
|
||||
var img = star.toImage();
|
||||
console.log('call toImage')
|
||||
star.toImage(function(img) {
|
||||
startTimer();
|
||||
for(var n = 0; n < 1000; n++) {
|
||||
var image = new Kinetic.Image({
|
||||
image: img,
|
||||
x: Math.random() * stage.getWidth(),
|
||||
y: Math.random() * stage.getHeight(),
|
||||
offset: 70
|
||||
});
|
||||
|
||||
for(var n = 0; n < 1000; n++) {
|
||||
var image = new Kinetic.Image({
|
||||
image: img,
|
||||
x: Math.random() * stage.getWidth(),
|
||||
y: Math.random() * stage.getHeight(),
|
||||
offset: 70
|
||||
});
|
||||
layer.add(image);
|
||||
}
|
||||
|
||||
layer.add(image);
|
||||
}
|
||||
layer.draw();
|
||||
|
||||
layer.draw();
|
||||
|
||||
endTimer('draw 1,000 stars');
|
||||
endTimer('draw 1,000 cached stars');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -362,7 +362,7 @@ Test.prototype.tests = {
|
||||
layer.add(group);
|
||||
stage.add(layer);
|
||||
|
||||
var startDataUrl = stage.toDataURL();
|
||||
var startDataUrl = layer.toDataURL();
|
||||
|
||||
warn(startDataUrl === urls[0], 'start data url is incorrect');
|
||||
test(triangle.getId() === 'myTriangle', 'triangle id should be myTriangle');
|
||||
@@ -377,7 +377,7 @@ Test.prototype.tests = {
|
||||
*/
|
||||
layer.draw();
|
||||
|
||||
var endDataUrl = stage.toDataURL();
|
||||
var endDataUrl = layer.toDataURL();
|
||||
warn(endDataUrl === urls[0], 'end data url is incorrect');
|
||||
|
||||
},
|
||||
@@ -1042,7 +1042,9 @@ Test.prototype.tests = {
|
||||
layer.draw();
|
||||
}
|
||||
|
||||
warn(urls[0] === stage.toDataURL(), 'stage data url is incorrect');
|
||||
stage.toDataURL(function(dataUrl) {
|
||||
warn(urls[0] === dataUrl, 'stage data url is incorrect');
|
||||
})
|
||||
warn(urls[0] === layer.toDataURL(), 'layer data url is incorrect');
|
||||
warn(urls[1] === group.toDataURL(), 'group data url is incorrect');
|
||||
warn(urls[1] === circle.toDataURL(), 'shape data url is incorrect');
|
||||
@@ -1989,96 +1991,6 @@ Test.prototype.tests = {
|
||||
};
|
||||
imageObj.src = '../darth-vader.jpg';
|
||||
},
|
||||
'SHAPE - add image with image data': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
height: 200
|
||||
});
|
||||
var layer = new Kinetic.Layer();
|
||||
var group = new Kinetic.Group();
|
||||
|
||||
var circle = new Kinetic.Ellipse({
|
||||
x: 100,
|
||||
y: stage.getHeight() / 2,
|
||||
radius: 70,
|
||||
fill: 'green',
|
||||
stroke: 'black',
|
||||
strokeWidth: 4
|
||||
});
|
||||
|
||||
var circle2 = new Kinetic.Ellipse({
|
||||
x: 150,
|
||||
y: stage.getHeight() / 2,
|
||||
radius: 70,
|
||||
fill: 'yellow',
|
||||
stroke: 'black',
|
||||
strokeWidth: 4
|
||||
});
|
||||
|
||||
group.add(circle);
|
||||
group.add(circle2);
|
||||
layer.add(group);
|
||||
stage.add(layer);
|
||||
|
||||
group.saveImageData();
|
||||
|
||||
var image = new Kinetic.Image({
|
||||
image: group.getImageData(),
|
||||
x: 200,
|
||||
y: 0,
|
||||
draggable: true
|
||||
});
|
||||
|
||||
layer.add(image);
|
||||
layer.draw();
|
||||
|
||||
test(Kinetic.Type._isElement(image.getImage()), 'image property should have been converted to an image element');
|
||||
},
|
||||
'SHAPE - add image with data url': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
container: containerId,
|
||||
width: 578,
|
||||
height: 200
|
||||
});
|
||||
var layer = new Kinetic.Layer();
|
||||
var group = new Kinetic.Group();
|
||||
|
||||
var circle = new Kinetic.Ellipse({
|
||||
x: 100,
|
||||
y: stage.getHeight() / 2,
|
||||
radius: 70,
|
||||
fill: 'green',
|
||||
stroke: 'black',
|
||||
strokeWidth: 4
|
||||
});
|
||||
|
||||
var circle2 = new Kinetic.Ellipse({
|
||||
x: 150,
|
||||
y: stage.getHeight() / 2,
|
||||
radius: 70,
|
||||
fill: 'yellow',
|
||||
stroke: 'black',
|
||||
strokeWidth: 4
|
||||
});
|
||||
|
||||
group.add(circle);
|
||||
group.add(circle2);
|
||||
layer.add(group);
|
||||
stage.add(layer);
|
||||
|
||||
var image = new Kinetic.Image({
|
||||
image: layer.toDataURL(),
|
||||
x: 200,
|
||||
y: 0,
|
||||
draggable: true
|
||||
});
|
||||
|
||||
layer.add(image);
|
||||
layer.draw();
|
||||
|
||||
test(Kinetic.Type._isElement(image.getImage()), 'image property should have been converted to an image element');
|
||||
},
|
||||
'SHAPE - set image fill to color then image': function(containerId) {
|
||||
var imageObj = new Image();
|
||||
imageObj.onload = function() {
|
||||
@@ -2272,10 +2184,18 @@ Test.prototype.tests = {
|
||||
layer.add(group);
|
||||
stage.add(layer);
|
||||
|
||||
test(Kinetic.Type._isElement(poly.toImage()), 'shape toImage() should be an image object');
|
||||
test(Kinetic.Type._isElement(group.toImage()), 'group toImage() should be an image object');
|
||||
test(Kinetic.Type._isElement(layer.toImage()), 'layer toImage() should be an image object');
|
||||
test(Kinetic.Type._isElement(stage.toImage()), 'stage toImage() should be an image object');
|
||||
poly.toImage(function(imageObj) {
|
||||
test(Kinetic.Type._isElement(imageObj), 'shape toImage() should be an image object');
|
||||
});
|
||||
group.toImage(function(imageObj) {
|
||||
test(Kinetic.Type._isElement(imageObj), 'group toImage() should be an image object');
|
||||
});
|
||||
layer.toImage(function(imageObj) {
|
||||
test(Kinetic.Type._isElement(imageObj), 'layer toImage() should be an image object');
|
||||
});
|
||||
stage.toImage(function(imageObj) {
|
||||
test(Kinetic.Type._isElement(imageObj), 'stage toImage() should be an image object');
|
||||
});
|
||||
},
|
||||
'SHAPE - add polygon': function(containerId) {
|
||||
var stage = new Kinetic.Stage({
|
||||
|
||||
Reference in New Issue
Block a user