Skip to content

Commit

Permalink
added arrow key navigation of examples in development demo
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Aug 13, 2023
1 parent da99acb commit 45cad77
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions demo/src/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ var demo = function(examples, isDev) {
document.title = 'Matter.js Demo' + (isDev ? ' ・ Dev' : '');

if (isDev) {
var buttonSource = demo.dom.buttonSource;
var buttonCompare = buttonSource.cloneNode(true);
// add compare button
var buttonSource = demo.dom.buttonSource,
buttonCompare = buttonSource.cloneNode(true);

buttonCompare.textContent = '⎄';
buttonCompare.title = 'Compare';
buttonCompare.href = '?compare';
Expand All @@ -52,11 +54,29 @@ var demo = function(examples, isDev) {
window.location = '?compare#' + demo.example.id;
event.preventDefault();
});

buttonSource.parentNode.insertBefore(buttonCompare, buttonSource.nextSibling);

// always show debug info
Matter.before('Render.create', function(renderOptions) {
renderOptions.options.showDebug = true;
});

// arrow key navigation of examples
document.addEventListener('keyup', function(event) {
var isBackKey = event.key === 'ArrowLeft' || event.key === 'ArrowUp',
isForwardKey = event.key === 'ArrowRight' || event.key === 'ArrowDown';

if (isBackKey || isForwardKey) {
var direction = isBackKey ? -1 : 1,
currentExampleIndex = demo.examples.findIndex(function(example) {
return example.id === demo.example.id;
}),
nextExample = demo.examples[(demo.examples.length + currentExampleIndex + direction) % demo.examples.length];

MatterTools.Demo.setExample(demo, nextExample);
}
});
}

MatterTools.Demo.start(demo);
Expand Down

0 comments on commit 45cad77

Please sign in to comment.