|
| 1 | +<html> |
| 2 | +<head> |
| 3 | + <link type="text/css" rel="stylesheet" href="../statics/css/style.css"/> |
| 4 | + <title>FlyWeight</title> |
| 5 | +</head> |
| 6 | +<body> |
| 7 | +<div id="source"> |
| 8 | + <h2>Source</h2> |
| 9 | + <pre> |
| 10 | +const CANVAS_SIZE = 600; |
| 11 | +const TREES_TO_DRAW = 99900; |
| 12 | +const TREE_TYPES = 2; |
| 13 | +const forest = new Forest(); |
| 14 | + |
| 15 | +const getAmountOfTreesToRender = (amount, types) => { |
| 16 | + return Math.floor(amount / types); |
| 17 | +}; |
| 18 | + |
| 19 | +const random = (min, max) => { |
| 20 | + return min + (Math.random() * ((max - min) + 1)); |
| 21 | +} |
| 22 | +const renderForest = (forest, canvas) => { |
| 23 | + for(let i = 0; i < getAmountOfTreesToRender(TREES_TO_DRAW, TREE_TYPES); i++) { |
| 24 | + forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE), 'Red Maple', 'red', 'Red Maple texture'); |
| 25 | + forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE), 'Gray Birch', 'gray', 'Gray Birch texture stub'); |
| 26 | + } |
| 27 | + |
| 28 | + forest.render(canvas); |
| 29 | + |
| 30 | + console.log(TREES_TO_DRAW + ' trees rendered'); |
| 31 | + console.log('Memory usage:'); |
| 32 | + console.log('Tree size (8 bytes) * ' + TREES_TO_DRAW + '+ TreeTypes size (~30 bytes) * ' + TREE_TYPES); |
| 33 | + console.log('Total: ' + ((TREES_TO_DRAW * 8 + TREE_TYPES * 30) / 1024 / 1024) + 'MB (instead of ' + ((TREES_TO_DRAW * 38) / 1024 / 1024) + 'MB)'); |
| 34 | +} |
| 35 | + |
| 36 | +renderForest(new Forest(), document.createElement('canvas')); |
| 37 | + </pre> |
| 38 | +</div> |
| 39 | +<div id="console"> |
| 40 | + <h2>Console</h2> |
| 41 | + <ul></ul> |
| 42 | + <h1>FlyWeight</h1> |
| 43 | +</div> |
| 44 | + |
| 45 | + |
| 46 | +<script type="text/javascript" src="../statics/js/utils.js"></script> |
| 47 | +<script type="text/javascript" src="dist/scripts/main.js"></script> |
| 48 | +</body> |
| 49 | +</html> |
0 commit comments