PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/project/network/jWebSocketClient-1.0/demos/canvas/iframe.htm

https://gitlab.com/BGCX261/zlatnaspirala2-svn-to-git
HTML | 361 lines | 303 code | 38 blank | 20 comment | 0 complexity | bdd8b910c64a60ad7820cc0c1751e8d7 MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD html 4.01//EN" "http://www.w3.org/TR/html4/transitional.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="X-UA-Compatible" content="chrome=1">
  5. <!-- If Google's chrome frame installed, use it! -->
  6. <!-- Comment needs to be BELOW this meta tag! -->
  7. <!--
  8. // ****************************************************************************
  9. // jWebSocket Simple Chat (uses jWebSocket Client and Server)
  10. // Copyright (c) 2010 Alexander Schulze, Innotrade GmbH, Herzogenrath
  11. // ****************************************************************************
  12. // This program is free software; you can redistribute it and/or modify it
  13. // under the terms of the GNU Lesser General Public License as published by the
  14. // Free Software Foundation; either version 3 of the License, or (at your
  15. // option) any later version.
  16. // This program is distributed in the hope that it will be useful, but WITHOUT
  17. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  19. // more details.
  20. // You should have received a copy of the GNU Lesser General Public License along
  21. // with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
  22. // ****************************************************************************
  23. -->
  24. <meta http-equiv="Content-Language" content="en">
  25. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  26. <title>jWebSocket Chat Demo</title>
  27. <link rel="stylesheet" type="text/css" href="../../res/css/jwebsocket.css">
  28. <script src="../../res/js/jWebSocket.js" type="text/javascript"></script>
  29. <script src="../../res/js/jwsCanvasPlugIn.js" type="text/javascript"></script>
  30. <!-- load file system plug-in to be able to send snap shots of the canvas -->
  31. <script src="../../res/js/jwsFilesystemPlugIn.js" type="text/javascript"></script>
  32. <script type="text/javascript" language="JavaScript">
  33. var lJWSID = "jWebSocket Chat",
  34. lWSC = null,
  35. eCanvas = null;
  36. eStatus = null;
  37. lIsConnected = false,
  38. lColor = "#000000",
  39. CANVAS_ID = "c1";
  40. var IN = 0;
  41. var OUT = 1;
  42. var EVT = 2;
  43. var SYS = "SYS";
  44. var USR = null;
  45. function doOpen() {
  46. // adjust this URL to your jWebSocket server
  47. var lURL = jws.getDefaultServerURL()
  48. + ( frameElement.id ? ";unid=" + frameElement.id : "");
  49. // try to establish connection to jWebSocket server
  50. lWSC.logon( lURL, "Guest", "guest", {
  51. // OnOpen callback
  52. OnOpen: function( aEvent ) {
  53. // start keep alive if user selected that option
  54. lWSC.startKeepAlive({ interval: 30000 });
  55. eStatus.src = "../../images/authenticated.png";
  56. lIsConnected = true;
  57. },
  58. // OnMessage callback
  59. OnMessage: function( aEvent, aToken ) {
  60. },
  61. // OnClose callback
  62. OnClose: function( aEvent ) {
  63. eStatus.src = "../../images/disconnected.png";
  64. lIsConnected = false;
  65. lWSC.stopKeepAlive();
  66. }
  67. });
  68. }
  69. function doClose() {
  70. // disconnect automatically logs out!
  71. lWSC.stopKeepAlive();
  72. var lRes = lWSC.close({
  73. // wait a maximum of 3 seconds for server good bye message
  74. timeout: 3000
  75. });
  76. }
  77. var ctx;
  78. var lPainting = false;
  79. var lX1 = -1;
  80. var lY1 = -1;
  81. function mouseDownLsnr( aEvent ) {
  82. // aEvent.preventDefault();
  83. jws.events.preventDefault( aEvent );
  84. if( lIsConnected ) {
  85. lPainting = true;
  86. lX1 = aEvent.clientX - eCanvas.offsetLeft;
  87. lY1 = aEvent.clientY - eCanvas.offsetTop;
  88. }
  89. }
  90. var eAvg = null;
  91. var loops = 0;
  92. var total = 0;
  93. function mouseMoveLsnr( aEvent ) {
  94. // aEvent.preventDefault();
  95. jws.events.preventDefault( aEvent );
  96. if( lIsConnected && lPainting ) {
  97. var lX2 = aEvent.clientX - eCanvas.offsetLeft;
  98. var lY2 = aEvent.clientY - eCanvas.offsetTop;
  99. loops++;
  100. start = new Date().getTime();
  101. lWSC.canvasLine( CANVAS_ID, lX1, lY1, lX2, lY2, {
  102. color: lColor
  103. });
  104. lX1 = lX2;
  105. lY1 = lY2;
  106. total += ( new Date().getTime() - start );
  107. eAvg.innerHTML = ( total / loops + "ms" );
  108. }
  109. }
  110. function mouseUpLsnr( aEvent ) {
  111. // aEvent.preventDefault();
  112. jws.events.preventDefault( aEvent );
  113. if( lIsConnected && lPainting ) {
  114. lX2 = aEvent.clientX - eCanvas.offsetLeft;
  115. lY2 = aEvent.clientY - eCanvas.offsetTop;
  116. lWSC.canvasLine( CANVAS_ID, lX1, lY1, lX2, lY2, {
  117. color: lColor
  118. });
  119. lPainting = false;
  120. }
  121. }
  122. function mouseOutLsnr( aEvent ) {
  123. mouseUpLsnr( aEvent );
  124. }
  125. function selectColor(aColor ) {
  126. lColor = aColor;
  127. jws.$( "spanSettings" ).style.borderColor = lColor;
  128. }
  129. function doClear() {
  130. if( lIsConnected ) {
  131. lWSC.canvasClear( CANVAS_ID );
  132. }
  133. }
  134. var lImgIdx = 0;
  135. var lImages = new Array();
  136. lImages[ 0 ] = new Image();
  137. lImages[ 1 ] = new Image();
  138. lImages[ 2 ] = new Image();
  139. lImages[ 3 ] = new Image();
  140. lImages[ 4 ] = new Image();
  141. lImages[ 5 ] = new Image();
  142. lImages[ 6 ] = new Image();
  143. lImages[ 7 ] = new Image();
  144. lImages[ 8 ] = new Image();
  145. lImages[ 0 ].src = "../../res/img/image1.jpg";
  146. lImages[ 1 ].src = "../../res/img/image2.jpg";
  147. lImages[ 2 ].src = "../../res/img/image3.jpg";
  148. lImages[ 3 ].src = "../../res/img/image4.jpg";
  149. lImages[ 4 ].src = "../../res/img/image5.jpg";
  150. lImages[ 5 ].src = "../../res/img/image6.jpg";
  151. lImages[ 6 ].src = "../../res/img/image7.jpg";
  152. lImages[ 7 ].src = "../../res/img/image8.jpg";
  153. lImages[ 8 ].src = "../../res/img/image9.jpg";
  154. function paint() {
  155. var lCanvas = document.getElementById( "cnvDemo" );
  156. lCanvas.clear = true;
  157. var lContext = lCanvas.getContext( "2d" );
  158. /*
  159. for( var lIdx = 0; lIdx < lImages.length; lIdx++ ){
  160. lImg.src = lImages[ lIdx ];
  161. lContext.drawImage( lImg, 0, 0 );
  162. }
  163. */
  164. lContext.drawImage( lImages[ lImgIdx ], 0, 0 );
  165. if ( lImgIdx >= 8 ) {
  166. lImgIdx = 0;
  167. } else {
  168. lImgIdx++;
  169. }
  170. /*
  171. lRes = lWSC.fileSend(
  172. // target was passed as optional argument
  173. // and thus can be used here
  174. "target2", // Token.args.target,
  175. "painting", // Token.fileName,
  176. // lCanvas.toDataURL( "image/jpeg" ),
  177. lCanvas.toDataURL( "image/png" ),
  178. { encoding: "base64",
  179. isNode: true
  180. }
  181. );
  182. */
  183. }
  184. function onFileSentObs( aToken ) {
  185. // console.log( new Date() + ": " + aToken.data.length + " " + aToken.data.substr(0, 40));
  186. var lImg = new Image();
  187. // document.body.appendChild(lImg);
  188. lImg.src = aToken.data;
  189. lImg.onload = function() {
  190. var lCanvas = document.getElementById( "cnvDemo" );
  191. var lContext = lCanvas.getContext( "2d" );
  192. lContext.drawImage( lImg, 0, 0 );
  193. }
  194. }
  195. function onFileSavedObs( aToken ) {
  196. var lImg = new Image();
  197. lImg.src = aToken.url;
  198. lImg.onload = function() {
  199. var lCanvas = document.getElementById( "cnvDemo" );
  200. var lContext = lCanvas.getContext( "2d" );
  201. lContext.drawImage( lImg, 0, 0 );
  202. }
  203. }
  204. var lRollingId = 1, lMaxRollingIDs = 9;
  205. function snapshot() {
  206. if( lIsConnected ) {
  207. // png should be supported by all HTML5 compliant browser
  208. // jpeg may not be supported yet (as of 2011-03-01)
  209. // by Safari and Opera. Thus, take png as default for now.
  210. var lRes = lWSC.canvasGetBase64( CANVAS_ID, "image/png" );
  211. if( lRes.code == 0 ) {
  212. // the image could be loaded successfully
  213. // from the canvase element
  214. var lRes = lWSC.fileSave(
  215. // use hardcoded file name for now in this
  216. // demo to keep it as simple as possible
  217. "canvas_demo_" + lRollingId + ".png",
  218. // the data is already base64 encoded!
  219. lRes.data,
  220. { scope: jws.SCOPE_PUBLIC,
  221. encoding: "base64",
  222. suppressEncoder: true // data already base64 encoded!
  223. }
  224. );
  225. lRollingId++;
  226. if( lRollingId > lMaxRollingIDs ) {
  227. lRollingId = 1;
  228. }
  229. } else {
  230. // an error occured
  231. alert( lRes.msg );
  232. }
  233. }
  234. }
  235. function initPage() {
  236. // get some required HTML elements
  237. eAvg = jws.$("spnAvg");
  238. eCanvas = jws.$( "cnvDemo" );
  239. eStatus = jws.$( "simgStatus" );
  240. ctx = eCanvas.getContext( "2d" );
  241. jws.events.addEventListener( eCanvas, "mousedown", mouseDownLsnr );
  242. jws.events.addEventListener( eCanvas, "mousemove", mouseMoveLsnr );
  243. jws.events.addEventListener( eCanvas, "mouseup", mouseUpLsnr );
  244. jws.events.addEventListener( eCanvas, "mouseout", mouseOutLsnr );
  245. /*
  246. eCanvas.addEventListener( "mousedown", mouseDownLsnr, false );
  247. eCanvas.addEventListener( "mousemove", mouseMoveLsnr, false );
  248. eCanvas.addEventListener( "mouseup", mouseUpLsnr, false );
  249. eCanvas.addEventListener( "mouseout", mouseOutLsnr, false );
  250. */
  251. // check if WebSockets are supported by the browser
  252. if( jws.browserSupportsWebSockets() ) {
  253. // instaniate new TokenClient, either JSON, CSV or XML
  254. lWSC = new jws.jWebSocketJSONClient({
  255. });
  256. lWSC.setFileSystemCallbacks({
  257. OnFileSaved: onFileSavedObs,
  258. OnFileSent: onFileSentObs
  259. // OnLocalFileRead: onLocalFileLoadedObs,
  260. // OnLocalFileError: onLocalFileErrorObs
  261. });
  262. lWSC.canvasOpen( CANVAS_ID, "cnvDemo" );
  263. doOpen();
  264. } else {
  265. // jws.$( "sbtnClearLog" ).setAttribute( "disabled", "disabled" );
  266. var lMsg = jws.MSG_WS_NOT_SUPPORTED;
  267. alert( lMsg );
  268. }
  269. }
  270. function exitPage() {
  271. // this allows the server to release the current session
  272. // immediately w/o waiting on the timeout.
  273. if( lWSC ) {
  274. lWSC.close({
  275. // force immediate client side disconnect
  276. timeout: 0
  277. });
  278. }
  279. lWSC.canvasClose( CANVAS_ID );
  280. }
  281. </script>
  282. </head>
  283. <body
  284. onload="initPage();"
  285. onunload="exitPage();"
  286. >
  287. <div> <!-- style="border:1px solid red" -->
  288. <p>
  289. <input style="width:80px" class="sbtnDlg" id="sbtnClear" type="button" value="Clear" onclick="doClear();"
  290. title="Clears the current canvas locally and on all connected clients.">&nbsp;
  291. <input style="width:80px" class="sbtnDlg" id="sbtnPaint" type="button" value="Paint" onclick="paint();"
  292. title="Paints some images on the canvas.">&nbsp;
  293. <input style="width:80px" class="sbtnDlg" id="sbtnSend" type="button" value="Snapshot" onclick="snapshot();"
  294. title="Creates a snapshot, save it on the server and broadcasts a 'filesave' event.">&nbsp;
  295. <img id="simgStatus" alt="status" src="../../images/disconnected.png"/>
  296. </p>
  297. <p id="spnAvg" style="display:none">0ms</p>
  298. <canvas id="cnvDemo" width="265" height="230"
  299. style="width:265px; height:230px; overflow:hidden; border:1px solid gray; cursor:crosshair; float:left" >
  300. </canvas>
  301. <div id="spanSettings" style="border:1px solid gray; position:relative;
  302. top: 0px; left: 5px; width:20px; height:142px; margin-bottom:80px; overflow:hidden">
  303. <table border="0" cellspacing="2" cellpadding="1">
  304. <colgroup><col width="20px"></colgroup>
  305. <tr><td style="cursor: pointer; background-color: #000000" onclick="selectColor('#000000');"
  306. title="black">&nbsp;</td></tr>
  307. <tr><td style="cursor: pointer; background-color: #c0c0c0" onclick="selectColor('#c0c0c0');"
  308. title="gray">&nbsp;</td></tr>
  309. <tr><td style="cursor: pointer; background-color: #ff0000" onclick="selectColor('#ff0000');"
  310. title="red">&nbsp;</td></tr>
  311. <tr><td style="cursor: pointer; background-color: #ff00ff" onclick="selectColor('#ff00ff');"
  312. title="magenta">&nbsp;</td></tr>
  313. <tr><td style="cursor: pointer; background-color: #008000" onclick="selectColor('#008000');"
  314. title="green">&nbsp;</td></tr>
  315. <tr><td style="cursor: pointer; background-color: #0000ff" onclick="selectColor('#0000ff');"
  316. title="blue">&nbsp;</td></tr>
  317. <tr><td style="cursor: pointer; background-color: #ff9900" onclick="selectColor('#ff9900');"
  318. title="cyan">&nbsp;</td></tr>
  319. </table>
  320. </div>
  321. </div>
  322. </body>
  323. </html>