summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/rectangles')
-rw-r--r--public/assets/javascripts/rectangles/engine/map/draw.js76
-rw-r--r--public/assets/javascripts/rectangles/engine/rooms/_rooms.js10
-rw-r--r--public/assets/javascripts/rectangles/engine/rooms/_walls.js4
-rw-r--r--public/assets/javascripts/rectangles/models/rect.js23
-rw-r--r--public/assets/javascripts/rectangles/models/wall.js2
5 files changed, 89 insertions, 26 deletions
diff --git a/public/assets/javascripts/rectangles/engine/map/draw.js b/public/assets/javascripts/rectangles/engine/map/draw.js
index 3e185d2..6c27ff1 100644
--- a/public/assets/javascripts/rectangles/engine/map/draw.js
+++ b/public/assets/javascripts/rectangles/engine/map/draw.js
@@ -10,7 +10,7 @@ Map.Draw = function(map, opt){
draw.animate = function(){
ctx.save()
draw.clear()
- // draw.ruler()
+ draw.fill("rgba(255,255,255,0.98)")
if (opt.minimap) {
ctx.translate( map.dimensions.a * 1/2, map.dimensions.b * 1/2)
@@ -19,7 +19,7 @@ Map.Draw = function(map, opt){
ctx.scale( -1, 1 )
draw.coords()
- draw.regions(Rooms.regions, [ "#fff" ])
+ draw.regions(Rooms.regions, [ "#fff" ], "#000")
draw.camera(scene.camera)
}
@@ -29,7 +29,7 @@ Map.Draw = function(map, opt){
ctx.translate( map.center.a, map.center.b )
ctx.scale( -1, 1 )
- draw.regions(Rooms.regions, [ "#f8f8f8" ])
+ draw.regions(Rooms.regions, [ "#f8f8f8" ], "#000")
draw.mouse(map.ui.mouse.cursor)
draw.coords()
scene && draw.camera(scene.camera)
@@ -38,46 +38,72 @@ Map.Draw = function(map, opt){
ctx.restore()
}
+ // changes the ctx temporarily
draw.render = function(){
- ctx.save()
+ var thumbnail_side = 1000
+
+ var extent = Rooms.extent()
+ var center = extent.center()
+ var zoom = thumbnail_side / Math.max( extent.width(), extent.height() ) * 0.99
+
+ var canvas = document.createElement("canvas")
+ ctx = canvas.getContext('2d')
+ canvas.width = thumbnail_side
+ canvas.height = thumbnail_side
+
draw.clear()
- ctx.translate( map.dimensions.a * 1/2, map.dimensions.b * 1/2)
- ctx.scale( map.zoom, map.zoom )
- ctx.translate( map.center.a, map.center.b )
+ ctx.translate( thumbnail_side * 1/2, thumbnail_side * 1/2)
+ ctx.scale( zoom, zoom )
+ ctx.translate( center.a, -center.b )
ctx.scale( -1, 1 )
- draw.regions(Rooms.regions, ["#fff"])
- draw.mouse(map.ui.mouse.cursor)
- scene && draw.camera(scene.camera)
-
+ draw.regions(Rooms.regions, ["#fff"], null)
+
ctx.restore()
+
+ // invert opacity
+ var pixelData = ctx.getImageData(0, 0, canvas.width, canvas.height)
+ var pixels = pixelData.data
+ for (var i = 0, k, _len = pixels.length; i < _len; i++) {
+ k = i*4
+ if (pixels[k+3] == 0) {
+ pixels[k] = pixels[k+1] = pixels[k+2] = pixels[k+3] = 255
+ }
+ else {
+ pixels[k] = pixels[k+1] = pixels[k+2] = 255
+ pixels[k+3] = 0
+ }
+ }
+ ctx.putImageData(pixelData, 0, 0)
+
+ // reset the ctx
+ ctx = map.canvas.getContext("2d")
+
+ return canvas
}
draw.clear = function(){
- ctx.fillStyle = "rgba(255,255,255,0.98)"
ctx.clearRect(0, 0, map.dimensions.a, map.dimensions.b)
- ctx.fillRect(0, 0, map.dimensions.a, map.dimensions.b)
}
- draw.ruler = function (){
- ctx.strokeStyle = "rgba(80,80,80,0.5)"
- ctx.lineWidth = 1
- var len = 5
- for (var i = 0.5; i < map.dimensions.a; i += 10) {
- line(i, 0, i, len)
- line(0, i, len, i)
- }
+ draw.fill = function(fillStyle){
+ ctx.fillStyle = fillStyle
+ ctx.fillRect(0, 0, map.dimensions.a, map.dimensions.b)
}
- draw.regions = function(regions, colors){
+ draw.regions = function(regions, colors, stroke){
+ if (stroke) {
+ ctx.strokeStyle = "#000"
+ ctx.lineWidth = (1 / map.zoom)
+ }
for (var i = 0; i < regions.length; i++) {
if (regions[i].dupe) continue
ctx.fillStyle = colors[i % colors.length]
- ctx.strokeStyle = "#000"
- ctx.lineWidth = (1 / map.zoom)
fill_region(regions[i])
- stroke_sides(regions[i])
+ if (stroke) {
+ stroke_sides(regions[i])
+ }
}
}
diff --git a/public/assets/javascripts/rectangles/engine/rooms/_rooms.js b/public/assets/javascripts/rectangles/engine/rooms/_rooms.js
index 6f96275..0c3cfd4 100644
--- a/public/assets/javascripts/rectangles/engine/rooms/_rooms.js
+++ b/public/assets/javascripts/rectangles/engine/rooms/_rooms.js
@@ -118,6 +118,16 @@
return data
}
+ base.extent = function(){
+ var extent = new Rect ( new vec2(Infinity, -Infinity), new vec2(Infinity, -Infinity) )
+
+ base.forEach(function(room){
+ extent.expand(room.rect)
+ })
+
+ return extent
+ }
+
base.sorted_by_position = function(){
return sort.rooms_by_position( base.values() )
}
diff --git a/public/assets/javascripts/rectangles/engine/rooms/_walls.js b/public/assets/javascripts/rectangles/engine/rooms/_walls.js
index 5ff53fe..71ddde9 100644
--- a/public/assets/javascripts/rectangles/engine/rooms/_walls.js
+++ b/public/assets/javascripts/rectangles/engine/rooms/_walls.js
@@ -103,6 +103,10 @@
walls_data.forEach(function(wall_data){
if (! wall_data) { return }
var wall = base.lookup[ wall_data.id ]
+ if (! wall) {
+ console.log(wall_data);
+ return
+ }
wall.deserialize( wall_data )
})
},
diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js
index a08176a..00f2c55 100644
--- a/public/assets/javascripts/rectangles/models/rect.js
+++ b/public/assets/javascripts/rectangles/models/rect.js
@@ -141,6 +141,29 @@
Rect.prototype.width = function(){ return this.x.length() }
Rect.prototype.height = function(){ return this.y.length() }
Rect.prototype.delta = function(){ return new vec2( this.x.magnitude(), this.y.magnitude() ) }
+ Rect.prototype.expand = function(rect){
+ this.x.a = Math.min( this.x.a, rect.x.a )
+ this.x.b = Math.max( this.x.b, rect.x.b )
+ this.y.a = Math.min( this.y.a, rect.y.a )
+ this.y.b = Math.max( this.y.b, rect.y.b )
+ return this
+ }
+ Rect.prototype.square = function(){
+ var width = this.x.length()
+ var height = this.y.length()
+ var diff
+ if (width < height) {
+ diff = (height - width) / 2
+ this.x.a -= diff
+ this.x.b += diff
+ }
+ else {
+ diff = (width - height) / 2
+ this.y.a -= diff
+ this.y.b += diff
+ }
+ return this
+ }
Rect.prototype.toString = function(){
var sides = sidesToString(this.sides)
var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides
diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js
index 8174de7..fcb2f5e 100644
--- a/public/assets/javascripts/rectangles/models/wall.js
+++ b/public/assets/javascripts/rectangles/models/wall.js
@@ -13,7 +13,7 @@
}
var Wall = function(opt){
- this.id = [ opt.side, opt.edge, opt.vec.a ].join("_")
+ this.id = [ opt.side|0, opt.edge|0, opt.vec.a|0 ].join("_")
this.vec = opt.vec
this.edge = opt.edge
this.side = opt.side