konva/test/bunnies.html

201 lines
5.1 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>
<script src="http://www.html5canvastutorials.com/lib/stats/stats.js"></script>
2023-06-05 23:16:28 +08:00
<script type="module">
import Konva from '../src/index.ts';
2021-05-04 06:09:18 +08:00
Konva.isUnminified = false;
2021-05-04 21:57:03 +08:00
// Konva.autoDrawEnabled = trye;
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();
};
2021-05-04 06:09:18 +08:00
wabbitTexture.src = 'https://konvajs.org/assets/bunny.png';
2019-01-24 21:45:17 +08:00
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,
2020-06-24 06:22:28 +08:00
width: wabbitTexture.width,
height: wabbitTexture.height,
2023-06-05 23:25:36 +08:00
fill: 'yellow',
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);
}
2020-06-24 06:22:28 +08:00
// layer.draw();
// console.clear();
// bunnys.forEach((b) => {
// b.position({
// x: Math.random() * window.innerWidth,
// y: Math.random() * window.innerHeight,
// });
// });
// layer.draw();
// bunnys.forEach((b) => {
// b.position({
// x: Math.random() * window.innerWidth,
// y: Math.random() * window.innerHeight,
// });
// });
// 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,
2020-06-24 06:22:28 +08:00
width: wabbitTexture.width,
height: wabbitTexture.height,
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
}
2021-05-04 21:57:03 +08:00
if (!Konva.autoDrawEnabled) {
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>