konva/test/performance/common/random-squares.js

105 lines
2.7 KiB
JavaScript
Raw Normal View History

2013-11-30 05:49:59 +08:00
var stage;
var circlesLayer;
var circles;
var stats;
var width = 500;
var height = 300;
2015-01-27 15:07:51 +08:00
var VERSION = Konva.version === 'dev' ? 'new' : 'old';
2013-11-30 05:49:59 +08:00
2014-02-27 08:49:18 +08:00
window.requestAnimFrame = (function(){
2013-11-30 05:49:59 +08:00
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
window.setTimeout(callback, 1000 / 30);
};
})();
function fpsCounter() {
//fps stat---------------------------
stats = new Stats();
stats.setMode(0);
stats.domElement.style.position = 'fixed';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
$('html').append( stats.domElement );
}
$(function() {
fpsCounter();
make_stage();
var colors = ["red", "orange", "yellow", "green", "blue", "cyan", "purple"];
var colorIndex = 0;
circles = [];
for(var n = 0; n < 1500; n++) {( function() {
var color = colors[colorIndex++];
if(colorIndex >= colors.length) {
colorIndex = 0;
}
2014-02-27 08:49:18 +08:00
var shape = make_shape(color);
2013-11-30 05:49:59 +08:00
circlesLayer.add(shape);
circles.push(shape);
}());
}
stage.add(circlesLayer);
animate((new Date()).getTime());
});
function animate(lastTime){
stats.begin();
// update
var date = new Date();
var time = date.getTime();
var timeDiff = time - lastTime;
var period = timeDiff/1000; //times per second, our period
for (var i = 0; i < circles.length; i++) {
2014-02-27 08:49:18 +08:00
var x = Math.round(Math.random() * width);
var y = Math.round(Math.random() * height);
2014-03-10 04:15:01 +08:00
circles[i].setPosition({x: x, y: y});
2013-11-30 05:49:59 +08:00
}
lastTime = time;
circlesLayer.draw();
stats.end();
requestAnimFrame(function(){
animate(lastTime);
});
}
function make_shape(color) {
2015-01-27 15:07:51 +08:00
return new Konva.Rect({
2014-03-10 04:15:01 +08:00
fill: color,
width: 10,
height: 10
});
2013-11-30 05:49:59 +08:00
}
function make_stage() {
2015-01-27 15:07:51 +08:00
stage = new Konva.Stage({
2014-03-10 04:15:01 +08:00
container: "container",
width: width,
height: height
});
2013-11-30 05:49:59 +08:00
if (VERSION === 'new') {
2014-03-10 04:15:01 +08:00
console.log('create fast layer')
2015-01-27 15:07:51 +08:00
circlesLayer = new Konva.FastLayer();
2014-03-10 04:15:01 +08:00
}
else {
console.log('create normal layer')
2015-01-27 15:07:51 +08:00
circlesLayer = new Konva.Layer();
2013-11-30 05:49:59 +08:00
}
}