-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨feat (engine: input) add keyboard input support and examples.
- Loading branch information
1 parent
c6d4a3f
commit 534fd76
Showing
7 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<link | ||
rel="stylesheet" | ||
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/default.min.css" | ||
/> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script> | ||
|
||
<!-- and it's easy to individually load additional languages --> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/languages/go.min.js"></script> | ||
<script src="https://unpkg.com/highlightjs-copy/dist/highlightjs-copy.min.js"></script> | ||
<link | ||
rel="stylesheet" | ||
href="https://unpkg.com/highlightjs-copy/dist/highlightjs-copy.min.css" | ||
/> | ||
<link rel="stylesheet" href="/styles.css" /> | ||
|
||
<script> | ||
hljs.addPlugin(new CopyButtonPlugin()); | ||
hljs.highlightAll(); | ||
</script> | ||
<title>MiniPoint Colors</title> | ||
</head> | ||
<body> | ||
<div id="app"> | ||
Mouse Click Event Examples (Colorful Bees & Gods painting). Toggle | ||
checkbox to see difference. Press 'J' to put point on the canvas | ||
</div> | ||
<label for="">Clear canvas per frame</label | ||
><input type="checkbox" name="checkbox" id="checkbox" checked /> | ||
<div class="main-area"> | ||
<canvas id="canvas"></canvas> | ||
<pre><code class="language-javascript"> | ||
import { Engine, Point } from '../../lib'; | ||
|
||
const engine = new Engine( | ||
document.getElementById('canvas') as HTMLCanvasElement, | ||
{ | ||
engineOptions: { | ||
width: 600, | ||
height: 400, | ||
}, | ||
}, | ||
); | ||
|
||
const { renderer, input } = engine; | ||
|
||
const random = () => { | ||
return Math.random(); | ||
}; | ||
|
||
document.getElementById('checkbox')?.addEventListener('change', (e) => { | ||
engine.options.engineOptions!.clearEachFrame = (e.target as any).checked; | ||
}); | ||
|
||
engine.update = () => { | ||
if (input.keyboard.currentUpKey?.toLowerCase() === 'j') { | ||
const p1 = new Point({ | ||
x: input.mouse.position.x, | ||
y: input.mouse.position.y, | ||
radius: 6, | ||
color: `rgb(${random() * 255}, ${random() * 255}, ${random() * 255})`, | ||
}); | ||
|
||
p1.update = () => { | ||
p1.options.x += random() > 0.5 ? random() * 2 : -random() * 2; | ||
p1.options.y += random() > 0.5 ? random() * 2 : -random() * 2; | ||
}; | ||
|
||
renderer.addObject(p1); | ||
} | ||
}; | ||
|
||
</code></pre> | ||
</div> | ||
<script type="module" src="./script.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Engine, Point } from '../../lib'; | ||
|
||
const engine = new Engine( | ||
document.getElementById('canvas') as HTMLCanvasElement, | ||
{ | ||
engineOptions: { | ||
width: 600, | ||
height: 400, | ||
}, | ||
}, | ||
); | ||
|
||
const { renderer, input } = engine; | ||
|
||
const random = () => { | ||
return Math.random(); | ||
}; | ||
|
||
document.getElementById('checkbox')?.addEventListener('change', (e) => { | ||
engine.options.engineOptions!.clearEachFrame = (e.target as any).checked; | ||
}); | ||
|
||
engine.update = () => { | ||
if (input.keyboard.currentUpKey?.toLowerCase() === 'j') { | ||
const p1 = new Point({ | ||
x: input.mouse.position.x, | ||
y: input.mouse.position.y, | ||
radius: 6, | ||
color: `rgb(${random() * 255}, ${random() * 255}, ${random() * 255})`, | ||
}); | ||
|
||
p1.update = () => { | ||
p1.options.x += random() > 0.5 ? random() * 2 : -random() * 2; | ||
p1.options.y += random() > 0.5 ? random() * 2 : -random() * 2; | ||
}; | ||
|
||
renderer.addObject(p1); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters