toDataURL() is now synchronous, and works with all nodes, including the stage, layers, groups, and shapes. This also sets things up nicely for node caching. You can now cache anything, including the whole stage, layers, groups, or shapes, manifested as Kinetic Images that were instantiated with data urls

This commit is contained in:
Eric Rowell
2012-07-14 18:10:37 -07:00
parent d62df7ba5c
commit 4692c51c74
11 changed files with 295 additions and 219 deletions

File diff suppressed because one or more lines are too long

View File

@@ -38,43 +38,40 @@ Test.prototype.tests = {
circle.on('dragend', function() {
dragEnd = true;
});
stage.toDataURL(function(startDataUrl) {
warn(urls[0] === startDataUrl, 'start data url is incorrect');
/*
* simulate drag and drop
*/
stage._mousedown({
clientX: 380,
clientY: 98
});
test(!dragStart, 'dragstart event should not have been triggered');
test(!dragMove, 'dragmove event should not have been triggered');
test(!dragEnd, 'dragend event should not have been triggered');
stage._mousemove({
clientX: 100,
clientY: 98
});
test(dragStart, 'dragstart event was not triggered');
test(dragMove, 'dragmove event was not triggered');
test(!dragEnd, 'dragend event should not have been triggered');
stage._mouseup({
clientX: 100,
clientY: 98
});
test(dragStart, 'dragstart event was not triggered');
test(dragMove, 'dragmove event was not triggered');
test(dragEnd, 'dragend event was not triggered');
stage.toDataURL(function(endDataUrl) {
warn(urls[1] === endDataUrl, 'end data url is incorrect');
});
startDataUrl = stage.toDataURL();
warn(urls[0] === startDataUrl, 'start data url is incorrect');
/*
* simulate drag and drop
*/
stage._mousedown({
clientX: 380,
clientY: 98
});
test(!dragStart, 'dragstart event should not have been triggered');
test(!dragMove, 'dragmove event should not have been triggered');
test(!dragEnd, 'dragend event should not have been triggered');
stage._mousemove({
clientX: 100,
clientY: 98
});
test(dragStart, 'dragstart event was not triggered');
test(dragMove, 'dragmove event was not triggered');
test(!dragEnd, 'dragend event should not have been triggered');
stage._mouseup({
clientX: 100,
clientY: 98
});
test(dragStart, 'dragstart event was not triggered');
test(dragMove, 'dragmove event was not triggered');
test(dragEnd, 'dragend event was not triggered');
var endDataUrl = stage.toDataURL();
warn(urls[1] === endDataUrl, 'end data url is incorrect');
},
'DRAG AND DROP - cancel drag and drop by setting draggable to false': function(containerId) {
var stage = new Kinetic.Stage({
@@ -179,31 +176,30 @@ Test.prototype.tests = {
stage.add(layer);
stage.toDataURL(function(startDataUrl) {
warn(urls[0] === startDataUrl, 'start data url is incorrect');
var startDataUrl = stage.toDataURL();
warn(urls[0] === startDataUrl, 'start data url is incorrect');
/*
* simulate drag and drop
*/
stage._mousedown({
clientX: 399,
clientY: 96
});
stage._mousemove({
clientX: 210,
clientY: 109
});
stage._mouseup({
clientX: 210,
clientY: 109
});
stage.toDataURL(function(endDataUrl) {
warn(urls[1] === endDataUrl, 'end data url is incorrect');
});
/*
* simulate drag and drop
*/
stage._mousedown({
clientX: 399,
clientY: 96
});
stage._mousemove({
clientX: 210,
clientY: 109
});
stage._mouseup({
clientX: 210,
clientY: 109
});
var endDataUrl = stage.toDataURL()
warn(urls[1] === endDataUrl, 'end data url is incorrect');
},
'EVENTS - modify fill stroke and stroke width on hover with circle': function(containerId) {
var urls = dataUrls['EVENTS - modify fill stroke and stroke width on hover with circle'];
@@ -239,18 +235,16 @@ Test.prototype.tests = {
layer.add(circle);
stage.add(layer);
stage.toDataURL(function(startDataUrl) {
warn(startDataUrl === urls[0], 'start data url is incorrect');
var startDataUrl = stage.toDataURL();
warn(startDataUrl === urls[0], 'start data url is incorrect');
stage._mousemove({
clientX: 377,
clientY: 101
});
stage.toDataURL(function(endDataUrl) {
warn(urls[1] === endDataUrl, 'end data url is incorrect');
});
stage._mousemove({
clientX: 377,
clientY: 101
});
var endDataUrl = stage.toDataURL();
warn(urls[1] === endDataUrl, '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({

View File

@@ -1468,14 +1468,7 @@ Test.prototype.tests = {
});
Ellipse.on('click', function() {
stage.toDataURL(function(dataUrl) {
/*
* here you can do anything you like with the data url.
* In this tutorial we'll just open the url with the browser
* so that you can see the result as an image
*/
window.open(dataUrl);
});
window.open(stage.toDataURL());
});
layer.add(Ellipse);
@@ -1499,14 +1492,7 @@ Test.prototype.tests = {
});
Ellipse.on('click', function() {
stage.toDataURL(function(dataUrl) {
/*
* here you can do anything you like with the data url.
* In this tutorial we'll just open the url with the browser
* so that you can see the result as an image
*/
window.open(dataUrl);
}, 'image/jpeg', 0);
window.open(stage.toDataURL('image/jpeg', 0));
});
layer.add(Ellipse);
@@ -1530,14 +1516,7 @@ Test.prototype.tests = {
});
Ellipse.on('click', function() {
stage.toDataURL(function(dataUrl) {
/*
* here you can do anything you like with the data url.
* In this tutorial we'll just open the url with the browser
* so that you can see the result as an image
*/
window.open(dataUrl);
}, 'image/jpeg', 1);
window.open(stage.toDataURL('image/jpeg', 1));
});
layer.add(Ellipse);

View File

@@ -358,31 +358,28 @@ Test.prototype.tests = {
id: 'myTriangle'
});
stage.add(layer);
layer.add(group);
group.add(triangle);
layer.add(group);
stage.add(layer);
var startDataUrl = stage.toDataURL();
warn(startDataUrl === urls[0], 'start data url is incorrect');
test(triangle.getId() === 'myTriangle', 'triangle id should be myTriangle');
//console.log(stage.toJSON())
var expectedJson = '{"attrs":{"width":578,"height":200,"throttle":80,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"offset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"throttle":80,"clearBeforeDraw":true,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"offset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"offset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"offset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false,"fill":"#00D2FF","stroke":"black","strokeWidth":4,"id":"myTriangle"},"nodeType":"Shape"}]}]}]}';
test(stage.toJSON() === expectedJson, "problem serializing stage with custom shape");
/*
* test redrawing layer after serialization
* drawing should be the same
*/
layer.draw();
stage.toDataURL(function(startDataUrl) {
warn(startDataUrl === urls[0], 'start data url is incorrect');
var endDataUrl = stage.toDataURL();
warn(endDataUrl === urls[0], 'end data url is incorrect');
test(triangle.getId() === 'myTriangle', 'triangle id should be myTriangle');
//console.log(stage.toJSON())
var expectedJson = '{"attrs":{"width":578,"height":200,"throttle":80,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"offset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"throttle":80,"clearBeforeDraw":true,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"offset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"offset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"offset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false,"fill":"#00D2FF","stroke":"black","strokeWidth":4,"id":"myTriangle"},"nodeType":"Shape"}]}]}]}';
test(stage.toJSON() === expectedJson, "problem serializing stage with custom shape");
/*
* test redrawing layer after serialization
* drawing should be the same
*/
layer.draw();
stage.toDataURL(function(endDataUrl) {
warn(endDataUrl === urls[0], 'end data url is incorrect');
});
});
},
'STAGE - load stage with custom shape using json': function(containerId) {
var stage = new Kinetic.Stage({
@@ -1011,7 +1008,7 @@ Test.prototype.tests = {
layer.draw();
},
'LAYER - set clearBeforeDraw to false': function(containerId) {
'LAYER - set clearBeforeDraw to false, and test toDataURL for stage, layer, group, and shape': function(containerId) {
var urls = dataUrls['LAYER - set clearBeforeDraw to false'];
var stage = new Kinetic.Stage({
@@ -1025,6 +1022,8 @@ Test.prototype.tests = {
throttle: 999
});
var group = new Kinetic.Group();
var circle = new Kinetic.Ellipse({
x: 100,
y: stage.getHeight() / 2,
@@ -1034,7 +1033,8 @@ Test.prototype.tests = {
strokeWidth: 4
});
layer.add(circle);
group.add(circle);
layer.add(group);
stage.add(layer);
for(var n = 0; n < 20; n++) {
@@ -1042,9 +1042,10 @@ Test.prototype.tests = {
layer.draw();
}
stage.toDataURL(function(dataUrl) {
warn(urls[0] === dataUrl, 'data url is incorrect');
});
warn(urls[0] === stage.toDataURL(), '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');
},
'LAYER - throttling': function(containerId) {
var stage = new Kinetic.Stage({
@@ -2021,16 +2022,16 @@ Test.prototype.tests = {
stage.add(layer);
group.saveImageData();
var image = new Kinetic.Image({
image: group.getImageData(),
x: 200,
y: 0,
draggable: true
});
layer.add(image);
layer.draw();
layer.draw();
},
'SHAPE - set image fill to color then image': function(containerId) {
var imageObj = new Image();