-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain-2d.js
More file actions
85 lines (72 loc) · 2.42 KB
/
Copy pathmain-2d.js
File metadata and controls
85 lines (72 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var Window = require('pex-sys/Window')
var PerspCamera = require('pex-cam/PerspCamera')
var Arcball = require('pex-cam/Arcball')
var createCube = require('primitive-cube')
var glslify = require('glslify-promise')
var isBrowser = require('is-browser')
var random = require('pex-random')
Window.create({
settings: {
width: 1280,
height: 720,
type: '2d'
},
init: function() {
var ctx = this.getContext()
ctx.strokeStyle = '#777'
var w = this.getWidth()
var h = this.getHeight()
ctx.fillStyle = '#EEE'
ctx.fillRect(0, 0, w, h)
var MAX_DEPTH = 10
var rect = [10, 10, w-20, h-20, 0] //x, y, w, h, depth
function divide(parent, rects) {
rects.push(parent);
var depth = parent[4]
var shouldDivide = random.chance(1/(depth+1));
if (depth <= 1) {
shouldDivide = true
}
if (depth >= MAX_DEPTH || !shouldDivide) {
return rects
}
var numDivisions = random.int(2, 5)
var horizontal = random.chance(0.5);
if (depth == 0) horizontal = false;
if (depth == 1) horizontal = true;
for(var i=0; i<numDivisions; i++) {
var child = null
if (horizontal) {
child = [
parent[0] + parent[2] * i * 1 / numDivisions,
parent[1],
parent[2] * 1 / numDivisions,
parent[3],
depth + 1
]
}
else {
child = [
parent[0],
parent[1] + parent[3] * i * 1 / numDivisions,
parent[2],
parent[3] * 1 / numDivisions,
depth + 1
]
}
divide(child, rects)
}
return rects
}
var rects = divide(rect, [])
console.log(rects.length)
ctx.strokeStyle = '#666'
rects.map(function(rect) {
ctx.strokeRect(rect[0], rect[1], rect[2], rect[3])
ctx.fillStyle = 'rgba(255,0,0,0.2)'
ctx.fillRect(rect[0], rect[1], rect[2], rect[3])
})
},
draw: function() {
}
})