File: bunny.html

package info (click to toggle)
rabbitmq-server 4.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 37,972 kB
  • sloc: erlang: 257,835; javascript: 22,466; sh: 3,037; makefile: 2,517; python: 1,966; xml: 646; cs: 335; java: 244; ruby: 212; php: 100; perl: 63; awk: 13
file content (136 lines) | stat: -rw-r--r-- 3,353 bytes parent folder | download | duplicates (5)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!doctype html>
<html><head>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script src="stomp.js"></script>

    <style>
      #cnvs {
          border: none;
          -moz-border-radius: 4px;
          cursor: url(pencil.cur),crosshair;
          position: absolute;
          overflow: hidden;
          width: 100%;
          height: 100%;
      }
      #cnvs:active {
          cursor: url(pencil.cur),crosshair;
      }
      body {
          overflow: hidden;
      }
    </style>
    <title>RabbitMQ Web STOMP Examples: Bunny Drawing</title>
    <link href="main.css" rel="stylesheet" type="text/css"/>
</head><body lang="en">
    <h1><a href="index.html">RabbitMQ Web STOMP Examples</a> > Bunny Drawing</h1>
    <canvas id="cnvs"></canvas>
<script>
var send; var draw;
send = draw = function(){};

var lines = [];

var canvas = document.getElementById('cnvs');

if (canvas.getContext) {
  var ctx = canvas.getContext('2d');

  var img = new Image();
  img.onload = function() {
    ctx.drawImage(img, 230, 160);
  };
  img.src = 'bunny.png';

  draw = function(p) {
    ctx.beginPath();
    ctx.moveTo(p.x1, p.y1);
    ctx.lineTo(p.x2, p.y2);
    ctx.stroke();
    ctx.drawImage(img, 230, 160);
  };

  var do_resize = function() {
    canvas.width  = window.innerWidth;
    canvas.height = window.innerHeight;

    ctx.font = "bold 20px sans-serif";
    ctx.fillStyle = "#444";
    ctx.fillText("Draw wings on the bunny!", 260, 100);
    ctx.font = "normal 16px sans-serif";
    ctx.fillStyle = "#888";
    ctx.fillText("(For more fun open a second browser)", 255, 130);

    ctx.drawImage(img, 230, 160);

    ctx.strokeStyle = "#fa0";
    ctx.lineWidth = "10";
    ctx.lineCap = "round";

    $.map(lines, function (p) {
      draw(p);
    });
  };

  $(window).resize(do_resize);
  $(do_resize);


  var pos = $('#cnvs').position();
  var prev = null;
  $('#cnvs').mousedown(function(evt) {
      evt.preventDefault();
      evt.stopPropagation();
      $('#cnvs').bind('mousemove', function(e) {
          var curr = {x:e.pageX-pos.left, y:e.pageY-pos.top};
          if (!prev) {
              prev = curr;
              return;
          }
          if (Math.sqrt(Math.pow(prev.x - curr.x, 2) +
                        Math.pow(prev.y - curr.y, 2)) > 8) {
               var p = {x1:prev.x, y1:prev.y, x2:curr.x, y2:curr.y}
               lines.push(p);
               draw(p);
               send(JSON.stringify(p));
               prev = curr;
          }
      });
  });
  $('html').mouseup(function() {
      prev = null;
      $('#cnvs').unbind('mousemove');
  });
}
else {
  document.write("Sorry - this demo requires a browser with canvas tag support.");
}

// Stomp.js boilerplate
var ws = new WebSocket('ws://' + window.location.hostname + ':15674/ws');
var client = Stomp.over(ws);

client.debug = function() {
  if (window.console && console.log && console.log.apply) {
    console.log.apply(console, arguments);
  }
};

send = function(data) {
  client.send('/topic/bunny', {}, data);
};

var on_connect = function(x) {
  id = client.subscribe('/topic/bunny', function(d) {
    var p = JSON.parse(d.body);
    lines.push(p);
    draw(p, true);
  });
};
var on_error = function() {
  console.log('error');
};
client.connect('guest', 'guest', on_connect, on_error, '/');

</script>
</body></html>