PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/canvasport.js

https://github.com/esimionato/google-plus-me
JavaScript | 184 lines | 159 code | 16 blank | 9 comment | 23 complexity | 8beb599541c228a46d50ba1cffaad5ca MD5 | raw file
  1. /* v0.1 from http://nebulos.googlecode.com/svn/trunk/%20nebulos%20--username%20leforthomas/nebulos/src/canvasport-v0.1.js */
  2. var canvasport = {
  3. createCanvas: function(container, width, height, canvasstyle)
  4. {
  5. var canvas = document.createElement("canvas");
  6. container.appendChild(canvas);
  7. canvas.style.width = width;
  8. canvas.style.height = height;
  9. // huyz 2011-09-18
  10. canvas.setAttribute('width', parseInt(width, 10));
  11. canvas.setAttribute('height', parseInt(height, 10));
  12. canvas.style.margin = "0px";
  13. canvas.style.padding = "0px";
  14. // add canvas for image
  15. if( window.G_vmlCanvasManager) {
  16. // excanvas hack
  17. window.G_vmlCanvasManager.initElement(canvas);
  18. }
  19. return canvas;
  20. },
  21. getDrawing: function(canvas)
  22. {
  23. return canvas.getContext('2d');
  24. },
  25. _fillText: function(ctx, text, posx, posy, font)
  26. {
  27. if(ctx.fillText)
  28. {
  29. ctx.font = font;
  30. ctx.fillText(text, posx, posy);
  31. } else {
  32. if(ctx.mozDrawText)
  33. {
  34. ctx.save();
  35. ctx.mozTextStyle = font;
  36. ctx.translate(posx, posy);
  37. ctx.mozDrawText(text);
  38. ctx.restore();
  39. }
  40. }
  41. },
  42. _strokeText: function(ctx, text, posx, posy, font)
  43. {
  44. if(ctx.strokeText)
  45. {
  46. ctx.font = font;
  47. ctx.strokeText(text, posx, posy);
  48. } else {
  49. if(ctx.mozDrawText)
  50. {
  51. ctx.save();
  52. ctx.mozTextStyle = font;
  53. ctx.translate(posx, posy);
  54. ctx.mozDrawText(text);
  55. ctx.restore();
  56. }
  57. }
  58. },
  59. drawText: function(ctx, text, posx, posy, vertical, font, stroke)
  60. {
  61. if(vertical)
  62. {
  63. ctx.save();
  64. ctx.translate(posx, posy);
  65. ctx.rotate(270 * Math.PI / 180);
  66. this._fillText(ctx, text, 0, 0, font);
  67. if(stroke)
  68. {
  69. this._strokeText(ctx, text, 0, 0, font);
  70. }
  71. ctx.restore();
  72. } else {
  73. this._fillText(ctx, text, posx, posy, font);
  74. if(stroke)
  75. {
  76. this._strokeText(ctx, text, posx, posy, font);
  77. }
  78. }
  79. },
  80. measureText: function(ctx, text)
  81. {
  82. return (ctx.measureText ? ctx.measureText(text).width : (ctx.mozMeasureText ? ctx.mozMeasureText(text) : 100));
  83. },
  84. drawImage: function(ctx, image, startx, starty, width, height, destx, desty, destwidth, destheight)
  85. {
  86. try {
  87. var img = image[0];
  88. ctx.drawImage(img, startx, starty, width, height, destx, desty, destwidth, destheight);
  89. } catch(e)
  90. {
  91. alert('Problem with canvas ' + e.message);
  92. }
  93. },
  94. drawImageslice: function(ctx, spritepos, pos)
  95. {
  96. try {
  97. var spriteposition = spritepos;
  98. var imagepos = pos;
  99. if(this.spriteimage != null)
  100. {
  101. ctx.drawImage(this.spriteimage, spriteposition.x, 0, spriteposition.w, spriteposition.h, imagepos.x - Math.floor(spriteposition.w / 2), imagepos.y - Math.floor(spriteposition.h / 2), spriteposition.w, spriteposition.h);
  102. }
  103. } catch(e)
  104. {
  105. llh.error('Problem with canvas ' + e.message);
  106. }
  107. },
  108. roundedRect: function (ctx, x, y, width, height, startpos, toporientation, leftorientation, radius, options)
  109. {
  110. var opening = 7;
  111. var openingheight = (height - opening) / 2;
  112. function v(value) {return (leftorientation ? value + x : width + x - value);}
  113. ctx.beginPath();
  114. ctx.moveTo(v(0),y+radius);
  115. // draw to opening
  116. ctx.lineTo(v(0), y + openingheight - opening / 2);
  117. // draw to origin
  118. ctx.quadraticCurveTo(v(-x), y + openingheight - opening / 2, startpos.x, startpos.y);
  119. // back to bubble
  120. ctx.quadraticCurveTo(v(-x), y + openingheight + opening / 2, v(0), y + openingheight + opening / 2);
  121. ctx.lineTo(v(0),y+height-radius);
  122. ctx.quadraticCurveTo(v(0),y+height,v(radius),y+height);
  123. ctx.lineTo(v(width-radius),y+height);
  124. ctx.quadraticCurveTo(v(width),y+height,v(width),y+height-radius);
  125. ctx.lineTo(v(width),y+radius);
  126. ctx.quadraticCurveTo(v(width),y,v(width-radius),y);
  127. ctx.lineTo(v(radius),y);
  128. ctx.quadraticCurveTo(v(0),y,v(0),y+radius);
  129. ctx.closePath();
  130. ctx.strokeStyle = (options && options.color ? options.color : "#723F3F");
  131. ctx.fillStyle = (options && options.bkcolor ? options.bkcolor : "rgba(255, 255, 255, 1.0)");
  132. ctx.fill();
  133. ctx.stroke();
  134. },
  135. customiseBubble: function(ctx, divelement, toporientation, leftorientation, options)
  136. {
  137. var position = divelement.position();
  138. position.top += parseInt(divelement.css("padding-top").replace("px", ""));
  139. position.left += parseInt(divelement.css("padding-left").replace("px", ""));
  140. var margin = 5;
  141. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  142. var startpos = {x: (leftorientation ? 0 : ctx.canvas.width), y: (toporientation ? 0 : ctx.canvas.height)};
  143. this.roundedRect(ctx, position.left - margin, position.top - margin, divelement.width() + 2 * margin, divelement.height() + 2 * margin, startpos, toporientation, leftorientation, 15, options);
  144. },
  145. hitTest: function(imagesrc, x, y, width, height, incanvas)
  146. {
  147. var canvaswidth = imagesrc.canvas.width;
  148. var canvasheight = imagesrc.canvas.height;
  149. if(incanvas)
  150. {
  151. if((x < 0) || (y < 0) || (x + width > canvaswidth) || (y + height > canvasheight))
  152. {
  153. return true;
  154. }
  155. }
  156. var imagesrcdata = imagesrc.getImageData(x, y, width, height);
  157. var pixels = imagesrcdata.data;
  158. for (var i = 0, n = pixels.length; i < n; i += 4) {
  159. // uses opacity component to find out if pixel has been set
  160. if(pixels[i] != 255)
  161. {
  162. return true;
  163. }
  164. //message = message + ":" + pixels[i + 3];
  165. }
  166. return false;
  167. }
  168. }