|
| 1 | +/** |
| 2 | +* A Matter.js multi example testbed based on MatterTools. |
| 3 | +* |
| 4 | +* Tool to interactively test multiple examples at once. |
| 5 | +* |
| 6 | +* USAGE: [host]?multi#[example1,example2,example3...] |
| 7 | +* e.g. http://localhost:8000/?multi#mixed |
| 8 | +* |
| 9 | +* @module Multi |
| 10 | +*/ |
| 11 | + |
| 12 | +var MatterTools = require('matter-tools'); |
| 13 | + |
| 14 | +var multi = function(examples, isDev) { |
| 15 | + var demo = MatterTools.Demo.create({ |
| 16 | + toolbar: { |
| 17 | + title: 'matter-js ・ ' + (isDev ? 'dev' : '') + ' ・ multi', |
| 18 | + url: 'https://github.com/liabru/matter-js', |
| 19 | + reset: false, |
| 20 | + source: false, |
| 21 | + inspector: false, |
| 22 | + tools: false, |
| 23 | + fullscreen: false, |
| 24 | + exampleSelect: false |
| 25 | + }, |
| 26 | + tools: { |
| 27 | + inspector: false, |
| 28 | + gui: false |
| 29 | + }, |
| 30 | + inline: false, |
| 31 | + preventZoom: false, |
| 32 | + resetOnOrientation: false, |
| 33 | + routing: false, |
| 34 | + startExample: false |
| 35 | + }); |
| 36 | + |
| 37 | + var urlHash = window.location.hash, |
| 38 | + allExampleIds = examples.map(function(example) { return example.id; }), |
| 39 | + exampleIds = urlHash ? urlHash.slice(1).split(',') : allExampleIds.slice(0, 4), |
| 40 | + exampleCount = Math.ceil(Math.sqrt(exampleIds.length)); |
| 41 | + |
| 42 | + var container = document.createElement('div'); |
| 43 | + container.style = 'display: grid; grid-template-columns: repeat(' + exampleCount + ', 1fr); grid-template-rows: repeat(' + exampleCount + ', 1fr); max-width: calc(100vmin * 1.25 - 40px); max-height: 100vmin;'; |
| 44 | + |
| 45 | + demo.dom.root.appendChild(container); |
| 46 | + document.body.appendChild(demo.dom.root); |
| 47 | + |
| 48 | + document.title = 'Matter.js Multi' + (isDev ? ' ・ Dev' : ''); |
| 49 | + console.info('Demo.Multi: matter-js@' + Matter.version); |
| 50 | + |
| 51 | + // always show debug info |
| 52 | + Matter.before('Render.create', function(renderOptions) { |
| 53 | + renderOptions.options.showDebug = true; |
| 54 | + }); |
| 55 | + |
| 56 | + Matter.after('Runner.create', function() { |
| 57 | + this.isFixed = true; |
| 58 | + }); |
| 59 | + |
| 60 | + var runExamples = function(exampleIds) { |
| 61 | + for (var i = 0; i < exampleIds.length; i += 1) { |
| 62 | + var exampleId = exampleIds[i], |
| 63 | + example = examples.find(function(example) { return example.id === exampleId; }); |
| 64 | + |
| 65 | + if (!example) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + var canvas = example.init().render.canvas; |
| 70 | + container.appendChild(canvas); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + runExamples(exampleIds); |
| 75 | + |
| 76 | + // arrow key navigation of examples |
| 77 | + document.addEventListener('keyup', function(event) { |
| 78 | + var isBackKey = event.key === 'ArrowLeft' || event.key === 'ArrowUp', |
| 79 | + isForwardKey = event.key === 'ArrowRight' || event.key === 'ArrowDown'; |
| 80 | + |
| 81 | + if (isBackKey || isForwardKey) { |
| 82 | + var direction = isBackKey ? -1 : 1; |
| 83 | + |
| 84 | + var currentExampleIndex = allExampleIds.findIndex(function(exampleId) { |
| 85 | + return exampleId === exampleIds[0]; |
| 86 | + }); |
| 87 | + |
| 88 | + var nextExampleId = (allExampleIds.length + currentExampleIndex + direction * exampleIds.length) % allExampleIds.length, |
| 89 | + nextExamples = allExampleIds.slice(nextExampleId, (nextExampleId + exampleIds.length) % allExampleIds.length); |
| 90 | + |
| 91 | + window.location.hash = nextExamples.join(','); |
| 92 | + window.location.reload(); |
| 93 | + } |
| 94 | + }); |
| 95 | +}; |
| 96 | + |
| 97 | +module.exports = { multi: multi }; |
0 commit comments