Skip to content

Commit 0fec9f9

Browse files
author
Raphael Brand
committed
add threejs CanvasRenderer
1 parent 76b3d95 commit 0fec9f9

File tree

8 files changed

+79
-46
lines changed

8 files changed

+79
-46
lines changed

demo/Gulpfile.js

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,23 @@ gulp.task('js-vendor', function () {
5757
(typeof vendorConfig.js.src == 'string' || vendorConfig.js.src.length === 0)) {
5858
throw new Error('no vendor scripts added. \nvendor.json:\nuse src["file.a.js"]')
5959
}
60-
return gulp.src(vendorConfig.js.src)
61-
.pipe(uglifyjs({ output: { comments: /^!|@preserve|@license|@cc_on/i } }))
62-
.pipe(rename({ suffix: '.min' }))
60+
vendorConfig.js.src.map((value) => {
61+
console.log('Building', value, '...');
62+
let suffix = ''
63+
suffix = (!/\.min/.test(value) ? '.min' : '');
64+
65+
gulp.src(value)
66+
.pipe(uglifyjs({ output: { comments: /^!|@preserve|@?license|@cc_on/i } }))
67+
.pipe(rename({ "suffix": suffix }))
6368
.pipe(gulp.dest(vendorConfig.js.dest))
69+
});
6470
});
6571

