当前位置:   article > 正文

JS特效第158弹:情人节爱心气球告白动画特效_js表白特效

js表白特效

         html5基于canvas绘制爱心气球挂着文字缓缓上升,情人节气球告白背景动画特效,先来看看效果:

        一部分关键的代码如下:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>情人节快乐:)</title>
  6. <style>
  7. canvas {
  8. position: absolute;
  9. top: 0;
  10. left: 0;
  11. }</style>
  12. </head>
  13. <body>
  14. <canvas id=c></canvas>
  15. <script>
  16. var w = c.width = window.innerWidth,
  17. h = c.height = window.innerHeight,
  18. ctx = c.getContext( '2d' ),
  19. opts = {
  20. phrases: [ "我爱你", "你是最棒的!", "你真可爱!", "愿长长久久", "希望你每天开心快乐!", "我爱你的心是直到世界末日也不变", "我不需要分清东西南北,反正我会走向有你的那边", "想想和我一同看日升日落的喜悦吧!", "每天我的动力就是见到你,并和你说说话", "世界一般,但你超值" ],
  21. balloons: 10,
  22. baseVelY: -1,
  23. addedVelY: -1,
  24. baseVelX: -.25,
  25. addedVelX: .5,
  26. baseSize: 20,
  27. addedSize: 10,
  28. baseSizeAdder: 2,
  29. addedSizeAdder: 2,
  30. baseIncrementer: .01,
  31. addedIncrementer: .03,
  32. baseHue: -10,
  33. addedHue: 30,
  34. font: '15px Verdana'
  35. },
  36. cycle = 0,
  37. balloons = [];
  38. ctx.font = opts.font;
  39. function Balloon(){
  40. this.reset();
  41. }
  42. Balloon.prototype.reset = function(){
  43. this.size = opts.baseSize + opts.addedSize * Math.random();
  44. this.sizeAdder = opts.baseSizeAdder + opts.addedSizeAdder * Math.random();
  45. this.incrementer = opts.baseIncrementer + opts.addedIncrementer * Math.random();
  46. this.tick = 0;
  47. this.x = Math.random() * w;
  48. this.y = h + this.size;
  49. this.vx = opts.baseVelX + opts.addedVelX * Math.random();
  50. this.vy = opts.baseVelY + opts.addedVelY * Math.random();
  51. this.color = 'hsla(hue,70%,60%,.8)'.replace( 'hue', opts.baseHue + opts.addedHue * Math.random() );
  52. this.phrase = opts.phrases[ ++cycle % opts.phrases.length ].split( '\n' );
  53. this.lengths = [];
  54. for( var i = 0; i < this.phrase.length; ++i )
  55. this.lengths.push( -ctx.measureText( this.phrase[ i ] ).width / 2 );
  56. }
  57. Balloon.prototype.step = function(){
  58. this.tick += this.incrementer;
  59. this.x += this.vx;
  60. this.y += this.vy;
  61. var size = this.size + this.sizeAdder * Math.sin( this.tick );
  62. ctx.lineWidth = size / 40;
  63. ctx.strokeStyle = '#eee';
  64. ctx.beginPath();
  65. ctx.moveTo( this.x, this.y - 2 );
  66. ctx.lineTo( this.x, this.y + size );
  67. ctx.stroke();
  68. ctx.fillStyle = this.color;
  69. ctx.translate( this.x, this.y );
  70. ctx.rotate( Math.PI / 4 );
  71. //ctx.fillRect( -size / 2, -size / 2, size / 2, size / 2 );
  72. ctx.beginPath();
  73. ctx.moveTo( 0, 0 );
  74. ctx.arc( -size / 2, -size / 2 + size / 4, size / 4, Math.PI / 2, Math.PI * 3 / 2 );
  75. ctx.arc( -size / 2 + size / 4, -size / 2, size / 4, Math.PI, Math.PI * 2 );
  76. ctx.lineTo( 0, 0 );
  77. ctx.fill();
  78. ctx.rotate( -Math.PI / 4 );
  79. ctx.translate( -this.x, -this.y );
  80. ctx.translate( this.x, this.y + size + 15 );
  81. ctx.scale( size / this.size, size / this.size );
  82. ctx.fillStyle = '#eee';
  83. for( var i = 0; i < this.phrase.length; ++i )
  84. ctx.fillText( this.phrase[ i ], this.lengths[ i ], i * 15 );
  85. ctx.scale( this.size / size, this.size / size );
  86. ctx.translate( -this.x, -( this.y + size + 15 ) );
  87. if( this.y < -size * 3 )
  88. this.reset();
  89. }
  90. function anim(){
  91. window.requestAnimationFrame( anim );
  92. ctx.fillStyle = '#222';
  93. ctx.fillRect( 0, 0, w, h );
  94. if( balloons.length < opts.balloons && Math.random() < .01 )
  95. balloons.push( new Balloon );
  96. for( var i = 0; i < balloons.length; ++i )
  97. balloons[ i ].step();
  98. }
  99. anim();</script>
  100. </body>
  101. </html>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/870964
推荐阅读
相关标签
  

闽ICP备14008679号