Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.66 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.  
  5. <style>
  6. * {
  7. box-sizing: border-box;
  8. }
  9.  
  10. /* Create three unequal columns that floats next to each other */
  11. .column {
  12. float: left;
  13. padding: 10px;
  14. height: 501px;
  15. }
  16.  
  17. .left, .right {
  18. text-align: center;
  19. margin-top: 10px;
  20. width: 270px;
  21. border: 5px solid brown;
  22. background-color: #bbb;
  23. overflow-y: scroll;
  24. /* TODO: Add a property to make the two div's containing the wearables become srollable. */
  25. }
  26.  
  27. .middle {
  28. width: 620px;
  29. }
  30.  
  31. canvas {
  32. border: 1px solid black;
  33. }
  34. </style>
  35.  
  36. <script type="text/javascript">
  37. function allowDrop(ev) {
  38. ev.preventDefault();
  39. }
  40.  
  41. function drag(ev) {
  42. // TODO: put the id of the dragged element into data transfer
  43. ev.dataTransfer.setData("wear", ev.target.wearable.id);
  44. ev.dataTransfer.setData("type", ev.target.wearable.dropId);
  45. ev.dataTransfer.setData("fpath", ev.target.wearable.fPath);
  46. ev.dataTransfer.setData("posX", ev.target.wearable.fPath);
  47. ev.dataTransfer.setData("posY", ev.target.wearable.fPath);
  48. hintSpan.innerHTML="Hint: Drop me onto the "+ev.target.wearable.dropId+" of the girl.";
  49. // Also show the hint "Hint: Drop me onto the ... of the girl." on the hintSpan element
  50. }
  51.  
  52. function drop(ev) {
  53. // TODO: Implement this function to redraw the canvas when a wearable is dropped onto a right droppable area.
  54. ev.preventDefault();
  55. var id = ev.dataTransfer.getData("type");
  56. var data = ev.dataTransfer.getData("wear");
  57. var fpath = ev.dataTransfer.getData("fpath");
  58. var posX = ev.dataTransfer.getData("posX");
  59. var posY = ev.dataTransfer.getData("posY");
  60. var targetArea;
  61. var cordX;
  62. var cordY;
  63. ev.target.appendChild(document.getElementById(data));
  64. console.log(ev.offsetX);
  65. cordX=ev.offsetX;
  66. cordY=ev.offsetY;
  67. console.log(ev.offsetY);
  68. const mousePos = {
  69. x: ev.clientX - canvas.offsetLeft,
  70. y: ev.clientY - canvas.offsetTop
  71. };
  72. const pixel = hitCtx.getImageData(mousePos.x, mousePos.y, 1, 1).data;
  73. const color = `rgb(${pixel[0]},${pixel[1]},${pixel[2]})`;
  74. console.log(color);
  75. const shape = colorsHash[color];
  76. if (shape.id == id) {
  77. console.log("yeet");
  78. var wear = new Image();
  79. wear.id = data;
  80. wear.src = fpath;
  81. ctx.drawImage(wear,posX,posY,wear.width/2,wear.height/2);
  82. }else{
  83. console.log("nah");
  84. }
  85. // Tips: - Get the id of the dragged element (dragId).
  86. // - Detect the current mouse position.
  87. // - Study the click event listener function for canvas.
  88. // - Look for the wearable of id = dragId in the wearables array
  89. // - Compare the found wearable's dropId and the droppable area's id to see if the drop is valid.
  90. // If they are the same, that means valid. Then set the 'dropped' field of the droppable area
  91. // to the object reference of the wearable.
  92. // - The redraw() function should be called in this case.
  93. // Think of whether you should pass true or false for the redraw() function here.
  94. }
  95. </script>
  96. </head>
  97.  
  98. <body>
  99.  
  100. <div class="row">
  101. <div class="column left" id="leftDiv">
  102. </div>
  103. <div class="column middle" id="midDiv">
  104. <canvas id="canvas" width="600" height="500" ondrop="drop(event)" ondragover="allowDrop(event)">
  105. Your browser does not support canvas.
  106. </canvas>
  107. <button id="reset">Reset</button>
  108. &nbsp; Canvas coordinate: (<span id="xPos">x</span>, <span id="yPos">y</span>)
  109. &nbsp; &nbsp; <span id="hint"></span>
  110. </div>
  111. <div class="column right" id="rightDiv">
  112. </div>
  113. <div id="debug">i am here</div>
  114. </div>
  115. <script>
  116. const canvas = document.getElementById('canvas');
  117. const ctx = canvas.getContext('2d');
  118.  
  119. const hitCanvas = document.createElement('canvas');
  120. hitCanvas.width = 600;
  121. hitCanvas.height = 500;
  122. const hitCtx = hitCanvas.getContext('2d');
  123.  
  124.  
  125. // uncomment this if you want to see the hidden canvas for testing or debugging
  126. document.getElementById('midDiv').appendChild(hitCanvas);
  127.  
  128.  
  129. const colorsHash = {};
  130.  
  131. // a function that generates a random color
  132. function getRandomColor() {
  133. const r = Math.round(Math.random() * 255);
  134. const g = Math.round(Math.random() * 255);
  135. const b = Math.round(Math.random() * 255);
  136. return `rgb(${r},${g},${b})`;
  137. }
  138.  
  139. // the sequence of drawing the wearables depends on the sequence of the elements listed here
  140. const dropAreas = [
  141. {id: 'feet', positions: [ [250, 400], [350, 400], [350, 460], [250, 460] ], dropped: null},
  142. {id: 'hip', positions: [ [240, 295], [360, 295], [360, 360], [240, 360] ], dropped: null},
  143. {id: 'body', positions: [ [205, 216], [400, 216], [400, 317], [205, 317] ], dropped: null},
  144. {id: 'head', positions: [ [210, 144], [220, 77], [300, 30], [374, 77], [390, 144] ], dropped: null},
  145. ];
  146.  
  147. // for each shape of droppable area, assign a unique randome color
  148. dropAreas.forEach(shape => {
  149. while (true) {
  150. const colorKey = getRandomColor();
  151. if (!colorsHash[colorKey]) {
  152. shape.colorKey = colorKey;
  153. console.log(colorKey);
  154. colorsHash[colorKey] = shape;
  155. return;
  156. }
  157. }
  158. });
  159.  
  160. // the set of wearable items (dresses, shirts, pants, etc.)
  161. const wearables = [
  162. {id: 'headwear1', pos: [222, 50], dropId: 'head', wardrobe: 'right', fPath: 'images/Headwear1.png'},
  163. {id: 'hat1', pos: [179, 1], dropId: 'head', wardrobe: 'right', fPath: 'images/Hat1.png'},
  164. {id: 'hat2', pos: [188, 12], dropId: 'head', wardrobe: 'right', fPath: 'images/Hat2.png'},
  165. {id: 'hat3', pos: [205, 5], dropId: 'head', wardrobe: 'right', fPath: 'images/Hat3.png'},
  166. {id: 'dress1', pos: [227, 210], dropId: 'body', wardrobe: 'left', fPath: 'images/Dress1.png'},
  167. {id: 'dress2', pos: [230, 215], dropId: 'body', wardrobe: 'left', fPath: 'images/Dress2.png'},
  168. {id: 'shirt1', pos: [220, 215], dropId: 'body', wardrobe: 'left', fPath: 'images/Shirt1.png'},
  169. {id: 'shirt2', pos: [246, 215], dropId: 'body', wardrobe: 'left', fPath: 'images/Shirt2.png'},
  170. {id: 'pants1', pos: [258, 300], dropId: 'hip', wardrobe: 'right', fPath: 'images/Pants1.png'},
  171. {id: 'pants2', pos: [258, 305], dropId: 'hip', wardrobe: 'right', fPath: 'images/Pants2.png'},
  172. {id: 'shoe1', pos: [251, 405], dropId: 'feet', wardrobe: 'right', fPath: 'images/Shoe1.png'},
  173. ];
  174.  
  175. const SCALE = 0.5;
  176. var hintSpan = document.getElementById("hint");
  177.  
  178. // creates images of wearable items inside the left and right div's (representing the wardrobes)
  179. wearables.forEach(wearable => {
  180. var img = new Image();
  181. img.id = wearable.id;
  182. img.src = wearable.fPath;// TODO ;
  183.  
  184. img.onload = function(){
  185. img.width = img.width * SCALE;
  186. img.draggable = true;
  187. img.ondragstart = drag;
  188. img.wearable = wearable;
  189. img.ondragend = function(){
  190. // TODO: Clear the hint "Hint: Drop me onto the ... of the girl."
  191. hintSpan.innerHTML="";
  192. };
  193. var wardrobe = document.getElementById(wearable.wardrobe + "Div");
  194. var hr = document.createElement("hr");
  195. wardrobe.appendChild(img);
  196. wardrobe.appendChild(hr);
  197. }
  198. });
  199.  
  200.  
  201. function drawShape(context, shape) {
  202.  
  203.  
  204. // TODO: Implement this function by following the comments below
  205. context.beginPath();
  206. context.moveTo(shape.positions[0][0], shape.positions[0][1]);
  207. for(var i = 1; i < shape.positions.length; i++){
  208. context.lineTo(shape.positions[i][0], shape.positions[i][1]);
  209. }
  210. context.moveTo(shape.positions[0][0], shape.positions[0][1]);
  211. context.fillStyle = shape.colorKey;
  212. context.fill();
  213.  
  214. // begin the path in the context
  215.  
  216. // move to the first position
  217.  
  218. // then make a line to other positions using a for loop
  219.  
  220. // close the path
  221.  
  222. // fill in the color specified by the shape's colorKey
  223. }
  224.  
  225. // draw the shapes of droppable areas in the hidden canvas
  226. dropAreas.forEach(shape => {
  227. drawShape(hitCtx, shape);
  228. });
  229.  
  230. /* A test function which alerts a message when a mouse click is done within a droppable area on the visible canvas.
  231. A droppable area is predefined by drawing a colored shape in the hidden canvas.
  232. The width and height of the visible canvas and the hidden canvas are the same.
  233. We check the color of the pixel in the hidden canvas at the coordinates of the current mouse position.
  234. If the color can match to a shape via the colorsHash array, that means the click is upon a droppable area.
  235. This example is a helpful basis for you to learn how to implement the drop() function.
  236. */
  237. canvas.addEventListener('click', (e) => {
  238. const mousePos = {
  239. x: e.clientX - canvas.offsetLeft,
  240. y: e.clientY - canvas.offsetTop
  241. };
  242. const pixel = hitCtx.getImageData(mousePos.x, mousePos.y, 1, 1).data;
  243. const color = `rgb(${pixel[0]},${pixel[1]},${pixel[2]})`;
  244. console.log(color);
  245. const shape = colorsHash[color];
  246. if (shape) {
  247. alert('click on shape: ' + shape.id);
  248. }
  249. });
  250.  
  251.  
  252. var xSpan = document.getElementById('xPos');
  253. var ySpan = document.getElementById('yPos');
  254.  
  255. canvas.addEventListener('mousemove', (e) => {
  256. xSpan.innerHTML = e.offsetX;
  257. ySpan.innerHTML = e.offsetY;
  258. // TODO: show the canvas coordinate of the current mouse position in xSpan and ySpan
  259. });
  260.  
  261.  
  262. // draw background
  263.  
  264. const BG_SCALE = 1.02;
  265. var bg = new Image();
  266. bg.onload = drawBackground;
  267. bg.src = "images/background/4.jpg";
  268.  
  269. function drawBackground() {
  270. ctx.save();
  271. ctx.globalAlpha = 0.6; // make the image semi-transparent
  272. ctx.drawImage(bg,canvas.width/2-bg.width*BG_SCALE/2, canvas.height/2-bg.height*BG_SCALE/2,bg.width*BG_SCALE,bg.height*BG_SCALE);
  273. // TODO: Draw the given background image on canvas
  274. // Note: Make sure to center the image properly and adjust its size with the BG_SCALE constant.
  275.  
  276. ctx.restore();
  277. };
  278.  
  279. // draw an image
  280. var girl = new Image();
  281. girl.src = "images/Girl.png";
  282. girl.onload = drawForeground;
  283.  
  284. function drawForeground() {
  285.  
  286. // TODO: Draw the given girl image on canvas
  287. ctx.drawImage(girl, canvas.width/2-girl.width*0.5/2, canvas.height/2-girl.height*0.5/2,girl.width*0.5,girl.height*0.5);
  288. // Note: Make sure to center the image properly and adjust its size with the SCALE constant.
  289.  
  290. // for each droppable area, if a wearable has been dropped onto it, then draw the wearable image on canvas
  291. dropAreas.forEach(shape => {
  292. var w = shape.dropped;
  293. if (w) {
  294. document.getElementById("debug").innerHTML=shape.fPath;
  295. // TODO: draw the wearable linked with each shape of droppable area.
  296. }
  297. });
  298. }
  299.  
  300. // Implement the reset button; reset will remove all weared items on the girl
  301. var resetButton = document.getElementById("reset");
  302. resetButton.addEventListener('click', function(){
  303. redraw(true);
  304. });
  305.  
  306. function redraw(clear) {
  307. ctx.clearRect(0, 0, canvas.width, canvas.height);
  308. if (clear) {
  309. // TODO: remove every wearable that was dropped into each of droppable area
  310. }
  311. drawBackground();
  312. drawForeground();
  313. }
  314. </script>
  315. </body>
  316. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement