diff options
| author | Julie Lala <jules@okfoc.us> | 2014-08-19 21:08:20 -0400 |
|---|---|---|
| committer | Julie Lala <jules@okfoc.us> | 2014-08-19 21:08:20 -0400 |
| commit | 287d180afe42bfc1a4fe20832cb7714593b50fbb (patch) | |
| tree | 953d82339e105718dd2525854fb5a079f0bd6f17 /public/assets | |
| parent | ca54810c810e8dd08b20dce5e901630d0d36fe25 (diff) | |
| parent | 24870ab6ebd31c18b3533ec96ff04ad16153c844 (diff) | |
Merge branch 'walls' of github.com:okfocus/vvalls into walls
Diffstat (limited to 'public/assets')
| -rw-r--r-- | public/assets/javascripts/rectangles/models/rect.js | 1 | ||||
| -rw-r--r-- | public/assets/javascripts/rectangles/models/surface.js | 4 | ||||
| -rw-r--r-- | public/assets/javascripts/rectangles/util/mouse.js | 2 | ||||
| -rw-r--r-- | public/assets/test/surface.html | 77 |
4 files changed, 82 insertions, 2 deletions
diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index f23ab9e..67852b4 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -139,6 +139,7 @@ } 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.toString = function(){ var sides = sidesToString(this.sides) var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 7001ee3..eebd566 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -113,7 +113,9 @@ return old_bounds } - var center_index = this.index_for_x( position.a + dimension.a/2 + delta.a, 0 ) + var left_index = this.index_for_x( position.a + dimension.a/2 + delta.a, 0 ) + var center_index = this.index_for_x( position.a + dimension.a/2 + delta.a, left_index ) + var right_index = this.index_for_x( position.a + dimension.a/2 + delta.a, center_index ) var new_bounds = this.bounds_at_index_with_dimensions(center_index, dimension) this.clamp_delta(new_bounds, dimension, position, delta) diff --git a/public/assets/javascripts/rectangles/util/mouse.js b/public/assets/javascripts/rectangles/util/mouse.js index 06958af..fea3376 100644 --- a/public/assets/javascripts/rectangles/util/mouse.js +++ b/public/assets/javascripts/rectangles/util/mouse.js @@ -8,7 +8,7 @@ // cursor.x.a // cursor.y.a }, - move: function(e, cursor, delta){ + move: function(e, cursor){ // delta.a (x) // delta.b (y) }, diff --git a/public/assets/test/surface.html b/public/assets/test/surface.html new file mode 100644 index 0000000..85e3a7b --- /dev/null +++ b/public/assets/test/surface.html @@ -0,0 +1,77 @@ +<canvas id="canvas"></canvas> + +<script src="/assets/javascripts/util.js"></script> +<script src="/assets/javascripts/vendor/tube.js"></script> +<script src="/assets/javascripts/rectangles/util/constants.js"></script> +<script src="/assets/javascripts/rectangles/util/mouse.js"></script> +<script src="/assets/javascripts/rectangles/models/vec2.js"></script> +<script src="/assets/javascripts/rectangles/models/rect.js"></script> +<script src="/assets/javascripts/rectangles/models/surface.js"></script> +<script> + +var ctx = canvas.getContext('2d') + +var w = canvas.width = 600 +var h = canvas.height = 400 + +var surface = new Surface () +surface.add( new Rect( new vec2(10, 100), new vec2(0, 400) ) ) +surface.add( new Rect( new vec2(100, 300), new vec2(200, 400) ) ) +surface.add( new Rect( new vec2(300, 400), new vec2(0, 300) ) ) +surface.add( new Rect( new vec2(400, 500), new vec2(0, 400) ) ) + +var position = new vec2( 40, 40 ) +var dimension = new vec2( 50, 80 ) +var bounds = surface.bounds_at_index_with_dimensions(0, dimension) + +var delta = new vec2( 0, 0 ) + +var dragging = false + +var mymouse = new mouse({ + el: canvas, + down: function(e, cursor){ + console.log(cursor.x.a, cursor.y.a) + if (-cursor.x.a > position.a && -cursor.x.a < position.a + dimension.a && + cursor.y.a > position.b && cursor.y.a < position.b + dimension.b) { + dragging = true + } + }, + drag: function(e, cursor){ + if (! dragging) return + delta = cursor.delta() + delta.a = - delta.a + var new_bounds = surface.translate(bounds, dimension, position, delta) + bounds = new_bounds || bounds + }, + up: function(e, cursor, new_cursor){ + position.a += delta.a + position.b += delta.b + delta.zero() + dragging = false + }, +}) + + +var colors = ["#ddd", "#eee"] + +function draw(){ + ctx.clearRect(0,0,w,h) + surface.faces.forEach(function(face, i){ + ctx.fillStyle = colors[i % colors.length] + ctx.fillRect(face.x.a, face.y.a, face.width(), face.height()) + }) + ctx.fillStyle = "rgba(255,0,0,0.1)" + ctx.fillRect(bounds.x.a, bounds.y.a, bounds.width(), bounds.height()) + + ctx.fillStyle = dragging ? "#ff0" : "#00f" + ctx.fillRect(position.a + delta.a, position.b + delta.b, dimension.a, dimension.b) +} + +function animate(){ + requestAnimationFrame(animate) + draw() +} +animate() + +</script> |
