Posted By

weilawei on 05/07/12


Tagged

ajax streaming client canvas svg


Versions (?)

Simple SVG Streaming Client


 / Published in: HTML
 

This is a simple SVG streaming client, which displays the output on an HTML5 canvas element. The server can be (almost) transparently restarted (output freezes). The client allows for adjusting the FPS and changing the input stream.

The server is at http://snipplr.com/view/64802/simple-svg-streaming-server/.

  1. <!doctype html>
  2. <head>
  3. <title>SVG Streaming Client</title>
  4.  
  5. <script src="http://code.jquery.com/jquery-git.js"></script>
  6. function start_stream() {
  7. stream_sink = $("#stream_sink")[0].getContext("2d");
  8. stream_source = new Image();
  9. stream_source.onload = function() {
  10. clear_sink();
  11. stream_sink.drawImage(stream_source, 0, 0, stream_source.width, stream_source.height);
  12. };
  13.  
  14. return setInterval(update_stream, (1000 / $("#fps").val()));
  15. }
  16.  
  17. function clear_sink() {
  18. stream_sink.fillStyle = "#454A41";
  19. stream_sink.fillRect(0, 0, 640, 480);
  20. }
  21.  
  22. function update_sink(data, text_status, jqXHR) {
  23. stream_source.src = data;
  24. }
  25.  
  26. function update_stream() {
  27. $.ajax({
  28. url: $("#stream").val(),
  29. success: update_sink,
  30. dataType: "text"});
  31. }
  32.  
  33. $(function(){
  34. interval_id = start_stream();
  35. $("#fps").change(function() {
  36. clearInterval(interval_id)
  37. interval_id = setInterval(update_stream, (1000 / $("#fps").val()));
  38. });
  39. });
  40. </script>
  41. </head>
  42. <section>
  43. <canvas id="stream_sink" width="640" height="480"></canvas>
  44. <form>
  45. <label for="stream">Stream</label>: <input id="stream" type="text" value="clock.svg">
  46. <label for="stream">FPS</label>: <input id="fps" type="range" min="0" max="1000" step="5" value="1">
  47. </form>
  48. </section>
  49. </body>
  50. </html>

Report this snippet  

You need to login to post a comment.