Guest User

Untitled

a guest
May 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. <canvas id="world" width="300" height="300"></canvas>
  2. <canvas id="dst" width="300" height="300"></canvas>
  3. <script>
  4. var start = (new Date).getTime();
  5.  
  6. var canvasSrc = document.getElementById("world");
  7. var canvasDst = document.getElementById("dst");
  8. var contextSrc = canvasSrc.getContext("2d");
  9. var contextDst = canvasDst.getContext("2d");
  10.  
  11. var width = canvasSrc.width;
  12. var height = canvasSrc.height;
  13.  
  14. var lightX = 190;
  15. var lightY = 120;
  16. var step = 1;
  17.  
  18.  
  19. contextSrc.fillStyle = '#000';
  20. contextSrc.strokeStyle = '#FFC';
  21. contextSrc.lineWidth = 1;
  22. contextSrc.fillRect(0,0,width, height);
  23.  
  24. contextSrc.fillStyle = "#FF0000";
  25. contextSrc.fillRect(25,25,100,100);
  26.  
  27.  
  28. contextSrc.strokeStyle = '#0EC';
  29. contextSrc.moveTo(50,0);
  30. contextSrc.lineTo(50,300);
  31. contextSrc.stroke();
  32.  
  33.  
  34. contextSrc.strokeStyle = '#0EC';
  35. contextSrc.moveTo(150,0);
  36. contextSrc.lineTo(150,300);
  37. contextSrc.stroke();
  38.  
  39.  
  40. contextSrc.strokeStyle = '#0EC';
  41. contextSrc.moveTo(250,0);
  42. contextSrc.lineTo(250,300);
  43. contextSrc.stroke();
  44.  
  45. function LineTo(c, ox, oy, dx, dy) {
  46.  
  47.  
  48. var imgData = c.getImageData(1,1,width, height);
  49. var pix = imgData.data;
  50. var minx = Math.min(ox,dx);
  51. var maxx = Math.max(ox,dx);
  52. var miny = Math.min(oy,dy);
  53. var maxy = Math.max(oy,dy);
  54.  
  55. var i,j;
  56.  
  57. for (i=minx; i<maxx; i+=5) {
  58. for (j=miny; j<maxy; j+=5) {
  59. pos = (width*i+j)*4;
  60. pix[pos] = 0xFF;
  61. }
  62. }
  63.  
  64. c.putImageData(imgData,1,1);
  65.  
  66. }
  67.  
  68. var imgData = contextSrc.getImageData(1,1,width, height);
  69. var pix = imgData.data;
  70.  
  71. var diff;
  72. var angle = 60;
  73. var torch_power = 50;
  74. var ray_angle, angle;
  75. var lightAngle = 360;
  76. var angleStep = 120;
  77. var x,y;
  78.  
  79.  
  80. function trace(xa, xb, ya, yb) {
  81.  
  82. var a = (yb - ya) / (xb - xa);
  83.  
  84. var tmp;
  85. if (xa > xb) {
  86. tmp = xb;
  87. xb = xa;
  88. xa = tmp;
  89.  
  90. tmp = yb;
  91. yb = ya;
  92. ya = tmp;
  93. }
  94.  
  95. a = (yb - ya) / (xb - xa);
  96.  
  97. for (var i = xa; i<xb; i++) {
  98. var posx = i;
  99. var posy = a * ( i - xb ) + yb;
  100. contextSrc.fillRect(posx, posy, 1,1);
  101. }
  102.  
  103. }
  104.  
  105. contextSrc.beginPath();
  106. for (x=0; x<=lightAngle; x += (lightAngle/angleStep)) {
  107. ray_angle = angle/(Math.PI/180)-90-(lightAngle/2)+x;
  108. ray_angle = ray_angle*(Math.PI/180);
  109. cosray = Math.cos(ray_angle);
  110. sinray = Math.sin(ray_angle);
  111. for (y=1; y<=angleStep; y++) {
  112.  
  113. //trace(lightX, lightY, lightX+(torch_power/angleStep*y)*cosray, lightY+(torch_power/angleStep*y)*sinray);
  114.  
  115. contextSrc.moveTo(lightX, lightY);
  116. contextSrc.lineTo(lightX+(torch_power/angleStep*y)*cosray, lightY+(torch_power/angleStep*y)*sinray);
  117.  
  118. //LineTo(context, lightX, lightY, lightX+(torch_power/angleStep*y)*cosray, lightY+(torch_power/angleStep*y)*sinray);
  119.  
  120. }
  121. diff = (new Date).getTime() - start;
  122. //console.log(diff);
  123. }
  124. diff = (new Date).getTime() - start;
  125. contextSrc.stroke();
  126. diff = (new Date).getTime() - start;
  127. //console.log(diff);
  128. //contextSrc.closePath();
  129.  
  130.  
  131. contextSrc.fillStyle = "#00F0FF";
  132.  
  133. /*
  134. contextSrc.fillRect(xa, ya, 10,10);
  135. contextSrc.fillRect(xb, yb, 10,10);
  136. */
  137. //LineTo(context, 10,10 ,1000,1000);
  138.  
  139. /*
  140.  
  141. // top
  142. var a= lightY/ (lightX - i);
  143. var i,j;
  144. var curX, curY;
  145. var color = 255;
  146. for (i=0; i<lightX; i+=step) {
  147. a = lightY / (lightX - i );
  148. curX = lightX;
  149. for(j=lightX; j>0; j-- ) {
  150. curX = j;
  151. curY = Math.floor((curX-i)*a);
  152. pos = (width*curY+curX)*4;
  153. console.log(curX, curY);
  154. if(pix[pos] === 0xFF) {
  155. pix[pos] = 0x00;
  156. break;
  157. } else {
  158. pix[pos] = 0xFF;
  159. }
  160. }
  161. }
  162.  
  163. //contextSrc.putImageData(imgData,1,1);
  164. contextSrc.fillStyle = "#0000FFAA";
  165. contextSrc.fillRect(25,25,100,100);
  166. contextSrc.fillStyle = "#00FF00";
  167. contextSrc.arc(lightX,lightY,10,0,Math.PI*2, true);
  168. contextSrc.fill();
  169.  
  170.  
  171. // right
  172. for (i=0; i<height; i+=step) {
  173. contextSrc.beginPath();
  174. contextSrc.moveTo(lightX, lightY);
  175. contextSrc.lineTo(width, i);
  176. contextSrc.stroke();
  177. contextSrc.closePath();
  178. }
  179.  
  180. // bottom
  181. for (i=0; i<width; i+=step) {
  182. contextSrc.beginPath();
  183. contextSrc.moveTo(lightX, lightY);
  184. contextSrc.lineTo(i, height);
  185. contextSrc.stroke();
  186. contextSrc.closePath();
  187. }
  188.  
  189. // left
  190. for (i=0; i<width; i+=step) {
  191. contextSrc.beginPath();
  192. contextSrc.moveTo(lightX, lightY);
  193. contextSrc.lineTo(0, i);
  194. contextSrc.stroke();
  195. contextSrc.closePath();
  196. }
  197.  
  198. */
  199.  
  200. function doit(a) {
  201.  
  202.  
  203. var imgDataSrc = contextSrc.getImageData(1,1, width, height);
  204. var pixSrc = imgDataSrc.data;
  205.  
  206. var imgDataDst = contextDst.getImageData(1,1, width, height);
  207. var pixDst = imgDataDst.data;
  208.  
  209.  
  210.  
  211. start = (new Date).getTime();
  212. step = 1;
  213. centerX = width/2;
  214. centerY = height/2;
  215. cnt = 0;
  216. for (j=1; j<height; j+=step) {
  217.  
  218. for (i=1; i<width; i+=step) {
  219.  
  220. cnt++;
  221.  
  222. posX = i - centerX;
  223. posY = j - centerY;
  224.  
  225. r = Math.floor(Math.sqrt(posX*posX + posY*posY))*2;
  226. //theta = Math.floor(Math.atan(posY/posX)*180);
  227. theta = Math.floor(Math.atan2(-posY,posX)*180/Math.PI) % 360;
  228.  
  229.  
  230. if (theta < 0) {
  231. theta += 360;
  232. }
  233.  
  234.  
  235. if (theta > 359) {
  236. console.log(r,theta);
  237. debugger;
  238. }
  239. if (theta < 0) {
  240. console.log(r,theta);
  241. debugger;
  242. }
  243. theta = theta / 360 * height;
  244.  
  245. pos = (j-1)*width*4 + (i-1)*4;
  246. posSrc = (r-1)*width*4 + (theta-1)*4;
  247.  
  248. pixDst[pos+0] = pixSrc[posSrc+0];
  249. pixDst[pos+1] = pixSrc[posSrc+1];
  250. pixDst[pos+2] = pixSrc[posSrc+2];
  251. pixDst[pos+3] = pixSrc[posSrc+3];
  252.  
  253. //pixDst[i] = 125;
  254.  
  255. }
  256.  
  257. }
  258.  
  259.  
  260. console.log('end:', cnt);
  261. contextDst.putImageData(imgDataDst,1,1);
  262. diff = (new Date).getTime() - start;
  263. console.log(diff);
  264. }
  265. doit(45);
  266. </script>
Add Comment
Please, Sign In to add comment