Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <head>
- <title>Tree</title>
- <script type="text/javascript">
- var ctx;
- var time;
- const INTERVAL = 10;
- const SPAN_AVE = 15;
- const HEIGHT = 1200;
- const WIDTH = 1000;
- const MIN = 1;
- const G = 1;
- var poke_map;
- var poke_mama;
- var spot_list = new Array();
- var end = 1;
- var time_cnt = 0;
- var createMap = function(){
- var map = new Array();
- map.getElements = function(){
- return this;
- }
- map.getElementsInSpot = function(spot){
- var elements = new Array();
- for(var idx_ver = 0;idx_ver < this.length; idx_ver++){
- if(getDistance(this[idx_ver],spot) < spot.size){
- elements.push(this[idx_ver]);
- }
- }
- return elements;
- }
- return map;
- }
- var PokeManager = function(map,type){
- this.map = map;
- this.type = type;
- this.createElement = function(){
- var inst = new type(arguments);
- map.push(inst);
- return inst;
- }
- this.type.prototype.replace = function(down,right){
- var boo = true;
- if(this.left + right > -1 && this.top + down < HEIGHT/MIN && this.left + right < WIDTH/MIN){
- this.top = this.top + down;
- this.left = this.left + right;
- } else {
- boo = false;
- }
- return boo;
- }
- this.type.prototype.leave = function(){
- var idx = map.indexOf(this);
- map.splice(idx,1);
- }
- this.type.prototype.clear = function(){
- this.leave();
- for (var ppty in this){
- delete this[ppty];
- ppty = undefined;
- }
- }
- }
- var getDistance = function(a,b){
- var v = a.top - b.top;
- var h = a.left - b.left;
- return Math.floor(Math.sqrt(Math.pow(v,2) + Math.pow(h,2)));
- }
- var getColorDifference = function(a,b){
- var red = Math.abs(a.R - b.R);
- var green = Math.abs(a.G - b.G);
- var blue = Math.abs(a.B - b.B);
- return red + blue + green;
- }
- var Poke = function(args){
- this.top = args[0];
- this.left = args[1];
- this.R = args[2];
- this.G = args[3];
- this.B = args[4];
- this.spd_v = args[5];
- this.spd_h = args[6];
- this.size = args[7];
- this.opacity = 0;
- }
- Poke.prototype.print = function(){
- ctx.fillStyle = "rgba(" + this.R +"," + this.G + "," + this.B + "," + this.opacity + ")";
- ctx.beginPath();
- ctx.arc(this.left*MIN,this.top*MIN,this.size*MIN,0,Math.PI*2.0,true);
- ctx.fill();
- }
- Poke.prototype.move = function(){
- if(!this.replace(this.spd_v,this.spd_h)){
- this.clear();
- }
- }
- Poke.prototype.fall = function(){
- this.spd_v += G;
- this.opacity -= 0.05;
- if(this.opacity < 0){
- this.opacity = 0;
- }
- }
- Poke.prototype.stop = function(stopper){
- if(stopper){
- this.spd_v = parseInt(this.spd_v * stopper);
- this.spd_h = 0;
- } else {
- this.spd_v = parseInt(this.spd_v * 0.93);
- this.spd_h = parseInt(this.spd_h * 0.90);
- }
- this.opacity += 0.07;
- }
- var Spot = function(top,left,size,R,G,B,per,stopper){
- this.top = top;
- this.left = left;
- this.size = size;
- this.R = R;
- this.G = G;
- this.B = B;
- this.per = per;
- this.stopper = stopper;
- }
- Spot.prototype.catchPoke = function(){
- var list = poke_map.getElementsInSpot(this);
- for(var idx = 0;idx < list.length;idx++){
- var poke = list[idx];
- if(getColorDifference(poke,this) < 80){
- if(Math.random() * this.size * 22000 / list.length * this.per * end > getDistance(poke,this)){
- poke.stop(this.stopper);
- }
- }
- }
- }
- Spot.prototype.print = function(){
- ctx.fillStyle = "rgba(" + this.R +"," + this.G + "," + this.B + "," + 0.1 + ")";
- ctx.beginPath();
- ctx.arc(this.left*MIN,this.top*MIN,this.size*MIN,0,Math.PI*2.0,true);
- ctx.fill();
- }
- function start(){
- var canvas = document.getElementById('canvas');
- if (canvas.getContext){
- ctx = canvas.getContext('2d');
- ctx.globalCompositeOperation = "source-over";
- }
- poke_map = createMap();
- poke_mama = new PokeManager(poke_map,Poke);
- var ha = new Array(50,256,50);
- var ki = new Array(170,100,0);
- var mi = new Array(250,50,50);
- var yuki = new Array(230,230,230);
- spot_list.push(new Spot(850,300,50,ha[0],ha[1],ha[2],5));
- spot_list.push(new Spot(850,700,50,ha[0],ha[1],ha[2],5));
- spot_list.push(new Spot(750,200,100,ha[0],ha[1],ha[2],4.5));
- spot_list.push(new Spot(750,400,100,ha[0],ha[1],ha[2],4.5));
- spot_list.push(new Spot(750,600,100,ha[0],ha[1],ha[2],4.5))
- spot_list.push(new Spot(750,800,100,ha[0],ha[1],ha[2],4.5));
- spot_list.push(new Spot(600,300,100,ha[0],ha[1],ha[2],5));
- spot_list.push(new Spot(600,500,100,ha[0],ha[1],ha[2],6));
- spot_list.push(new Spot(600,700,100,ha[0],ha[1],ha[2],5));
- spot_list.push(new Spot(450,400,100,ha[0],ha[1],ha[2],6));
- spot_list.push(new Spot(450,600,100,ha[0],ha[1],ha[2],6));
- spot_list.push(new Spot(300,500,100,ha[0],ha[1],ha[2],20));
- spot_list.push(new Spot(650,500,70,ha[0],ha[1],ha[2],0.001));
- for(var idx = 0;idx < 50;idx++){
- var t = Math.random() * 451;
- var l = Math.random() * 901 - 450;
- if(Math.abs(l) + t < 400 + Math.random() * 120){
- spot_list.push(new Spot(850 - t,500 + l,5,mi[0],mi[1],mi[2],5,0.001));
- }
- }
- spot_list.push(new Spot(700,500,100,ki[0],ki[1],ki[2],0.008));
- spot_list.push(new Spot(800,500,100,ki[0],ki[1],ki[2],0.007));
- spot_list.push(new Spot(900,500,100,ki[0],ki[1],ki[2],0.006));
- spot_list.push(new Spot(1000,500,100,ki[0],ki[1],ki[2],0.005));
- spot_list.push(new Spot(1100,500,100,ki[0],ki[1],ki[2],0.004));
- spot_list.push(new Spot(450,500,700,yuki[0],yuki[1],yuki[2],1,0));
- time = setInterval(exec, INTERVAL);
- }
- function exec(){
- time_cnt++;
- ctx.fillStyle = "rgb(255,255,255)";
- ctx.fillRect(0, 0, WIDTH, HEIGHT);
- if(Math.random() * 200 < 200){
- poke_mama.createElement(
- 600,
- WIDTH/MIN/2,
- Math.floor(Math.random() * 256),
- Math.floor(Math.random() * 256),
- Math.floor(Math.random() * 256),
- Math.floor(Math.random() * -50) - 10,
- Math.floor(Math.random() * 100) - 50,
- Math.floor(Math.random() * 10) + 20
- );
- poke_mama.createElement(
- HEIGHT/MIN -10,
- 10,
- Math.floor(Math.random() * 100 + 30),
- Math.floor(Math.random() * 100 + 120),
- Math.floor(Math.random() * 100 + 30),
- Math.floor(Math.random() * -50) - 10,
- Math.floor(Math.random() * 30),
- Math.floor(Math.random() * 10) + 20
- );
- poke_mama.createElement(
- HEIGHT/MIN -10,
- WIDTH/MIN - 10,
- Math.floor(Math.random() * 100 + 30),
- Math.floor(Math.random() * 100 + 120),
- Math.floor(Math.random() * 100 + 30),
- Math.floor(Math.random() * -50) - 10,
- Math.floor(Math.random() * -30),
- Math.floor(Math.random() * 10) + 20
- );
- poke_mama.createElement(
- HEIGHT/MIN -400,
- 10,
- Math.floor(Math.random() * 100 + 50),
- Math.floor(Math.random() * 100 + 120),
- Math.floor(Math.random() * 100 + 30),
- Math.floor(Math.random() * -50) - 10,
- Math.floor(Math.random() * 30),
- Math.floor(Math.random() * 10) + 20
- );
- poke_mama.createElement(
- HEIGHT/MIN -400,
- WIDTH/MIN - 10,
- Math.floor(Math.random() * 100 + 50),
- Math.floor(Math.random() * 100 + 120),
- Math.floor(Math.random() * 100 + 50),
- Math.floor(Math.random() * -50) - 10,
- Math.floor(Math.random() * -30),
- Math.floor(Math.random() * 10) + 20
- );
- poke_mama.createElement(
- HEIGHT/MIN -600,
- 10,
- Math.floor(Math.random() * 256),
- Math.floor(Math.random() * 256),
- Math.floor(Math.random() * 256),
- Math.floor(Math.random() * -50) - 10,
- Math.floor(Math.random() * 30),
- Math.floor(Math.random() * 10) + 20
- );
- poke_mama.createElement(
- HEIGHT/MIN -600,
- WIDTH/MIN - 10,
- Math.floor(Math.random() * 256),
- Math.floor(Math.random() * 256),
- Math.floor(Math.random() * 256),
- Math.floor(Math.random() * -50) - 10,
- Math.floor(Math.random() * -30),
- Math.floor(Math.random() * 10) + 20
- );
- poke_mama.createElement(
- HEIGHT/MIN -600,
- WIDTH/MIN - 10,
- Math.floor(Math.random() * 150 + 100),
- Math.floor(Math.random() * 50 + 100),
- Math.floor(Math.random() * 30),
- Math.floor(Math.random() * -50) - 10,
- Math.floor(Math.random() * 31) - 15,
- Math.floor(Math.random() * 10) + 20
- );
- poke_mama.createElement(
- 0,
- WIDTH/MIN/2,
- Math.floor(Math.random() * 150 + 100),
- Math.floor(Math.random() * 100 + 150),
- Math.floor(Math.random() * 150 + 100),
- Math.floor(Math.random() * -50) - 10,
- Math.floor(Math.random() * 301) - 150,
- Math.floor(Math.random() * 10) + 20
- );
- }
- for(var idx = 0;idx < poke_map.length;idx++){
- poke_map[idx].move();
- poke_map[idx].print();
- poke_map[idx].fall();
- }
- for(var idx = 0;idx < spot_list.length;idx++){
- spot_list[idx].catchPoke();
- //spot_list[idx].print();
- }
- }
- </script>
- <style type="text/css">
- canvas { background-color:#fff; border: 10px solid #000; }
- </style>
- </head>
- <body onload="start();">
- <canvas id="canvas" width="1000" height="1200"></canvas>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement