2012-07-15 00:57:21 +08:00
|
|
|
Test.prototype.tests = {
|
2012-07-27 13:58:38 +08:00
|
|
|
'DRAWING - draw rect': function(containerId) {
|
|
|
|
var stage = new Kinetic.Stage({
|
|
|
|
container: containerId,
|
|
|
|
width: 578,
|
|
|
|
height: 200
|
|
|
|
});
|
|
|
|
var layer = new Kinetic.Layer();
|
|
|
|
|
|
|
|
startTimer();
|
|
|
|
console.profile();
|
|
|
|
for(var n = 0; n < 1000; n++) {
|
|
|
|
var rect = new Kinetic.Rect({
|
|
|
|
x: 10,
|
|
|
|
y: 10,
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
fill: 'yellow',
|
|
|
|
stroke: 'blue'
|
|
|
|
});
|
|
|
|
|
|
|
|
layer.add(rect);
|
|
|
|
}
|
|
|
|
stage.add(layer);
|
2012-07-29 07:08:14 +08:00
|
|
|
|
|
|
|
console.profileEnd();
|
2012-07-27 13:58:38 +08:00
|
|
|
endTimer('add and draw 1,000 Kinetic rectangles');
|
|
|
|
|
|
|
|
},
|
2012-07-29 00:55:21 +08:00
|
|
|
'ANIMATION - test animation frame rate': function(containerId) {
|
2012-07-27 13:58:38 +08:00
|
|
|
var stage = new Kinetic.Stage({
|
|
|
|
container: containerId,
|
|
|
|
width: 578,
|
|
|
|
height: 200
|
|
|
|
});
|
|
|
|
var layer = new Kinetic.Layer();
|
|
|
|
var rect = new Kinetic.Rect({
|
|
|
|
x: 200,
|
|
|
|
y: 100,
|
|
|
|
width: 100,
|
|
|
|
height: 50,
|
|
|
|
fill: 'green',
|
|
|
|
stroke: 'black',
|
|
|
|
strokeWidth: 4
|
|
|
|
});
|
|
|
|
|
|
|
|
layer.add(rect);
|
|
|
|
stage.add(layer);
|
|
|
|
|
|
|
|
var amplitude = 150;
|
|
|
|
var period = 1000;
|
|
|
|
// in ms
|
|
|
|
var centerX = stage.getWidth() / 2 - 100 / 2;
|
|
|
|
|
|
|
|
stage.onFrame(function(frame) {
|
|
|
|
rect.attrs.x = amplitude * Math.sin(frame.time * 2 * Math.PI / period) + centerX;
|
|
|
|
layer.draw();
|
|
|
|
//console.log(frame.timeDiff)
|
|
|
|
});
|
|
|
|
|
|
|
|
stage.start();
|
2012-07-29 07:08:14 +08:00
|
|
|
|
2012-07-27 13:58:38 +08:00
|
|
|
setTimeout(function() {
|
2012-07-29 07:08:14 +08:00
|
|
|
//stage.stop();
|
2012-07-27 13:58:38 +08:00
|
|
|
}, 1000)
|
|
|
|
},
|
2012-07-16 11:12:18 +08:00
|
|
|
'DRAWING - draw rect vs image from image data': function(containerId) {
|
2012-07-15 00:57:21 +08:00
|
|
|
var stage = new Kinetic.Stage({
|
|
|
|
container: containerId,
|
|
|
|
width: 578,
|
|
|
|
height: 200
|
|
|
|
});
|
|
|
|
var layer = new Kinetic.Layer();
|
|
|
|
stage.add(layer);
|
|
|
|
|
|
|
|
var canvas = layer.getCanvas();
|
|
|
|
var context = layer.getContext();
|
|
|
|
|
|
|
|
startTimer();
|
|
|
|
for(var n = 0; n < 10000; n++) {
|
|
|
|
context.fillStyle = 'blue';
|
|
|
|
context.lineWidth = 6;
|
|
|
|
context.strokeStyle = 'red';
|
|
|
|
context.fillRect(10, 10, 100, 100);
|
|
|
|
context.strokeRect(10, 10, 100, 100);
|
|
|
|
}
|
|
|
|
endTimer('draw 10,000 rects with canvas API');
|
|
|
|
|
|
|
|
startTimer();
|
|
|
|
var imageData = context.getImageData(7, 7, 106, 106);
|
|
|
|
endTimer('create image data');
|
|
|
|
|
2012-07-19 14:28:45 +08:00
|
|
|
layer.canvas.clear();
|
2012-07-15 00:57:21 +08:00
|
|
|
|
|
|
|
startTimer();
|
|
|
|
for(var n = 0; n < 10000; n++) {
|
|
|
|
context.putImageData(imageData, 7, 7);
|
|
|
|
}
|
2012-07-17 15:32:26 +08:00
|
|
|
endTimer('draw 10,000 images with putImageData');
|
2012-07-15 00:57:21 +08:00
|
|
|
|
|
|
|
},
|
2012-07-16 11:12:18 +08:00
|
|
|
'DRAWING - draw rect vs image from data url': function(containerId) {
|
2012-07-15 00:57:21 +08:00
|
|
|
var stage = new Kinetic.Stage({
|
|
|
|
container: containerId,
|
|
|
|
width: 578,
|
|
|
|
height: 200
|
|
|
|
});
|
|
|
|
var layer = new Kinetic.Layer();
|
|
|
|
stage.add(layer);
|
|
|
|
|
|
|
|
var canvas = layer.getCanvas();
|
|
|
|
var context = layer.getContext();
|
|
|
|
|
|
|
|
startTimer();
|
|
|
|
for(var n = 0; n < 10000; n++) {
|
|
|
|
context.fillStyle = 'blue';
|
|
|
|
context.lineWidth = 6;
|
|
|
|
context.strokeStyle = 'red';
|
|
|
|
context.fillRect(10, 10, 100, 100);
|
|
|
|
context.strokeRect(10, 10, 100, 100);
|
|
|
|
}
|
|
|
|
endTimer('draw 10,000 rects with canvas API');
|
|
|
|
|
|
|
|
startTimer();
|
2012-07-16 11:12:18 +08:00
|
|
|
var url = layer.toDataURL();
|
2012-07-15 00:57:21 +08:00
|
|
|
endTimer('create data url');
|
|
|
|
|
|
|
|
var imageObj = new Image();
|
2012-07-17 15:32:26 +08:00
|
|
|
imageObj.onload = function() {
|
2012-07-19 14:28:45 +08:00
|
|
|
layer.canvas.clear();
|
2012-07-15 00:57:21 +08:00
|
|
|
|
2012-07-17 15:32:26 +08:00
|
|
|
startTimer();
|
|
|
|
for(var n = 0; n < 10000; n++) {
|
|
|
|
context.drawImage(imageObj, 7, 7, 106, 106, 10, 10, 106, 106);
|
|
|
|
}
|
|
|
|
endTimer('draw 10,000 images with image object from data url');
|
2012-07-15 00:57:21 +08:00
|
|
|
}
|
2012-07-17 15:32:26 +08:00
|
|
|
imageObj.src = url;
|
|
|
|
|
2012-07-15 14:41:16 +08:00
|
|
|
},
|
2012-07-16 11:12:18 +08:00
|
|
|
'DRAWING - draw 1,000 stars': function(containerId) {
|
2012-07-15 14:41:16 +08:00
|
|
|
var stage = new Kinetic.Stage({
|
|
|
|
container: containerId,
|
|
|
|
width: 578,
|
|
|
|
height: 200
|
|
|
|
});
|
|
|
|
var layer = new Kinetic.Layer();
|
|
|
|
|
|
|
|
startTimer();
|
|
|
|
for(var n = 0; n < 1000; n++) {
|
2012-08-01 11:36:36 +08:00
|
|
|
var star = new Kinetic.Star({
|
2012-07-15 14:41:16 +08:00
|
|
|
innerRadius: 20,
|
|
|
|
outerRadius: 50,
|
|
|
|
fill: 'yellow',
|
|
|
|
stroke: 'black',
|
|
|
|
strokeWidth: 5,
|
|
|
|
numPoints: 5,
|
|
|
|
x: Math.random() * stage.getWidth(),
|
2012-07-23 08:05:45 +08:00
|
|
|
y: Math.random() * stage.getHeight(),
|
|
|
|
shadow: {
|
|
|
|
offset: 5,
|
|
|
|
color: 'black',
|
|
|
|
blur: 5,
|
|
|
|
alpha: 0.5
|
|
|
|
}
|
2012-07-15 14:41:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
layer.add(star);
|
|
|
|
}
|
|
|
|
|
|
|
|
stage.add(layer);
|
|
|
|
|
|
|
|
endTimer('draw 1,000 stars');
|
|
|
|
},
|
2012-07-16 11:12:18 +08:00
|
|
|
'DRAWING - draw 1,000 cached stars': function(containerId) {
|
2012-07-15 14:41:16 +08:00
|
|
|
var stage = new Kinetic.Stage({
|
|
|
|
container: containerId,
|
|
|
|
width: 578,
|
|
|
|
height: 200
|
|
|
|
});
|
|
|
|
var layer = new Kinetic.Layer();
|
|
|
|
|
2012-08-01 11:36:36 +08:00
|
|
|
var star = new Kinetic.Star({
|
2012-07-15 14:41:16 +08:00
|
|
|
innerRadius: 20,
|
|
|
|
outerRadius: 50,
|
|
|
|
fill: 'yellow',
|
|
|
|
stroke: 'black',
|
|
|
|
strokeWidth: 5,
|
|
|
|
numPoints: 5,
|
|
|
|
x: 70,
|
2012-07-23 08:05:45 +08:00
|
|
|
y: 70,
|
|
|
|
shadow: {
|
|
|
|
offset: 5,
|
|
|
|
color: 'black',
|
|
|
|
blur: 5,
|
|
|
|
alpha: 0.5
|
|
|
|
}
|
2012-07-15 14:41:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
layer.add(star);
|
|
|
|
stage.add(layer);
|
|
|
|
|
2012-07-22 06:38:25 +08:00
|
|
|
star.toImage({
|
|
|
|
callback: 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
|
|
|
|
});
|
|
|
|
|
|
|
|
layer.add(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
layer.draw();
|
|
|
|
|
|
|
|
endTimer('draw 1,000 cached stars');
|
2012-07-16 11:12:18 +08:00
|
|
|
}
|
|
|
|
});
|
2012-07-29 07:08:14 +08:00
|
|
|
},
|
|
|
|
'*PATH - add map path': function(containerId) {
|
|
|
|
startTimer();
|
|
|
|
var stage = new Kinetic.Stage({
|
|
|
|
container: containerId,
|
|
|
|
width: 1024,
|
|
|
|
height: 480,
|
|
|
|
throttle: 80,
|
|
|
|
scale: 0.5,
|
|
|
|
x: 50,
|
|
|
|
y: 10
|
|
|
|
});
|
|
|
|
var mapLayer = new Kinetic.Layer();
|
|
|
|
|
|
|
|
for(var key in worldMap.shapes) {
|
|
|
|
var c = worldMap.shapes[key];
|
|
|
|
|
2012-08-01 11:36:36 +08:00
|
|
|
var path = new Kinetic.Path({
|
2012-07-29 07:08:14 +08:00
|
|
|
data: c,
|
|
|
|
fill: '#ccc',
|
|
|
|
stroke: '#999',
|
|
|
|
strokeWidth: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
if(key === 'US')
|
|
|
|
test(path.dataArray[0].command === 'M', 'first command should be a moveTo');
|
|
|
|
|
|
|
|
path.on('mouseover', function() {
|
|
|
|
this.setFill('red');
|
|
|
|
mapLayer.draw();
|
|
|
|
});
|
|
|
|
|
|
|
|
path.on('mouseout', function() {
|
|
|
|
this.setFill('#ccc');
|
|
|
|
mapLayer.draw();
|
|
|
|
});
|
|
|
|
|
|
|
|
mapLayer.add(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
stage.add(mapLayer);
|
|
|
|
|
|
|
|
endTimer('time build and to draw map');
|
|
|
|
|
|
|
|
mapLayer.beforeDraw(startTimer);
|
|
|
|
mapLayer.afterDraw(function() {
|
|
|
|
endTimer('redraw layer');
|
|
|
|
});
|
2012-07-15 00:57:21 +08:00
|
|
|
}
|
|
|
|
};
|