Advertisement
nzisaacnz

Heat diffusion (rough)

Sep 18th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 8.01 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script>
  4. var Template = {};
  5. var requestAnimationFrame =
  6. window.requestAnimationFrame||
  7. window.webkitRequestAnimationFrame||
  8. window.mozRequestAnimationFrame||
  9. window.oRequestAnimationFrame||
  10. window.msRequestAnimationFrame;
  11.  
  12. var cancelAnimationFrame =
  13. window.cancelAnimationFrame||
  14. window.webkitCancelRequestAnimationFrame||
  15. window.webkitCancelAnimationFrame||
  16. window.mozCancelRequestAnimationFrame||
  17. window.mozCancelAnimationFrame||
  18. window.oCancelRequestAnimationFrame||
  19. window.oCancelAnimationFrame||
  20. window.msCancelRequestAnimationFrame||
  21. window.msCancelAnimationFrame;
  22.  
  23. Template.newTime = Date.now();
  24. Template.time = 0;
  25. Template.animation = function()
  26. {
  27.     if(!Template.stopped)
  28.     {
  29.         Template.oldTime = Template.newTime;
  30.         if(!Template.paused)
  31.         {
  32.             Template.calculate();
  33.         }
  34.         Template.draw();
  35.         Template.newTime = Date.now();
  36.         Template.time=Template.newTime-Template.oldTime;
  37.         Template.animationFrame = requestAnimationFrame(Template.animation);
  38.     }
  39. }
  40. Template.start = function(d,c)
  41. {
  42.     Template.draw = d;
  43.     Template.calculate = c;
  44.     Template.paused = false;
  45.     Template.animationFrame = requestAnimationFrame(Template.animation);
  46. }
  47. Template.stop = function()
  48. {
  49.     cancelAnimationFrame(Template.animationFrame);
  50. }
  51. function Transition(start,end,period,loops,func)
  52. {
  53.     this.start = start;
  54.     this.end = end;
  55.     this.period = period;
  56.     if(!func)this.func = function(a){return a;};
  57.     else this.func = func;
  58.     this.pos = 0;
  59.     this.reversed;
  60.     this.loop = loops;
  61.     this.res = 0;
  62.     this.calc=function()
  63.     {
  64.         if(this.reversed) this.pos -= Template.time/this.period;
  65.         else this.pos += Template.time/this.period;
  66.         if(this.loop===0)
  67.         {
  68.             this.pos = (this.pos+1)%1;
  69.         }
  70.         else
  71.         {
  72.             this.pos = Math.min(this.loop,Math.max(this.pos,0));
  73.         }
  74.         this.res = this.func(this.pos)*(end-start)+start;
  75.     }
  76.     this.get = function()
  77.     {
  78.         return this.res;
  79.     }
  80.     this.reverse = function()
  81.     {
  82.         this.reversed = !this.reversed;
  83.     }
  84. }
  85. Template.random = function(a,b,round)
  86. {
  87.     if(round)return parseInt(Math.random()*(b-a)+a)
  88.     return Math.random()*(b-a)+a;
  89. }
  90. </script>
  91. <script>
  92. function Point(x,y)
  93. {
  94.     this.x = x;
  95.     this.y = y;
  96.     this.draw = function(G)
  97.     {
  98.         G.beginPath();
  99.         G.moveTo(this.x+10,this.y);
  100.         G.arc(this.x,this.y,10,0,2*Math.PI);
  101.         G.stroke();
  102.     }
  103. }
  104. function Line(xs,ys,xe,ye)
  105. {
  106.     this.start = new Point(xs,ys);
  107.     this.end = new Point(xe,ye);
  108.     this.Intersects = function(line,x,y,x1,y1)
  109.     {
  110.         x = +x|0;
  111.         y = +y|0;
  112.         x1 = +x1|0;
  113.         y1 = +y1|0;
  114.         return get_line_intersection(this.start.x+x,this.start.y+y,
  115.         this.end.x+x,this.end.y+y,
  116.         line.start.x+x1,line.start.y+y1,
  117.         line.end.x+x1,line.end.y+y1);
  118.     }
  119. }
  120.  
  121. function Mesh(x,y,strokeStyle,fillStyle)
  122. {
  123.     this.prevPoint = null;
  124.     this.lines = [];
  125.     this.x = x;
  126.     this.y = y;
  127.     this.strokeStyle = strokeStyle;
  128.     this.fillStyle = fillStyle;
  129.     this.maxX = 5;
  130.     this.add = function(point,index)
  131.     {
  132.         if(this.prevPoint)
  133.         {
  134.             index = +index|this.lines.length;
  135.             this.lines.splice(index,0,new Line(this.prevPoint.x,this.prevPoint.y,point.x,point.y));
  136.         }
  137.         this.prevPoint = new Point(point.x,point.y);
  138.     }
  139.     this.collisions = function(thing,breakOnIntersect)
  140.     {
  141.         var cols = [];
  142.         if(thing.constructor == Mesh)
  143.         {
  144.             var mesh = thing;
  145.             for(var a=0,aa=this.lines.length; a<aa; a++)
  146.             {
  147.                 for(var b=0,bb=mesh.lines.length; b<bb; b++)
  148.                 {
  149.                     var collision = this.lines[a].Intersects(mesh.lines[b],this.x,this.y,mesh.x,mesh.y);
  150.                     if(breakOnIntersect && collision) return true;
  151.                     if(collision)cols[cols.length] = collision;
  152.                 }
  153.             }
  154.         }
  155.         else if(thing.constructor == Line)
  156.         {
  157.             var line = thing;
  158.             for(var a=0,aa=this.lines.length; a<aa; a++)
  159.             {
  160.                 var collision = this.lines[a].Intersects(line,this.x,this.y);
  161.                 if(breakOnIntersect && collision) return true;
  162.                 if(collision)cols[cols.length] = collision;
  163.             }
  164.         }
  165.         return breakOnIntersect?false:cols;
  166.     }
  167.     this.IsPointInside = function(point)
  168.     {
  169.         return this.collisions(new Line(point.x,point.y,cvs.width*50,point.y),false).length%2==1;
  170.     }
  171.     this.FinishMesh = function()
  172.     {
  173.         if(this.lines.length==0)return false;
  174.         this.add(new Point(this.lines[0].start.x,this.lines[0].start.y));
  175.         this.finished = true;
  176.         return true;
  177.     }
  178.     this.draw = function(G)
  179.     {
  180.         G.strokeStyle = this.strokeStyle;
  181.         G.fillStyle = this.fillStyle;
  182.         if(this.lines.length==0)return false;
  183.         G.beginPath();
  184.         G.moveTo(this.lines[0].start.x+this.x,this.lines[0].start.y+this.y);
  185.         for(var a=0; a<this.lines.length; a++)
  186.         {
  187.             G.lineTo(this.lines[a].end.x+this.x,this.lines[a].end.y+this.y);
  188.         }
  189.         if(this.finished)G.fill();
  190.         G.stroke();
  191.         return true;
  192.     }
  193. }
  194. function get_line_intersection(p0_x,p0_y,p1_x,p1_y,p2_x,p2_y,p3_x,p3_y)
  195. {
  196.     var s1_x, s1_y, s2_x, s2_y;
  197.     s1_x = p1_x - p0_x;     s1_y = p1_y - p0_y;
  198.     s2_x = p3_x - p2_x;     s2_y = p3_y - p2_y;
  199.    
  200.     var cache2=(-s2_x * s1_y + s1_x * s2_y);
  201.    
  202.     if(cache2===0) return false; // Parallel
  203.    
  204.     var s, t;
  205.     var cache0=(p0_y - p2_y);
  206.     var cache1=(p0_x - p2_x);
  207.    
  208.     s = (-s1_y * cache1 + s1_x * cache0) / cache2;
  209.     t = ( s2_x * cache0 - s2_y * cache1) / cache2;
  210.    
  211.     if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
  212.     {
  213.         // Collision detected
  214.         return new Point(p0_x + (t * s1_x),p0_y + (t * s1_y));
  215.     }
  216.    
  217.     return false; // No collision
  218. }
  219. </script>
  220. </head>
  221. <body>
  222. <canvas id="cvs" width="800" height="600" onmousewheel="mouseEvent(event)" onmousemove="mouseEvent(event)" onmousedown="mouseEvent(event)" onmouseup="mouseEvent(event)" onclick="mouseEvent(event)"></canvas>
  223. <script>
  224. var mouse = {x:null,y:null};
  225. var base = 1.2;
  226.  
  227. var totalTime = 0;var intersections = [];
  228. var map;
  229. var loc = {};
  230. window.parent.variable = loc;
  231. var width = 10;
  232. var height = 100;
  233. function mouseEvent(e)
  234. {
  235.     var m = Math.pow(base,loc.scale);
  236.     mouse.x = e.offsetX-0.5;
  237.     mouse.y = e.offsetY;
  238.     switch(e.type)
  239.     {
  240.         case "click":
  241.         mouse.button = e.button
  242.         e.preventDefault();
  243.         break;
  244.         case "mousemove":
  245.         if(mouse.down)
  246.         {
  247.             if(map[parseInt((mouse.x)/m)]!=null&&map[parseInt((mouse.x)/m)][parseInt(mouse.y/m)]!=null)
  248.             map[parseInt((mouse.x)/m)][parseInt(mouse.y/m)]+=mouse.button?-1000:1000;
  249.         }
  250.         break;
  251.        
  252.         case "mousedown":
  253.         mouse.button = e.button
  254.         mouse.down = true;
  255.         e.preventDefault();
  256.         break;
  257.         case "mouseup":
  258.         mouse.down = false;
  259.         break;
  260.         case "mousewheel":
  261.         var oldScale = Math.pow(base,loc.scale);
  262.         if(e.wheelDelta>0)loc.scale++;
  263.         else loc.scale--;
  264.         var newScale = Math.pow(base,loc.scale);
  265.         console.clear();
  266.         console.log(JSON.stringify([mouse,loc,newScale,oldScale,mouse.x/oldScale]).replace(/,/gm,"\n"));
  267.         //loc.pos.x-=loc.pos.x-mouse.x/oldScale;
  268.         e.preventDefault();
  269.         break;
  270.     }
  271. }
  272. function init()
  273. {
  274.     loc.scale = 7;
  275.     loc.pos = new Point(0,0);
  276.     map = [];
  277.     for(var a=0; a<width; a++)
  278.     {
  279.         map[a] = [];
  280.         for(var b=0; b<height; b++)
  281.         {
  282.             map[a][b] = 0;
  283.         }
  284.     }
  285.     Template.start(draw,calc);
  286. }
  287. function draw()
  288. {
  289.     var G = cvs.getContext("2d");
  290.     G.clearRect(0,0,cvs.width,cvs.height);
  291.     var m = Math.pow(base,loc.scale);
  292.     for(var a=0,aa=map.length; a<aa; a++)
  293.     {
  294.         for(var b=0,bb=map[a].length; b<bb; b++)
  295.         {
  296.             var x = (loc.pos.x+a)*m;
  297.             var y = (loc.pos.y+b)*m;
  298.             if(x<cvs.width && x>-m)
  299.             {
  300.                 G.fillStyle = "hsl("+map[a][b]+",50%,50%)";
  301.                
  302.                 G.fillRect(x,y,m+1,m+1)
  303.             }
  304.         }
  305.     }
  306. }
  307. function calc()
  308. {
  309.     if(parseInt((totalTime+Template.time)/1)>parseInt(totalTime/1))
  310.     {
  311.        
  312.         var newMap = [];
  313.         for(var a=0; a<map.length; a++)
  314.         {
  315.             newMap[a] = [];
  316.             for(var b=0; b<map[a].length; b++)
  317.             {
  318.                 newMap[a][b]=map[a][b];
  319.             }
  320.         }
  321.         for(var a=0; a<map.length; a++)
  322.         {
  323.             for(var b=0; b<map[a].length; b++)
  324.             {
  325.                 var divides = 0;
  326.                 var d = map[a][b]/6;
  327.                 if(a>0)
  328.                 {
  329.                     divides+=d;
  330.                     newMap[a-1][b]+=d;
  331.                 }
  332.                 if(b>0)
  333.                 {
  334.                     divides+=d;
  335.                     newMap[a][b-1]+=d;
  336.                 }
  337.                 if(a<map.length-1)
  338.                 {
  339.                     divides+=d;
  340.                     newMap[a+1][b]+=d;
  341.                 }
  342.                 if(b<map[a].length-1)
  343.                 {
  344.                     divides+=d;
  345.                     newMap[a][b+1]+=d;
  346.                 }
  347.                 newMap[a][b]-=divides;
  348.             }
  349.         }
  350.         map=newMap;
  351.     }
  352.     totalTime+=Template.time;
  353. }
  354. init();
  355. </script>
  356. </body>
  357. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement