diff options
Diffstat (limited to 'assets/javascripts/rectangles')
| -rw-r--r-- | assets/javascripts/rectangles/_env.js | 18 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/builder.js | 31 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/clipper.js (renamed from assets/javascripts/rectangles/app.js) | 53 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/draw.js | 2 | ||||
| -rw-r--r-- | assets/javascripts/rectangles/rect.js | 58 |
5 files changed, 134 insertions, 28 deletions
diff --git a/assets/javascripts/rectangles/_env.js b/assets/javascripts/rectangles/_env.js new file mode 100644 index 0000000..aa7af50 --- /dev/null +++ b/assets/javascripts/rectangles/_env.js @@ -0,0 +1,18 @@ + +var environment = new function(){} +environment.init = function(){ + scene.camera.move({ + "x": 0, + "y": 0, + "z": -1000, + "rotationX": 0.085, + "rotationY": 0.025 + }) + map && map.zoom(3.00) && map.recenter() + + clipper.init() + scene.update() +} +environment.update = function(t){ +} + diff --git a/assets/javascripts/rectangles/builder.js b/assets/javascripts/rectangles/builder.js new file mode 100644 index 0000000..1c576a5 --- /dev/null +++ b/assets/javascripts/rectangles/builder.js @@ -0,0 +1,31 @@ +var builder = new function(){ + var base = this + base.tube = new Tube () + + var els = [] + + base.tube.on("clipper:update", rebuild) + + function rebuild(){ + clear() + build() + } + function build (){ + clipper.regions.forEach(function(r){ + var walls = r.walls() + walls.forEach(function(el){ + els.push(el) + scene.add(el) + }) + }) + } + function clear (){ + els.forEach(function(el){ + scene.remove(el) + }) + els = [] + } + +} + + diff --git a/assets/javascripts/rectangles/app.js b/assets/javascripts/rectangles/clipper.js index 071dfe8..8cefca3 100644 --- a/assets/javascripts/rectangles/app.js +++ b/assets/javascripts/rectangles/clipper.js @@ -1,6 +1,7 @@ -window.ctx = window.rects = window.regions = window.w = window.h = null; +window.ctx = window.w = window.h = null; -var app = new function(){ +var clipper = new function(){ + var base = this var canvas = document.createElement("canvas") var ctx = window.ctx = canvas.getContext("2d") var w = window.w = canvas.width = 500 @@ -8,7 +9,7 @@ var app = new function(){ var regions = [] document.querySelector("#map").appendChild(canvas) - function init(){ + base.init = function (){ bind() animate() } @@ -18,14 +19,15 @@ var app = new function(){ if (modified) { solve_rects() + builder.tube("clipper:update") } draw_ruler() - draw_regions(regions) + draw_regions(base.regions) draw_mouse(mouse) z = false } - var rects = window.rects = [ + var rects = base.rects = [ new rect(100,100, 300,300), new rect(200,200, 400,400), ] @@ -48,13 +50,13 @@ var app = new function(){ // console.log(intersects) if (intersects.length){ - app.dragging = intersects[0] + clipper.dragging = intersects[0] } else { - app.creating = true + clipper.creating = true } - if (e.shiftKey && app.dragging) { - app.dragging.quantize(10) + if (e.shiftKey && clipper.dragging) { + clipper.dragging.quantize(10) } }) canvas.addEventListener("mousemove", function(e){ @@ -71,11 +73,11 @@ var app = new function(){ mouse.x.b = x mouse.y.b = y - if (app.dragging) { - app.dragging.translation.a = mouse.x.magnitude() - app.dragging.translation.b = mouse.y.magnitude() + if (clipper.dragging) { + clipper.dragging.translation.a = mouse.x.magnitude() + clipper.dragging.translation.b = mouse.y.magnitude() } - else if (app.creating) { + else if (clipper.creating) { mouse.x.b = x mouse.y.b = y } @@ -85,28 +87,28 @@ var app = new function(){ } }) document.addEventListener("mouseup", function(e){ - if (app.creating) { + if (clipper.creating) { if (mouse.height() != 0 && mouse.width() != 0) { rects.push(mouse.normalize()) } } - if (app.dragging) { - app.dragging.normalize() + if (clipper.dragging) { + clipper.dragging.normalize() } mouse = new rect(e.pageX, e.pageY) - app.creating = app.dragging = false + clipper.creating = clipper.dragging = false modified = true }) } function solve_rects(){ - rects = sort_rects_by_position(rects) + var rects = sort_rects_by_position(base.rects) for (var i = 0; i < rects.length; i++) { rects[i].id = i rects[i].reset() } - regions = [] + var regions = [] var left, right for (var i = 0; i < rects.length; i++) { @@ -159,19 +161,16 @@ var app = new function(){ } } - regions = sort_rects_by_position(regions) + base.regions = sort_rects_by_position(regions) modified = false // document.getElementById("intersects").innerHTML = sort_rects_by_position(regions).join("<br>") } - // generate floor and ceiling for some regions - // generate walls from surviving regions - // generate ceiling-walls where ceiling has discontinuity + // generate floor and ceiling for some regions + // generate walls from surviving regions + // generate ceiling-walls where ceiling has discontinuity - - this.init = init - return this + return base } -app.init() diff --git a/assets/javascripts/rectangles/draw.js b/assets/javascripts/rectangles/draw.js index 2785544..8786b0b 100644 --- a/assets/javascripts/rectangles/draw.js +++ b/assets/javascripts/rectangles/draw.js @@ -37,7 +37,7 @@ function draw_mouse(mouse){ ctx.fill(); if (mouse.width() != 0 && mouse.height() != 0) { - if (app.dragging) { + if (clipper.dragging) { mouse.stroke() } else { diff --git a/assets/javascripts/rectangles/rect.js b/assets/javascripts/rectangles/rect.js index 866727f..67abdee 100644 --- a/assets/javascripts/rectangles/rect.js +++ b/assets/javascripts/rectangles/rect.js @@ -181,6 +181,64 @@ window.rect = (function(){ return splits } + rect.prototype.walls = function(){ + var list = [], el = null + + var width = this.x.length() + var depth = this.y.length() + var height = 800 + + if (this.sides & FRONT) { + el = wall('.face.front') + el.scaleX = width + el.scaleY = height + el.z = this.y.a - depth/2 + el.x = this.x.a + el.y = height/2 + list.push(el) + } + if (this.sides & LEFT) { + el = wall('.face.left') + el.rotationY = -HALF_PI + el.scaleY = height + el.scaleZ = depth + el.x = this.x.a - width/2 + el.z = this.y.a + el.y = height/2 + list.push(el) + } + if (this.sides & RIGHT) { + el = wall('.face.right') + el.rotationY = HALF_PI + el.scaleY = height + el.scaleZ = depth + el.x = this.x.a + width/2 + el.y = height/2 + el.z = this.y.a + list.push(el) + } + if (this.sides & BACK) { + var el = wall('.face.back') + el.scaleX = width + el.scaleY = height + el.rotationY = PI + el.z = this.y.a + depth/2 + el.y = height/2 + el.x = this.x.a + list.push(el) + } + + function wall(klass){ + var el = new MX.Object3D(klass || ".face") + el.width = el.height = el.scaleX = el.scaleY = el.scaleZ = 1 + el.z = el.y = el.x = 0 + el.type = "Face" + return el + } + + return list + } + return rect })() |