6672
gulp.task('js', function () {
6773
return gulp.src([
6874
'app/js/test-module.js',
6975
'app/js/acceleration.js',
76+
'app/js/three-scene.js',
7077
'app/js/main.js']
7178
)
7279
.pipe(concat('main.js'))
@@ -86,31 +93,3 @@ gulp.task('default', ['sass', 'js', 'templates'], function () {
8693
gulp.watch('./app/sass/*.sass', ['sass']);
8794
gulp.watch('./app/*.pug', ['pug-watch']);
8895
});
89-
90-
91-
/*
92-
gulp.task('uglify-vendor', function () {
93-
94-
console.dir(arguments);
95-
return;
96-
let optionFiles;
97-
98-
optionFiles = process.argv.filter((option, index, arr) => {
99-
if (/\-\-file/gi.test(arr[index - 1])) {
100-
//optionFile = index+1;
101-
return arr[index];
102-
}
103-
return false;
104-
});
105-
if (optionFiles.length) {
106-
optionFiles.map((src) => {
107-
console.log(src);
108-
return gulp.src(src) // could do this cli-wise with process.argv
109-
.pipe(uglifyjs({ output: { comments: /^!|@preserve|@license|@cc_on/i } }))
110-
.pipe(rename(src.replace(/.+\/(\w+)\.js$/gi, '$1.min.js')))
111-
.pipe(gulp.dest('./dist/js/vendor', { overwrite: true }));
112-
});
113-
}
114-
115-
});
116-
*/

demo/app/index.pug

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ html
33
title Canvas - Pacman-alike-playfield (work-in-progress)
44
link(href='https://fonts.googleapis.com/css?family=Fresca')
55
link(href='/css/main.css', rel='stylesheet')
6+
script(src='https://threejs.org/build/three.min.js')
7+
script(src='https://threejs.org/examples/js/renderers/Projector.js')
8+
script(src='https://threejs.org/examples/js/renderers/CanvasRenderer.js')
69
script(type='text/javascript', data-main='js/main', src='js/vendor/require.min.js')
710
body
811
.container
912
.jumbotron
1013
.heading1.animation Hello World!
11-
1214
canvas#canvas(width=300,height=300)
13-
15+
#perspective
16+
#perspective_stats
1417
.btnWrap
1518
button.left='left'
1619
button.up='up'

demo/app/js/acceleration.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ define('acceleration', () => {
3131
return true;
3232
};
3333
var onKeyDown = function onKeyDown(e) {
34-
var code = codes[e.keyCode] != undefined ? codes[e.keyCode] : e.target.className;
34+
var code = codes[e.keyCode] ? codes[e.keyCode] : e.target.className;
3535

3636
move(code) && draw() && setTimeout(function () {
3737
Array.from(document.querySelectorAll(".btnWrap button")).forEach(function (el) {
3838
return el.classList.remove("active");
3939
});
4040
return true;
41-
}, 400) && document.querySelector("." + code).classList.add("active");
41+
}, 400) && document.querySelector('.' + code).classList.add("active");
4242
setTimeout(function () {
43-
return document.querySelector("." + code).blur();
43+
return document.querySelector('.' + code).blur();
4444
}, 400);
4545
};
4646

@@ -85,7 +85,7 @@ define('acceleration', () => {
8585
level[current.y][current.x] = free;
8686
level[playerPosition.y][playerPosition.x] = player;
8787
playerAt = playerPosition;
88-
console.clear();
88+
//console.clear();
8989
console.log("Player has moved " + direction);
9090
}
9191
return hasMoved;
@@ -121,7 +121,7 @@ define('acceleration', () => {
121121

122122
//move();
123123
//draw();
124-
console.clear();
124+
//console.clear();
125125
document.querySelector(".up").focus();
126126
setTimeout(function (el) {
127127
el.classList.remove("active"), el.blur();

demo/app/js/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ require(['test-module', 'acceleration'], function(test, acc) {
1616
]);
1717
acc.draw() && acc.printMap()
1818
});
19+
require(['three-scene'], (Scene) => {
20+
Scene.animate({rotation:{speed:{x:0.01, y:0.01}}})
21+
})

demo/app/js/three-scene.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
define('three-scene', () => {
2+
var scene, camera, renderer, container;
3+
var geometry, material, cube, w, h;
4+
w = h = 300;
5+
6+
init();
7+
8+
function init() {
9+
scene = new THREE.Scene();
10+
camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
11+
container = document.createElement('div');
12+
document.getElementById('perspective').appendChild(container);
13+
14+
gemoetry = new THREE.BoxGeometry(50, 50, 50);
15+
material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
16+
cube = new THREE.Mesh(gemoetry, material);
17+
scene.add(cube);
18+
renderer = new THREE.CanvasRenderer();
19+
console.log(renderer);
20+
renderer.setSize(w, h);
21+
camera.position.z = 200;
22+
23+
container.appendChild(renderer.domElement);
24+
25+
renderer.render(scene, camera);
26+
27+
}
28+
29+
let currentFrame = 0;
30+
let animate;
31+
32+
animate = (params) => {
33+
requestAnimationFrame(() => animate(params));
34+
cube.rotation.x += params.rotation.speed.x;
35+
cube.rotation.y += params.rotation.speed.y;
36+
//console.info(cube.rotation.y, scene, camera, cube);
37+
scene.add(cube);
38+
renderer.render(scene, camera);
39+
document.querySelector('#perspective_stats').innerHTML = cube.rotation.x;
40+
}
41+
return {
42+
"animate": animate
43+
}
44+
45+
console.log(currentFrame);
46+
})

demo/app/sass/main.sass

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
@import 'acceleration';
2+
#perspective
3+
position: absolute
4+
top: 0px
5+
right: 0px

demo/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "Gulpfile.js",
66
"private": true,
77
"scripts": {
8-
"start": "npm run check-vendor && gulp",
8+
"start": "npm run check-vendor && npm run vendor && gulp",
99
"check-vendor": "test -d node_modules || (npm install) || (echo 'npm not installed. Please run \"npm install -g npm\"' && exit 1)",
1010
"vendor": "gulp vendor-scripts",
1111
"css": "gulp sass",
@@ -17,19 +17,18 @@
1717
},
1818
"author": "Raphael Brand<info@raphaelbrand.de>",
1919
"license": "MIT",
20-
"devDependencies": {
20+
"devDependencies": {
2121
"browser-sync": "^2.17.5",
2222
"gulp": "^3.9.1",
2323
"gulp-concat": "^2.6.1",
2424
"gulp-pug": "^3.1.0",
2525
"gulp-rename": "^1.2.2",
2626
"gulp-sass": "^2.3.2",
2727
"gulp-uglify": "^3.0.0",
28-
"nodemon": "^1.11.0"
28+
"nodemon": "^1.11.0",
29+
"requirejs": "^2.3.3"
2930
},
3031
"dependencies": {
31-
"requirejs": "^2.3.3",
32-
"bootstrap-sass": "^3.3.7",
33-
"three": "^0.86.0"
32+
"bootstrap-sass": "^3.3.7"
3433
}
3534
}

demo/vendor.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"js": {
33
"src": [
4-
"node_modules/requirejs/require.js",
5-
"node_modules/three/build/three.min.js"
4+
"node_modules/requirejs/require.js"
65
],
76
"dest": "./dist/js/vendor"
87
},

0 commit comments

Comments
 (0)