Skip to content

Instantly share code, notes, and snippets.

@mitchallen
mitchallen / app.js
Last active December 16, 2021 03:19
ThreeJS hello-world app.js
// File: app.js
// Author: Mitch Allen
// 1. Add reminder for how to reference the script
/*
In a browser must use script type="module" parameter:
<script type="module" src="./src/app.js"></script>
*/
@mitchallen
mitchallen / cube-scene.js
Last active December 16, 2021 03:18
ThreeJS cube-scene.js example
// File: cube-scene.js
// Author: Mitch Allen
// 1. Import the cube class factory
import {CubeFactory} from './cube.js';
// 2. Define and export a scene factory class
export class CubeSceneFactory {
// 3. Define a static create method to return new scenes filled with cubes
@mitchallen
mitchallen / cube-example-snippet.js
Created December 16, 2021 03:10
ThreeJS cube example snippet
var cubeOptions = [
{ color: "#FF0000" },
{ color: "#00FF00", width: 0.5, height: 2, depth: 0.5 },
{ color: "#0000FF", width: 2, height: 0.5, depth: 0.5 },
{ color: "#FF00FF", width: 0.5, height: 0.5, depth: 2 },
{ color: "#FFFF00", translateX: 3.0 },
{ color: "#FF6619", translateX: -3.0 },
{ color: "#AAAAAA", translateY: 2.0, translateZ: -0.05, width: 0.5, height: 0.5, depth: 2 },
{ color: "#04D9FF", translateY: -2.0, translateZ: 0.05, width: 0.5, height: 0.5, depth: 2 },
];
@mitchallen
mitchallen / app.css
Created December 16, 2021 03:07
threejs-hello app.css
body {
margin: 0;
}
canvas {
width: 100%;
height: 100%;
}
@mitchallen
mitchallen / index.html
Created December 16, 2021 03:04
threejs-hello index.html
<html>
<head>
<title>threejs-hello</title>
<link rel="stylesheet" href="./css/app.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r122/three.min.js"></script>
</head>
<body>
<script type="module" src="./app.js"></script>
</body>
</html>
@mitchallen
mitchallen / cube.js
Last active December 16, 2021 03:02
// File: cube.js
// Author: Mitch Allen
// 1. Define and export a cube factory class
export class CubeFactory {
// 2. Define a static create method to return new cubes
static create( spec = {} ) {
// 3. Setup default values or use arguments
"use strict";
var five = require("johnny-five"),
board = new five.Board(),
servos = null,
servoPins = [9, 10],
// servoPins = [9], // if you have only one servo
express = require('express'),
app = express(),
port = 8000;
"use strict";
var five = require("johnny-five"),
board = new five.Board(),
led = null,
express = require('express'),
app = express(),
port = 8000;
board.on("ready", function() {
@mitchallen
mitchallen / reduce-count.js
Created December 24, 2018 16:01
Use reduce to count the occurrence of an item in a list.
/*
Using reduce to count the occurrence of an item in a list.
[ 'd', 'a', 'b', 'a', 'b', 'c', 'd', 'b', 'd', 'd' ]
a occurs 2 times in the list.
b occurs 3 times in the list.
c occurs 1 time in the list.
d occurs 4 times in the list.
*/
@mitchallen
mitchallen / reduce.js
Last active December 24, 2018 13:25
An example of using reduce in JavaScript to combine child arrays
/*
* An example of using reduce to combine child arrays
*
* Expected Output:
SOURCE:
[
{
"name": "alpha",
"kids": [