konva/test/performance/bunnies.html

177 lines
4.4 KiB
HTML
Raw Normal View History

2019-01-24 21:45:17 +08:00
<!DOCTYPE html>
2014-08-23 18:19:35 +08:00
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
2019-01-24 21:45:17 +08:00
<div id="container"></div>
2020-06-17 01:16:53 +08:00
<script src="../../konva.js"></script>
2020-06-11 00:57:48 +08:00
<!-- <script src="https://unpkg.com/konva@6.0.0/konva.min.js"></script> -->
2019-01-24 21:45:17 +08:00
<script src="http://www.html5canvastutorials.com/lib/stats/stats.js"></script>
<script defer="defer">
2014-08-23 18:19:35 +08:00
var lastTime = 0;
2019-01-24 21:45:17 +08:00
2018-05-24 10:21:32 +08:00
var width = window.innerWidth;
var height = window.innerHeight;
2019-01-24 21:45:17 +08:00
2014-08-23 18:19:35 +08:00
var wabbitTexture;
2019-01-24 21:45:17 +08:00
2014-08-23 18:19:35 +08:00
var bunnys = [];
var gravity = 0.75;
2019-01-24 21:45:17 +08:00
2014-08-23 18:19:35 +08:00
var maxX = width - 10;
var minX = 0;
var maxY = height - 10;
var minY = 0;
2019-01-24 21:45:17 +08:00
2020-06-11 00:57:48 +08:00
var startBunnyCount = 4000;
2014-08-23 18:19:35 +08:00
var isAdding = false;
var count = 0;
var container;
var layer;
var stats;
var amount = 10;
var counter;
2018-05-24 10:21:32 +08:00
2020-06-11 00:57:48 +08:00
Konva.pixelRatio = 1;
2019-01-24 21:45:17 +08:00
var stage = new Konva.Stage({
container: 'container',
width: width - 10,
height: height - 10,
2019-01-24 21:45:17 +08:00
});
layer = new Konva.Layer({ listening: false });
2019-01-24 21:45:17 +08:00
stage.add(layer);
stats = new Stats();
wabbitTexture = new Image();
wabbitTexture.onload = function () {
2019-01-24 21:45:17 +08:00
_handleTextureLoaded();
};
wabbitTexture.src = '../assets/bunny.png';
document.body.appendChild(stats.domElement);
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
window.requestAnimationFrame(update);
counter = document.createElement('div');
counter.className = 'counter';
counter.style.position = 'absolute';
counter.style.top = '50px';
document.body.appendChild(counter);
count = startBunnyCount;
counter.innerHTML = startBunnyCount + ' BUNNIES';
container = stage;
// stage.addChild(container);
stage.on('mousedown', function () {
2019-01-24 21:45:17 +08:00
isAdding = true;
});
stage.on('mouseup', function () {
2019-01-24 21:45:17 +08:00
isAdding = false;
});
document.addEventListener('touchstart', onTouchStart, true);
document.addEventListener('touchend', onTouchEnd, true);
function _handleTextureLoaded(event) {
for (var i = 0; i < startBunnyCount; i++) {
var bunny = new Konva.Image({
image: wabbitTexture,
transformsEnabled: 'position',
x: 10,
y: 10,
2020-06-17 05:20:36 +08:00
perfectDrawEnabled: false,
2014-08-23 18:19:35 +08:00
});
2019-01-24 21:45:17 +08:00
bunny.speedX = Math.random() * 10;
bunny.speedY = Math.random() * 10 - 5;
bunnys.push(bunny);
layer.add(bunny);
}
2019-11-21 02:39:02 +08:00
layer.draw();
2014-08-23 18:19:35 +08:00
}
2019-01-24 21:45:17 +08:00
function onTouchStart(event) {
isAdding = true;
2014-08-23 18:19:35 +08:00
}
2019-01-24 21:45:17 +08:00
function onTouchEnd(event) {
isAdding = false;
2014-08-23 18:19:35 +08:00
}
2019-01-24 21:45:17 +08:00
function update() {
stats.begin();
if (isAdding) {
// add 10 at a time :)
for (var i = 0; i < amount; i++) {
var bunny = new Konva.Image({
image: wabbitTexture,
transformsEnabled: 'position',
x: 0,
y: 0,
2020-06-17 05:20:36 +08:00
perfectDrawEnabled: false,
2019-01-24 21:45:17 +08:00
});
bunny.speedX = Math.random() * 10;
bunny.speedY = Math.random() * 10 - 5;
bunnys.push(bunny);
layer.add(bunny);
count++;
}
counter.innerHTML = count + ' BUNNIES';
}
for (var i = 0; i < bunnys.length; i++) {
var bunny = bunnys[i];
2020-06-11 00:57:48 +08:00
var pos = {
x: bunny.x(),
y: bunny.y(),
};
pos.x = pos.x + bunny.speedX;
pos.y = pos.y + bunny.speedY;
2019-01-24 21:45:17 +08:00
bunny.speedY += gravity;
2020-06-11 00:57:48 +08:00
if (pos.x > maxX - wabbitTexture.width) {
2019-01-24 21:45:17 +08:00
bunny.speedX *= -1;
2020-06-11 00:57:48 +08:00
pos.x = maxX - wabbitTexture.width;
} else if (pos.x < minX) {
2019-01-24 21:45:17 +08:00
bunny.speedX *= -1;
2020-06-11 00:57:48 +08:00
pos.x = minX;
2014-08-23 18:19:35 +08:00
}
2019-01-24 21:45:17 +08:00
2020-06-11 00:57:48 +08:00
if (pos.y > maxY - wabbitTexture.height) {
2019-01-24 21:45:17 +08:00
bunny.speedY *= -0.85;
2020-06-11 00:57:48 +08:00
pos.y = maxY - wabbitTexture.height;
2019-01-24 21:45:17 +08:00
if (Math.random() > 0.5) {
bunny.speedY -= Math.random() * 6;
}
2020-06-11 00:57:48 +08:00
} else if (pos.y < minY) {
2019-01-24 21:45:17 +08:00
bunny.speedY = 0;
2020-06-11 00:57:48 +08:00
pos.y = minY;
2014-08-23 18:19:35 +08:00
}
2020-06-11 00:57:48 +08:00
bunny.position({
x: pos.x,
y: pos.y,
});
2018-05-24 10:21:32 +08:00
}
layer.draw();
2019-01-24 21:45:17 +08:00
requestAnimationFrame(update);
stats.end();
}
2014-08-23 18:19:35 +08:00
</script>
</body>
2019-01-24 21:45:17 +08:00
</html>