Advertisement
Guest User

Untitled

a guest
Oct 1st, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.44 KB | None | 0 0
  1. var next_image = null;
  2.  
  3. var scale = [0.1, 0.25, 0.5, 1, 1.5, 2, 4];
  4.  
  5. function ImageItem(file, x, y, file_name){
  6. this.loaded = false;
  7. this.uploaded = false;
  8. this.sent = false; // For reconciliation through the websocket
  9.  
  10. this.server_file_name = "";
  11. this.complete_upload = function(name){
  12. this.server_file_name = name;
  13. this.uploaded = true;
  14. if(next_image.loaded){
  15. // If we have finished loading the image and
  16. // uploading then set as next asset
  17. cursor.assets.push(next_image);
  18. next_image = null;
  19. }
  20. }
  21. this.img = new Image();
  22.  
  23. this.size = new Location(0,0);
  24. this.type = "imageitem";
  25. this.moved = false;
  26. this.id = uuidv4().replace(/-/g, ""); // For tracking movements of items - ids will match
  27.  
  28. this.scale_index = 3;
  29.  
  30. this.get_lowest_point = function(){
  31. // Location and size of y
  32. // TODO: What about buttons or extra stuff?
  33. return this.loc.y + this.size.y;
  34. }
  35.  
  36.  
  37. if (FileReader && file && file!=null) {
  38. var fr = new FileReader();
  39. fr.onload = function () {
  40. next_image.img.src = fr.result;
  41. next_image.img.onload = function(){
  42. next_image.loaded = true;
  43. next_image.size.x = next_image.img.naturalWidth;
  44. next_image.size.y = next_image.img.naturalHeight;
  45.  
  46. if(next_image.uploaded){
  47. // If we have finished loading the image and
  48. // uploading then set as next asset
  49. cursor.assets.push(next_image);
  50. next_image = null;
  51. }
  52. }
  53. // TODO: Upload to the server so it can share with the other account
  54.  
  55. // document.getElementById(outImage).src = fr.result;
  56. }
  57. fr.readAsDataURL(file);
  58. } else {
  59. // fallback -- perhaps submit the input to an iframe and temporarily store
  60. // them on the server until the user's session ends.
  61. this.server_file_name = file_name;
  62. this.img.src = file_name;
  63.  
  64. this.img.onload = function(){
  65. // Not sure how to find the item in the list of assets to confirm
  66. }
  67. this.loaded = true;
  68. }
  69.  
  70. this.offset = 0;
  71. this.loc = new Location(x, y);
  72.  
  73. // Make sure we are on the visible canvas
  74. if(this.loc.x < 0){
  75. this.loc.x = 50;
  76. }
  77. if(this.loc.y < 0){
  78. this.loc.y = 50;
  79. }
  80. this.filename = file.name;
  81. // console.log(file);
  82. this.update = ImageItemUpdate;
  83. this.draw = ImageItemDraw;
  84.  
  85. this.hittest = ImageItemHitTest;
  86. this.bodyhittest = ImageItemBodyHitTest;
  87. this.highlight_dragbar = false;
  88. this.scaleImage = ImageItemScale;
  89. this.hittestresize = ImageItemHitTestResize;
  90.  
  91. this.assets_deleted = false;
  92. this.assets = []; // This is so that we can have lines tied to the item
  93. this.assets_dirty = false;
  94. this.add_asset = ImageItemAddAsset;
  95.  
  96. this.resize = ImageItemResize;
  97. this.start_resize = ImageItemStartResize;
  98. }
  99.  
  100. function ImageItemUpdate(){
  101. // Maybe update if dragging?
  102. }
  103.  
  104. function ImageItemDraw(ctx, offset){
  105. var old_style = ctx.strokeStyle;
  106. var old_fill = ctx.fillStyle;
  107.  
  108. if(this.loaded){
  109. var page = {
  110. width:$(window).width(),
  111. height:$(window).height()
  112. };
  113.  
  114. var sc = scale[this.scale_index];
  115. var sc_x = Math.round(sc *this.size.x);
  116. var sc_y = Math.round(sc * this.size.y);
  117.  
  118.  
  119.  
  120.  
  121. // if(this.size.x > page.width*0.5) {
  122. // this.size.x = page.width*0.5 ;
  123. // }
  124. //
  125. // if(this.size.y > page.width*0.5) {
  126. // this.size.y = page.height*0.5;
  127. // }
  128. // if(this.scale_index == 3){
  129. // console.log(sc_x + ", " + sc_y );
  130. // console.log(this.size);
  131. // }
  132.  
  133. if(this.highlight_dragbar === false){
  134. ctx.fillStyle = "#222222";
  135. }else{
  136. ctx.fillStyle = "#444444";
  137. }
  138. // Dragbar Top
  139. ctx.beginPath();
  140. var tb_width = this.size.x;
  141. ctx.fillRect(this.loc.x, this.loc.y - 20 - offset, tb_width , 20);
  142. ctx.stroke();
  143. ctx.closePath();
  144.  
  145. ctx.font = "14px verdana";
  146. ctx.fillStyle = "#eeffee";
  147. ctx.fillText(this.filename, this.loc.x + 5 , this.loc.y -5 - offset, this.size.x);
  148.  
  149. ctx.linewidth = 5;
  150. ctx.strokeStyle = "#222222";
  151.  
  152. // Outline of image
  153. ctx.beginPath();
  154. ctx.moveTo(this.loc.x, this.loc.y - offset);
  155. ctx.lineTo(this.loc.x+sc_x, this.loc.y - offset);
  156. ctx.lineTo(this.loc.x+sc_x, this.loc.y+sc_y - offset);
  157. ctx.lineTo(this.loc.x,this.loc.y+sc_y - offset);
  158. ctx.lineTo(this.loc.x,this.loc.y - offset);
  159. ctx.stroke();
  160. ctx.closePath();
  161.  
  162. // Image
  163. ctx.drawImage(this.img, this.loc.x, this.loc.y - offset, sc_x, sc_y);
  164. console.log('aaaa');
  165. // ctx.drawImage(this.img, this.loc.x, this.loc.y - offset, sc_x, sc_y);
  166.  
  167. // Buttons on image
  168. // ctx.fillStyle = "#000000";
  169. // ctx.fillRect( this.loc.x + (sc_x - 100), this.loc.y+sc_y, 40,50 ) ;
  170. // ctx.strokeStyle = "#ffffff";
  171. // ctx.beginPath();
  172. // ctx.moveTo(this.loc.x + (sc_x - 100) + 10, this.loc.y+sc_y + 25);
  173. // ctx.lineTo(this.loc.x + (sc_x - 100) + 30, this.loc.y+sc_y + 25);
  174. // ctx.moveTo(this.loc.x + (sc_x - 100) + 20, this.loc.y+sc_y + 15);
  175. // ctx.lineTo(this.loc.x + (sc_x - 100) + 20, this.loc.y+sc_y + 35);
  176. // ctx.stroke();
  177. // ctx.closePath();
  178.  
  179. // ctx.fillRect( this.loc.x + (sc_x - 50), this.loc.y+sc_y, 40,50 ) ;
  180. // // Draw the minus sign
  181. // ctx.beginPath();
  182. // ctx.moveTo(this.loc.x + (sc_x - 50) + 10, this.loc.y+sc_y + 25);
  183. // ctx.lineTo(this.loc.x + (sc_x - 50) + 30, this.loc.y+sc_y + 25);
  184. // ctx.stroke();
  185. // ctx.closePath();
  186.  
  187. // Draw any assets that are attached to the image
  188. for(var i=0;!cursor.moving && i<this.assets.length;i++){
  189. var ass = this.assets[i];
  190. ass.draw(ctx, offset, this);
  191. }
  192.  
  193. // Resize pips - bottom right hand corner
  194. ctx.strokeStyle = '#222222';
  195. ctx.beginPath();
  196. ctx.moveTo(this.loc.x + this.size.x - 20, this.loc.y + this.size.y - offset - 2);
  197. ctx.lineTo(this.loc.x + this.size.x - 2, this.loc.y + this.size.y - offset - 20);
  198. ctx.moveTo(this.loc.x + this.size.x - 15, this.loc.y + this.size.y - offset - 2);
  199. ctx.lineTo(this.loc.x + this.size.x - 2, this.loc.y + this.size.y - offset - 15);
  200. ctx.moveTo(this.loc.x + this.size.x - 10, this.loc.y + this.size.y - offset - 2);
  201. ctx.lineTo(this.loc.x + this.size.x - 2, this.loc.y + this.size.y - offset - 10);
  202. ctx.moveTo(this.loc.x + this.size.x - 5, this.loc.y + this.size.y - offset - 2);
  203. ctx.lineTo(this.loc.x + this.size.x - 2, this.loc.y + this.size.y - offset - 5);
  204. // ctx.moveTo(this.l)
  205. ctx.stroke();
  206. ctx.closePath();
  207. }
  208.  
  209. ctx.strokeStyle = old_style;
  210. ctx.fillStyle = old_fill;
  211. }
  212.  
  213. function ImageItemHitTest(_x, _y){
  214. var sc = scale[this.scale_index];
  215. var sc_x = Math.round(sc * this.size.x);
  216. var sc_y = Math.round(sc * this.size.y);
  217.  
  218. // Check if we have hit the drag bar
  219. if(_x>this.loc.x && _x < this.loc.x+sc_x)
  220. if(_y>this.loc.y -20 && _y < this.loc.y)
  221. return true;
  222. // if(_x>this.loc.x+sc_x - 100 && _x<this.loc.x+sc_x-60)
  223. // if(_y>this.loc.y+sc_y && _y<this.loc.y+sc_y+50)
  224. // this.scaleImage(true);
  225.  
  226. // if(_x>this.loc.x+sc_x - 40 && _x<this.loc.x+sc_x)
  227. // if(_y>this.loc.y+sc_y && _y<this.loc.y+sc_y+50)
  228. // this.scaleImage(false);
  229. return false;
  230. }
  231.  
  232. function ImageItemScale(grow){
  233. if(grow){
  234. this.scale_index += 1;
  235. }else{
  236. this.scale_index -= 1;
  237. }
  238. if(this.scale_index < 0){
  239. this.scale_index = 0;
  240. }
  241. if(this.scale_index > scale.length -1){
  242. this.scale_index = scale.length - 1;
  243. }
  244. }
  245.  
  246. function ImageItemHitTestResize(x, y, offset){
  247. // Check if the point given is over the resize bar
  248. if(x > this.loc.x + this.size.x - 20 && x < this.loc.x + this.size.x
  249. && y > this.loc.y + this.size.y - offset - 20 && y < this.loc.y + this.size.y - offset ){
  250. return true;
  251. }
  252. return false;
  253. }
  254.  
  255. function ImageItemResize(diff_x, diff_y){
  256. this.size.x += diff_x;
  257. this.size.y += diff_y;
  258.  
  259. // Bounds - don't let be smaller than 100x100
  260. if(this.size.x < 100){
  261. this.size.x = 100;
  262. }
  263. if(this.size.y < 100){
  264. this.size.y = 100;
  265. }
  266. }
  267.  
  268. function ImageItemStartResize(){
  269.  
  270. }
  271.  
  272. function ImageItemBodyHitTest(x, y){
  273. if(x > this.loc.x && x < this.loc.x + this.size.x && y > this.loc.y && y < this.loc.y + this.size.y){
  274. return true;
  275. }
  276. return false;
  277. }
  278.  
  279. function ImageItemAddAsset(asset){
  280.  
  281. if(asset.type !== "line"){
  282. console.log("ERROR: We only handle lines for textboxes");
  283. return;
  284. }
  285.  
  286. for(var i=0;i<asset.points.length;i++){
  287. asset.points[i].x -= this.loc.x;
  288. asset.points[i].y -= this.loc.y; // The offset is to make sure we respect the scroll amount
  289. }
  290. console.log(asset.points);
  291. this.assets.push(asset);
  292. this.assets_dirty = true;
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement