Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. "use strict";
  2.  
  3. function initialize(theCanvasID) {
  4. theSimulation = new World(theCanvasID);
  5.  
  6. /** >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
  7. var kuchen;
  8.  
  9. theSimulation.addNewGroup("Kuchen", true, true);
  10. var i;
  11. for (i = 0; i < 10; i++) {
  12. kuchen = new Kuchen(theSimulation, "Kuchen" + i);
  13. kuchen.setPosRandom(50, 200, 500, 200);
  14. theSimulation.addObjectToGroup(kuchen, "Kuchen");
  15. }
  16.  
  17.  
  18. var smiley;
  19.  
  20. theSimulation.addNewGroup("Smiley", true, true);
  21. {
  22. var i;
  23. for (i = 0; i < 4; i++) {
  24. smiley = new Smiley(theSimulation, "Smiley" + i);
  25. smiley.setPosRandom(100, 200, 500, 200);
  26. smiley.setVelocityRandom(0, 1, 0, 0);
  27. smiley.setRotationVelocityRandom(0, 0);
  28. theSimulation.addObjectToGroup(smiley, "Smiley");
  29. }
  30. }
  31.  
  32.  
  33. /** <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
  34.  
  35. theSimulation.animate();
  36. }
  37.  
  38. class World extends Simulation {
  39. constructor(theCanvasID) {
  40. //Aufruf der Superklasse
  41. super(theCanvasID);
  42. }
  43.  
  44. //die Funktion drawBackground muss hier überschrieben werden.
  45. drawBackground() {
  46. this.ctx.save();
  47. /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
  48. this.ctx.fillStyle = "rgba(255,255,0,1.0)";
  49. this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
  50. this.ctx.fillStyle = "rgba(255,125,0,1.0)";
  51. this.ctx.fillRect(0, 200, this.canvas.width, this.canvas.height);
  52. this.ctx.fillStyle = "rgba(0,255,0,1.0)";
  53. this.ctx.fillRect(0, 275, this.canvas.width, this.canvas.height);
  54. this.ctx.fillStyle = "rgba(255,15,0,1.0)";
  55. this.ctx.fillRect(0, 375, this.canvas.width, this.canvas.height);
  56. /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
  57. this.ctx.restore();
  58.  
  59. }
  60. }
  61.  
  62. /** ******************************************************************** */
  63. /** ********** Objekte ************************************************** */
  64. /** ******************************************************************** */
  65.  
  66. class DemoBox extends SimObject {
  67. constructor(theSimulation, objName) {
  68. //Aufruf der Superklasse
  69. super(theSimulation, objName);
  70. }
  71. //die Funktion draw muss hier überschrieben werden.
  72. draw() {
  73. this.simInstance.ctx.save();
  74. // Transformation
  75. this.simInstance.ctx.translate(this.xPos, this.yPos);
  76. this.simInstance.ctx.rotate(this.rotation * Math.PI / 180);
  77. this.simInstance.ctx.scale(this.xScale, this.yScale);
  78.  
  79. /** *<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<** */
  80. // Farben festlegen
  81. this.simInstance.ctx.fillStyle = "rgba(255,0,0,1.0)";
  82.  
  83. // Objekt zeichnen
  84. this.simInstance.ctx.fillRect(0, 0, 50, 50);
  85. /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
  86.  
  87. this.simInstance.ctx.restore();
  88. }
  89. }
  90.  
  91. class Kuchen extends SimObject {
  92. img;
  93. constructor(theSimulation, objName) {
  94. super(theSimulation, objName);
  95. this.img = new Image();
  96. this.img.src = '../img/canvas-img/kuchen.jpg';
  97. }
  98. //die Funktion draw muss hier überschrieben werden.
  99. draw() {
  100. this.simInstance.ctx.save();
  101. // Transformation
  102. this.simInstance.ctx.translate(this.xPos, this.yPos);
  103. this.simInstance.ctx.rotate(this.rotation * Math.PI / 180);
  104. this.simInstance.ctx.scale(this.xScale, this.yScale);
  105.  
  106. /** *<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<** */
  107. // Farben festlegen
  108. this.simInstance.ctx.drawImage(this.img, -35, -35);
  109. /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
  110.  
  111. this.simInstance.ctx.restore();
  112. }
  113. }
  114.  
  115.  
  116. class Smiley extends SimObject {
  117. img;
  118. imgArray = ['../img/canvas-img/smiley.png', '../img/canvas-img/smily2.png', '../images/smily3.png'];
  119. actImgIndex = 0;
  120. constructor(theSimulation, objName) {
  121. super(theSimulation, objName);
  122. var rnd = Math.random()*5+1;
  123. this.img = new Image();
  124. this.img.src = 'imgArray[rnd]';
  125. }
  126.  
  127. calcNextStep() {
  128. var theCanvasBox = theSimulation.getCanvasRect();
  129.  
  130. /** *<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<** */
  131.  
  132. if ((this.xPos >= theCanvasBox.width) || (this.xPos <= 0)) {
  133. //this.xPos = theCanvasBox.width;
  134. this.xVel = this.xVel * (-1);
  135. }
  136. if ((this.yPos >= theCanvasBox.height) || (this.yPos <= 0)) {
  137. //this.yPos = theCanvasBox.height;
  138. this.yVel = this.yVel * (-1);
  139. }
  140.  
  141. this.xVel = this.xVel + this.xAccel;
  142. this.yVel = this.yVel + this.yAccel;
  143. this.rotationVel = this.rotationVel + this.rotationAccel;
  144. this.xScaleVel = this.xScaleVel + this.xScaleAccel;
  145. this.yScaleVel = this.yScaleVel + this.yScaleAccel;
  146.  
  147. this.xPos = this.xPos + this.xVel;
  148. this.yPos = this.yPos + this.yVel;
  149.  
  150. this.rotation = this.rotation + this.rotationVel;
  151. this.xScale = this.xScale + this.xScaleVel;
  152. this.yScale = this.yScale + this.yScaleVel;
  153. /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
  154.  
  155. this.syncColBody();
  156. }
  157.  
  158. //die Funktion draw muss hier überschrieben werden.
  159. draw() {
  160. this.simInstance.ctx.save();
  161. // Transformation
  162. this.simInstance.ctx.translate(this.xPos, this.yPos);
  163. this.simInstance.ctx.rotate(this.rotation * Math.PI / 180);
  164. this.simInstance.ctx.scale(this.xScale, this.yScale);
  165.  
  166. /** *<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<** */
  167. this.simInstance.ctx.drawImage(this.img, -35, -35);
  168. /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
  169.  
  170. this.simInstance.ctx.restore();
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement