当前位置:   article > 正文

送给女朋友的跨年代码,来了_新年给女朋友惊喜的前端代码

新年给女朋友惊喜的前端代码

效果展示

 素材图片

 

 

 代码

  1. <!DOCTYPE >
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="utf-8">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  5. <style>
  6. body{margin:0;padding:0;overflow: hidden;}
  7. .city{width:100%;position:fixed;bottom: 0px;z-index: 100;}
  8. .city img{width: 100%;}
  9. </style>
  10. <title>新年快乐</title>
  11. </head>
  12. <body onselectstart = "return false">
  13. <div style="height:700px;overflow:hidden;">
  14. <canvas id='cas' style="background-color:rgba(0,5,24,1);">浏览器不支持canvas</canvas>
  15. <div class="city"><img src="image/1.png" alt="" /></div>
  16. <img src="image/2.png" alt="" id="moon" style="visibility: hidden;"/>
  17. <div style="display:none">
  18. <div class="shape">Happy</div>//此处几个单词可以替换为其他文字
  19. <div class="shape">Lucky</div>
  20. <div class="shape">Surprise</div>
  21. <div class="shape">Blessing</div>
  22. <div class="shape">Miss</div>
  23. </div>
  24. </div>
  25. <script>
  26. var canvas = document.getElementById("cas");
  27. var ocas = document.createElement("canvas");
  28. var octx = ocas.getContext("2d");
  29. var ctx = canvas.getContext("2d");
  30. ocas.width = canvas.width = window.innerWidth;
  31. ocas.height = canvas.height = 700;
  32. var bigbooms = [];
  33. window.onload = function(){
  34. initAnimate()
  35. }
  36. function initAnimate(){
  37. drawBg();
  38. lastTime = new Date();
  39. animate();
  40. }
  41. var lastTime;
  42. function animate(){
  43. ctx.save();
  44. ctx.fillStyle = "rgba(0,5,24,0.1)";
  45. ctx.fillRect(0,0,canvas.width,canvas.height);
  46. ctx.restore();
  47. var newTime = new Date();
  48. if(newTime-lastTime>500+(window.innerHeight-767)/2){
  49. var random = Math.random()*100>2?true:false;
  50. var x = getRandom(canvas.width/5 , canvas.width*4/5);
  51. var y = getRandom(50 , 200);
  52. if(random){
  53. var bigboom = new Boom(getRandom(canvas.width/3,canvas.width*2/3) ,2,"#FFF" , {x:x , y:y});
  54. bigbooms.push(bigboom)
  55. }
  56. else {
  57. var bigboom = new Boom(getRandom(canvas.width/3,canvas.width*2/3) ,2,"#FFF" , {x:canvas.width/2 , y:200} , document.querySelectorAll(".shape")[parseInt(getRandom(0, document.querySelectorAll(".shape").length))]);
  58. bigbooms.push(bigboom)
  59. }
  60. lastTime = newTime;
  61. console.log(bigbooms)
  62. }
  63. stars.foreach(function(){
  64. this.paint();
  65. })
  66. drawMoon();
  67. bigbooms.foreach(function(index){
  68. var that = this;
  69. if(!this.dead){
  70. this._move();
  71. this._drawLight();
  72. }
  73. else{
  74. this.booms.foreach(function(index){
  75. if(!this.dead) {
  76. this.moveTo(index);
  77. }
  78. else if(index === that.booms.length-1){
  79. bigbooms[bigbooms.indexOf(that)] = null;
  80. }
  81. })
  82. }
  83. });
  84. raf(animate);
  85. }
  86. function drawMoon(){
  87. var moon = document.getElementById("moon");
  88. var centerX = canvas.width-200 , centerY = 100 , width = 80;
  89. if(moon.complete){
  90. ctx.drawImage(moon , centerX , centerY , width , width )
  91. }
  92. else {
  93. moon.onload = function(){
  94. ctx.drawImage(moon ,centerX , centerY , width , width)
  95. }
  96. }
  97. var index = 0;
  98. for(var i=0;i<10;i++){
  99. ctx.save();
  100. ctx.beginPath();
  101. ctx.arc(centerX+width/2 , centerY+width/2 , width/2+index , 0 , 2*Math.PI);
  102. ctx.fillStyle="rgba(240,219,120,0.005)";
  103. index+=2;
  104. ctx.fill();
  105. ctx.restore();
  106. }
  107. }
  108. Array.prototype.foreach = function(callback){
  109. for(var i=0;i<this.length;i++){
  110. if(this[i]!==null) callback.apply(this[i] , [i])
  111. }
  112. }
  113. var raf = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000 / 60); };
  114. canvas.onclick = function(){
  115. var x = event.clientX;
  116. var y = event.clientY;
  117. var bigboom = new Boom(getRandom(canvas.width/3,canvas.width*2/3) ,2,"#FFF" , {x:x , y:y});
  118. bigbooms.push(bigboom)
  119. }
  120. // canvas.addEventLisener("touchstart" , function(event){
  121. // var touch = event.targetTouches[0];
  122. // var x = event.pageX;
  123. // var y = event.pageY;
  124. // var bigboom = new Boom(getRandom(canvas.width/3,canvas.width*2/3) ,2,"#FFF" , {x:x , y:y});
  125. // bigbooms.push(bigboom)
  126. // })
  127. var Boom = function(x,r,c,boomArea,shape){
  128. this.booms = [];
  129. this.x = x;
  130. this.y = (canvas.height+r);
  131. this.r = r;
  132. this.c = c;
  133. this.shape = shape || false;
  134. this.boomArea = boomArea;
  135. this.theta = 0;
  136. this.dead = false;
  137. this.ba = parseInt(getRandom(80 , 200));
  138. }
  139. Boom.prototype = {
  140. _paint:function(){
  141. ctx.save();
  142. ctx.beginPath();
  143. ctx.arc(this.x,this.y,this.r,0,2*Math.PI);
  144. ctx.fillStyle = this.c;
  145. ctx.fill();
  146. ctx.restore();
  147. },
  148. _move:function(){
  149. var dx = this.boomArea.x - this.x , dy = this.boomArea.y - this.y;
  150. this.x = this.x+dx*0.01;
  151. this.y = this.y+dy*0.01;
  152. if(Math.abs(dx)<=this.ba && Math.abs(dy)<=this.ba){
  153. if(this.shape){
  154. this._shapBoom();
  155. }
  156. else this._boom();
  157. this.dead = true;
  158. }
  159. else {
  160. this._paint();
  161. }
  162. },
  163. _drawLight:function(){
  164. ctx.save();
  165. ctx.fillStyle = "rgba(255,228,150,0.3)";
  166. ctx.beginPath();
  167. ctx.arc(this.x , this.y , this.r+3*Math.random()+1 , 0 , 2*Math.PI);
  168. ctx.fill();
  169. ctx.restore();
  170. },
  171. _boom:function(){
  172. var fragNum = getRandom(30 , 200);
  173. var style = getRandom(0,10)>=5? 1 : 2;
  174. var color;
  175. if(style===1){
  176. color = {
  177. a:parseInt(getRandom(128,255)),
  178. b:parseInt(getRandom(128,255)),
  179. c:parseInt(getRandom(128,255))
  180. }
  181. }
  182. var fanwei = parseInt(getRandom(300, 400));
  183. for(var i=0;i<fragNum;i++){
  184. if(style===2){
  185. color = {
  186. a:parseInt(getRandom(128,255)),
  187. b:parseInt(getRandom(128,255)),
  188. c:parseInt(getRandom(128,255))
  189. }
  190. }
  191. var a = getRandom(-Math.PI, Math.PI);
  192. var x = getRandom(0, fanwei) * Math.cos(a) + this.x;
  193. var y = getRandom(0, fanwei) * Math.sin(a) + this.y;
  194. var radius = getRandom(0 , 2)
  195. var frag = new Frag(this.x , this.y , radius , color , x , y );
  196. this.booms.push(frag);
  197. }
  198. },
  199. _shapBoom:function(){
  200. var that = this;
  201. putValue(ocas , octx , this.shape , 5, function(dots){
  202. var dx = canvas.width/2-that.x;
  203. var dy = canvas.height/2-that.y;
  204. for(var i=0;i<dots.length;i++){
  205. color = {a:dots[i].a,b:dots[i].b,c:dots[i].c}
  206. var x = dots[i].x;
  207. var y = dots[i].y;
  208. var radius = 1;
  209. var frag = new Frag(that.x , that.y , radius , color , x-dx , y-dy);
  210. that.booms.push(frag);
  211. }
  212. })
  213. }
  214. }
  215. function putValue(canvas , context , ele , dr , callback){
  216. context.clearRect(0,0,canvas.width,canvas.height);
  217. var img = new Image();
  218. if(ele.innerHTML.indexOf("img")>=0){
  219. img.src = ele.getElementsByTagName("img")[0].src;
  220. imgload(img , function(){
  221. context.drawImage(img , canvas.width/2 - img.width/2 , canvas.height/2 - img.width/2);
  222. dots = getimgData(canvas , context , dr);
  223. callback(dots);
  224. })
  225. }
  226. else {
  227. var text = ele.innerHTML;
  228. context.save();
  229. var fontSize =200;
  230. context.font = fontSize+"px 宋体 bold";
  231. context.textAlign = "center";
  232. context.textBaseline = "middle";
  233. context.fillStyle = "rgba("+parseInt(getRandom(128,255))+","+parseInt(getRandom(128,255))+","+parseInt(getRandom(128,255))+" , 1)";
  234. context.fillText(text , canvas.width/2 , canvas.height/2);
  235. context.restore();
  236. dots = getimgData(canvas , context , dr);
  237. callback(dots);
  238. }
  239. }
  240. function imgload(img , callback){
  241. if(img.complete){
  242. callback.call(img);
  243. }
  244. else {
  245. img.onload = function(){
  246. callback.call(this);
  247. }
  248. }
  249. }
  250. function getimgData(canvas , context , dr){
  251. var imgData = context.getImageData(0,0,canvas.width , canvas.height);
  252. context.clearRect(0,0,canvas.width , canvas.height);
  253. var dots = [];
  254. for(var x=0;x<imgData.width;x+=dr){
  255. for(var y=0;y<imgData.height;y+=dr){
  256. var i = (y*imgData.width + x)*4;
  257. if(imgData.data[i+3] > 128){
  258. var dot = {x:x , y:y , a:imgData.data[i] , b:imgData.data[i+1] , c:imgData.data[i+2]};
  259. dots.push(dot);
  260. }
  261. }
  262. }
  263. return dots;
  264. }
  265. function getRandom(a , b){
  266. return Math.random()*(b-a)+a;
  267. }
  268. var maxRadius = 1 , stars=[];
  269. function drawBg(){
  270. for(var i=0;i<100;i++){
  271. var r = Math.random()*maxRadius;
  272. var x = Math.random()*canvas.width;
  273. var y = Math.random()*2*canvas.height - canvas.height;
  274. var star = new Star(x , y , r);
  275. stars.push(star);
  276. star.paint()
  277. }
  278. }
  279. var Star = function(x,y,r){
  280. this.x = x;this.y=y;this.r=r;
  281. }
  282. Star.prototype = {
  283. paint:function(){
  284. ctx.save();
  285. ctx.beginPath();
  286. ctx.arc(this.x , this.y , this.r , 0 , 2*Math.PI);
  287. ctx.fillStyle = "rgba(255,255,255,"+this.r+")";
  288. ctx.fill();
  289. ctx.restore();
  290. }
  291. }
  292. var focallength = 250;
  293. var Frag = function(centerX , centerY , radius , color ,tx , ty){
  294. this.tx = tx;
  295. this.ty = ty;
  296. this.x = centerX;
  297. this.y = centerY;
  298. this.dead = false;
  299. this.centerX = centerX;
  300. this.centerY = centerY;
  301. this.radius = radius;
  302. this.color = color;
  303. }
  304. Frag.prototype = {
  305. paint:function(){
  306. ctx.save();
  307. ctx.beginPath();
  308. ctx.arc(this.x , this.y , this.radius , 0 , 2*Math.PI);
  309. ctx.fillStyle = "rgba("+this.color.a+","+this.color.b+","+this.color.c+",1)";
  310. ctx.fill()
  311. ctx.restore();
  312. },
  313. moveTo:function(index){
  314. this.ty = this.ty+0.3;
  315. var dx = this.tx - this.x , dy = this.ty - this.y;
  316. this.x = Math.abs(dx)<0.1 ? this.tx : (this.x+dx*0.1);
  317. this.y = Math.abs(dy)<0.1 ? this.ty : (this.y+dy*0.1);
  318. if(dx===0 && Math.abs(dy)<=80){
  319. this.dead = true;
  320. }
  321. this.paint();
  322. }
  323. }
  324. </script>
  325. </body>
  326. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/85127
推荐阅读
相关标签
  

闽ICP备14008679号