summaryrefslogtreecommitdiff
path: root/assets/test/lasso/index.html
blob: 16cb27fdb2c04439965477aaafc17636be24ba82 (plain)
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
86
87
88
<style>
body,html{margin:0;padding:0;}
#hud { position: absolute; top: 0; left: 0; pointer-events: none; }
</style>
<canvas id="canvas"></canvas>
<div id="hud"></div>

<script src="/assets/javascripts/vendor/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/assets/javascripts/vendor/bower_components/lodash/dist/lodash.min.js"></script>
<script src="/assets/javascripts/vendor/canvasutilities.js"></script>
<script src="/assets/javascripts/vendor/tube.js"></script>
<script src="/assets/javascripts/math/util.js"></script>
<script src="/assets/javascripts/math/point.js"></script>
<script src="/assets/javascripts/math/vec2.js"></script>
<script src="/assets/javascripts/util/mouse.js"></script>
<script src="/assets/javascripts/util/uid.js"></script>
<script>

var ctx = canvas.getContext('2d')
var w = canvas.width = window.innerWidth
var h = canvas.height = window.innerHeight

var placing = false
var points = []

var mymouse = new mouse({
  el: canvas,
  down: function(e, cursor){
    // compare to initial point
    if (placing) {
      if (points.length > 3 && points[0].distanceTo({ a: cursor.x.a, b: cursor.y.a }) < 3) {
        points.push(points[0].clone())
        placing = false
      }
      else {
        points.push( new point( cursor.x.a, cursor.y.a ) )
      }
    }
    else {
      placing = true
      points.length = 0
      points.push( new point( cursor.x.a, cursor.y.a ) )
    }
  },
  move: function(e, cursor){
    if (placing && points.length > 2 && points[0].distanceTo({ a: cursor.x.a, b: cursor.y.a }) < 3 ) {
      document.body.style.cursor = "pointer"
    }
    else {
      document.body.style.cursor = "crosshair"
    }
  },
  drag: function(e, cursor){
  },
  up: function(e, cursor, new_cursor){
  },
})

function draw (time) {
  ctx.fillStyle = "#fff"
  ctx.fillRect(0,0,w,h)

  if (points.length == 1) {
    ctx.fillStyle = "#000"
    ctx.fillRect(points[0].a, points[0].b, 1, 1)
  }
  if (points.length > 1) {
    ctx.fillStyle = "#000"
    ctx.strokeStyle = "#000"
    ctx.beginPath()
    ctx.moveTo(points[0].a, points[0].b)
    points.forEach(function(point, i){
      i && ctx.lineTo(point.a, point.b)
    })
    if (placing) {
      ctx.stroke()
    }
    else {
      ctx.fill()
    }
  }
}
function animate(time){
  requestAnimationFrame(animate)
  draw(time)
}
animate()
</script>