Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <style>
- #left,#right
- {
- position:absolute;
- top:10px;
- height:400px;
- width:25px;
- -webkit-appearance:slider-vertical;
- }
- #left
- {
- left:0%;
- }
- #right
- {
- right:0%;
- }
- #cvs
- {
- position:absolute;
- left:0;
- top:0px;
- }
- </style>
- </head>
- <body onclick="addKeyPoint(event)" onmousemove="cvsMouse(event)" onkeypress="randomPos()">
- <script>
- var Template = {};
- var requestAnimationFrame =
- window.requestAnimationFrame||
- window.webkitRequestAnimationFrame||
- window.mozRequestAnimationFrame||
- window.oRequestAnimationFrame||
- window.msRequestAnimationFrame;
- var cancelAnimationFrame =
- window.cancelAnimationFrame||
- window.webkitCancelRequestAnimationFrame||
- window.webkitCancelAnimationFrame||
- window.mozCancelRequestAnimationFrame||
- window.mozCancelAnimationFrame||
- window.oCancelRequestAnimationFrame||
- window.oCancelAnimationFrame||
- window.msCancelRequestAnimationFrame||
- window.msCancelAnimationFrame;
- Template.newTime = Date.now();
- Template.time = 0;
- Template.animation = function()
- {
- if(!Template.stopped)
- {
- Template.oldTime = Template.newTime;
- Template.newTime = Date.now();
- Template.time=Template.newTime-Template.oldTime;
- if(!Template.paused)
- {
- Template.calculate(Template.time);
- }
- Template.draw(Template.graphics);
- Template.animationFrame = requestAnimationFrame(Template.animation);
- }
- }
- Template.start = function(d,c,g)
- {
- Template.draw = d;
- Template.calculate = c;
- Template.paused = false;
- Template.graphics = g;
- Template.animationFrame = requestAnimationFrame(Template.animation);
- }
- Template.stop = function()
- {
- cancelAnimationFrame(Template.animationFrame);
- }
- Template.random = function(a,b)
- {
- return Math.random()*(b-a)+a;
- }
- function Transition(start,end,period,loops,func)
- {
- this.start = start;
- this.end = end;
- this.period = period;
- if(!func)this.func = function(a){return a;};
- else this.func = func;
- this.pos = 0;
- this.reversed;
- this.loop = loops;
- this.res = 0;
- this.calc=function()
- {
- if(this.reversed) this.pos -= Template.time/this.period;
- else this.pos += Template.time/this.period;
- if(this.loop===0)
- {
- this.pos = (this.pos+1)%1;
- }
- else
- {
- this.pos = Math.min(this.loop,Math.max(this.pos,0));
- }
- this.res = this.func(this.pos)*(end-start)+start;
- }
- this.get = function()
- {
- return this.res;
- }
- this.reverse = function()
- {
- this.reversed = !this.reversed;
- }
- }
- </script>
- <!--Game code-->
- <script>
- function distance(a,b)
- {
- return Math.sqrt(Math.pow(a.x-b.x,2)+Math.pow(a.y-b.y,2));
- }
- var string,oldString;
- var width,height;
- var keyPoints = new Array();
- var strLength =100,center={x:0,y:0},corner;
- function Draw(canvas)
- {
- //canvas.clearRect(0,0,width,height);
- canvas.lineWidth=1;
- for(var a=0; a<strLength; a++)
- {
- canvas.beginPath();
- canvas.lineTo(string[a].x,string[a].y);
- canvas.lineTo(string[(a+1)%strLength].x,string[(a+1)%strLength].y);
- canvas.lineTo(oldString[(a+1)%strLength].x,oldString[(a+1)%strLength].y);
- canvas.lineTo(oldString[a].x,oldString[a].y);
- var h = distance(string[a],oldString[a]);
- canvas.fillStyle=canvas.strokeStyle="hsla("+h*50+",100%,"+Math.abs(h*3)+"%,0.50)";
- canvas.fill();canvas.stroke();
- }
- /*canvas.beginPath()
- canvas.moveTo(center.x+5,center.y);
- canvas.arc(center.x,center.y,5,0,2*Math.PI);
- canvas.stroke();
- canvas.beginPath();
- canvas.moveTo(corner.TL.x,corner.TL.y);
- canvas.lineTo(corner.TR.x,corner.TR.y);
- canvas.lineTo(corner.BR.x,corner.BR.y);
- canvas.lineTo(corner.BL.x,corner.BL.y);
- canvas.closePath();
- canvas.stroke();*/
- }
- function Calc(time)
- {
- center={x:0,y:0}
- for(var a=0; a<strLength; a++)
- {
- if(!(oldString[a].x==string[a].x&&oldString[a].y==string[a].y))
- {
- oldString[a] = {x:string[a].x,y:string[a].y};
- }
- center.x+=string[a].x;
- center.y+=string[a].y;
- }
- center.x/=strLength;
- center.y/=strLength;
- corner={TL:{x:center.x,y:center.y},TR:{x:center.x,y:center.y},BL:{x:center.x,y:center.y},BR:{x:center.x,y:center.y}};
- for(var a=0; a<strLength; a++)
- {
- corner.TL = {x:Math.min(string[a].x,corner.TL.x),y:Math.min(string[a].y,corner.TL.y)};
- corner.TR = {x:Math.max(string[a].x,corner.TR.x),y:Math.min(string[a].y,corner.TR.y)};
- corner.BR = {x:Math.max(string[a].x,corner.BR.x),y:Math.max(string[a].y,corner.BR.y)};
- corner.BL = {x:Math.min(string[a].x,corner.BL.x),y:Math.max(string[a].y,corner.BL.y)};
- }
- for(var b=0; b<time/10&&b<1000; b++)
- {
- for(var a=0; a<10; a++)
- {
- var index = parseInt(a*(strLength)/10)+1;
- string[index].x =(keyPoints[a].x+string[index].x*5)/6;
- string[index].y = (keyPoints[a].y+string[index].y*5)/6;
- }
- for(var c=0; c<1; c++)
- for(var a=0; a<strLength; a++)
- {
- var beforeIndex = ((a-1)%strLength+strLength)%strLength;
- var afterIndex = (a+1)%strLength;
- string[(a)%strLength].x = (string[beforeIndex].x+string[afterIndex].x)/2;
- string[(a)%strLength].y = (string[beforeIndex].y+string[afterIndex].y)/2;
- }
- }
- }
- function Init()
- {
- Template.start(Draw,Calc,cvs.getContext("2d"));
- width = cvs.width = window.innerWidth;
- height = cvs.height = window.innerHeight;
- for(var a=0; a<10; a++)
- {
- keyPoints[a] = {x:width/2*(Math.sin(a/5*Math.PI)+1),y:height/2*(Math.cos(a/5*Math.PI)+1)};
- }
- string = new Array(strLength);
- oldString = new Array(strLength);
- for(var a=0; a<strLength; a++)
- {
- string[a] ={x:width/2,y:height/2};
- oldString[a] = {x:width/2,y:height/2};
- }
- }
- function cvsMouse(event)
- {
- //addKeyPoint(event)
- }
- function addKeyPoint(event)
- {
- keyPoints.push({x:event.offsetX,y:event.offsetY});
- if(keyPoints.length>11)keyPoints.splice(0,1);
- }
- function randomPos()
- {
- keyPoints.push({x:parseInt(Math.random()*cvs.width),y:parseInt(Math.random()*cvs.height)});
- if(keyPoints.length>11)keyPoints.splice(0,1);
- }
- </script>
- <canvas id="cvs" width="700" height="600"></canvas>
- <script>
- Init();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement