diff options
Diffstat (limited to 'rectangles-drag.html')
| -rw-r--r-- | rectangles-drag.html | 246 |
1 files changed, 0 insertions, 246 deletions
diff --git a/rectangles-drag.html b/rectangles-drag.html deleted file mode 100644 index 086ce27..0000000 --- a/rectangles-drag.html +++ /dev/null @@ -1,246 +0,0 @@ -<!doctype html> -<html> -<head> - <link rel="stylesheet" type="text/css" href="assets/stylesheets/css.css"> -<style type="text/css"> -body > div { - float: left; -} -</style> -</head> -<body> - -<div id="map"></div> -<div id="info"> - <span id="intersects"></span> -</div> - -</body> -<script type="text/javascript" src="assets/javascripts/util.js"></script> -<script type="text/javascript"> - -function vec2(a,b){ - this.a = a - this.b = b -} -vec2.prototype.magnitude = function(){ - return this.b-this.a -} -vec2.prototype.length = function(){ - return abs(this.b-this.a) -} -vec2.prototype.clone = function(){ - return new vec2(this.a, this.b) -} -vec2.prototype.normalize = function(){ - if (this.b < this.a) { - this.a = this.a ^ this.b - this.b = this.a ^ this.b - this.a = this.a ^ this.b - } - return this -} -vec2.prototype.add = function(n){ - this.a += n - this.b += n -} -vec2.prototype.sub = function(n){ - this.a -= n - this.b -= n -} -vec2.prototype.mul = function(n){ - this.a *= n - this.b *= n -} -vec2.prototype.div = function(n){ - this.a /= n - this.b /= n -} -vec2.prototype.contains = function(n){ - return this.a <= n && n <= this.b -} -vec2.prototype.intersects = function(v){ - return (this.a <= v.a && this.b > v.a) || - (v.a <= this.a && v.b > this.a) -} -vec2.prototype.union = function(v){ - if (this.intersects(v)) { - return new vec2( min(this.a,v.a), max(this.b, v.b) ) - } -} -vec2.prototype.intersection = function(v){ - if (this.intersects(v)) { - return new vec2( max(this.a,v.a), min(this.b, v.b) ) - } -} - -function rect (x0,y0,x1,y1){ - if (x0 instanceof vec2) { - this.x = x0 - this.y = y0 - } - else if (x1 === undefined) { - this.x = new vec2(x0,x0) - this.y = new vec2(y0,y0) - } - else { - this.x = new vec2(x0,x1) - this.y = new vec2(y0,y1) - } - this.translation = new vec2(0,0) -} -rect.prototype.clone = function(){ - return new rect( this.x.clone(), this.y.clone() ) -} -rect.prototype.normalize = function(){ - this.x.normalize().add(this.translation.a) - this.y.normalize().add(this.translation.b) - this.translation.a = this.translation.b = 0 - return this -} -rect.prototype.contains = function(x,y){ - return this.x.contains(x) && this.y.contains(y) -} -rect.prototype.intersects = function(r){ - return this.x.intersects(r.x) && this.y.intersects(r.y) -} -rect.prototype.width = function(){ return this.x.length() } -rect.prototype.height = function(){ return this.y.length() } -rect.prototype.fill = function(){ - ctx.fillRect(this.x.a + this.translation.a, this.y.a + this.translation.b, this.x.length(), this.y.length()) -} -rect.prototype.stroke = function(){ - ctx.beginPath() - ctx.moveTo(this.x.a, this.y.a) - ctx.lineTo(this.x.b, this.y.b) - ctx.stroke() -} - - - -var canvas = document.createElement("canvas") -var ctx = canvas.getContext("2d") -var w = canvas.width = 500 -var h = canvas.height = 500 -document.body.appendChild(canvas) - -var rects = [ - new rect(100,100, 300,300), - new rect(200,200, 400,400), -] - -var creating = false, dragging = false -var mouse = new rect(0,0,0,0) -canvas.addEventListener("mousedown", function(e){ - var x = e.pageX, y = e.pageY - mouse = new rect (x,y) - - var intersects = rects.filter(function(r){ return r.contains(x,y) }) - console.log(intersects) - if (intersects.length){ - dragging = intersects[0] - } - else { - creating = true - } - -}) -canvas.addEventListener("mousemove", function(e){ - mouse.x.b = e.pageX - mouse.y.b = e.pageY - if (dragging) { - dragging.translation.a = mouse.x.magnitude() - dragging.translation.b = mouse.y.magnitude() - } - else if (creating) { - mouse.x.b = e.pageX - mouse.y.b = e.pageY - } - else { - mouse.x.a = mouse.x.b - mouse.y.a = mouse.y.b - } -}) -document.addEventListener("mouseup", function(e){ - if (creating) { - if (mouse.height() != 0 && mouse.width() != 0) { - rects.push(mouse.normalize()) - } - } - if (dragging) { - dragging.normalize() - } - mouse = new rect(e.pageX, e.pageY) - creating = dragging = false -}) - -function animate(){ - requestAnimationFrame(animate) - ctx.fillStyle = "#fff" - ctx.fillRect(0,0,w,h) - - solve_rects() - draw_ruler() - draw_rects() - draw_mouse() -} -animate() - -function draw_ruler(){ - ctx.strokeStyle = "rgba(80,80,80,0.5)" - ctx.lineWidth = 1 - var len = 5 - for (var i = 0.5; i < w; i += 10) { - line(i, 0, i, len) - line(0, i, len, i) - } -} - -function line (x,y,a,b){ - ctx.beginPath() - ctx.moveTo(x,y) - ctx.lineTo(a,b) - ctx.stroke() -} - -function solve_rects(){ - var x_intervals = [] - var y_intervals = [] - for (var i = 0; i < rects.length; i++) { - - } -} -function draw_regions(){ -} -function draw_rects(){ - ctx.strokeStyle = "rgba(80,80,80,0.5)" - ctx.lineWidth = 1 - // ctx.setLineDash([randint(5)+2,randint(2)+2]); - ctx.setLineDash([1,1]); - - for (var i = 0; i < rects.length; i++) { - ctx.fillStyle = "rgba(0,200,220,0.5)" - rects[i].fill() -// line(rect.x, 0, rect.x, rect.y) -// line(0, rect.y, rect.x, rect.y) - } -} -function draw_mouse(){ - ctx.fillStyle = "rgba(255,0,0,0.4)"; - ctx.beginPath(); - ctx.arc(mouse.x.b, mouse.y.b, 5, 0, 2*Math.PI, false); - ctx.fill(); - - if (mouse.width() != 0 && mouse.height() != 0) { - if (dragging) { - mouse.stroke() - } - else { - ctx.fillStyle = "rgba(255,255,0,0.5)" - mouse.clone().normalize().fill() - } - } -} - -</script> -</html> |
