一年一度的情人节即将到来!
你想好怎么跟心仪的TA表白了吗?
想送女孩子礼物不知道送啥?
怕她不喜欢?
如果你掌握一些编程的技能,
那就可以不按照常规出牌,
给Ta来个小惊喜啦!
❤️ 1.爱心表白动画特效
js爱心气泡表白墙特效代码,
js+css3实现的程序员爱心表白网页动画,
带恋爱时间记录满屏的粉色爱心漂浮
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>爱心</title> <style> *{margin:0; padding:0;} body{ background-color: #1E1E1E; } </style> </head> <body> <canvas id="drawHeart"></canvas> <script> var hearts = []; var canvas = document.getElementById('drawHeart'); var wW = window.innerWidth; var wH = window.innerHeight; // 创建画布 var ctx = canvas.getContext('2d'); // 创建图片对象 var heartImage = new Image(); heartImage.src = 'img/heart.svg'; var num = 100; init(); window.addEventListener('resize', function(){ wW = window.innerWidth; wH = window.innerHeight; }) function getText(){ var val = Math.random() * 10; if(val > 1 && val <= 3){ return '爱你一辈子'; } else if(val > 3 && val <= 5){ return '感谢你'; } else if(val > 5 && val <= 8){ return '喜欢你'; } else{ return 'I Love You'; } } function Heart(type){ this.type = type; // 初始化生成范围 this.x = Math.random() * wW; this.y = Math.random() * wH; this.opacity = Math.random() * .5 + .5; // 偏移量 this.vel = { x: (Math.random() - .5) * 5, y: (Math.random() - .5) * 5 } this.initialW = wW * .5; this.initialH = wH * .5; // 缩放比例 this.targetScale = Math.random() * .15 + .02; // 最小0.02 this.scale = Math.random() * this.targetScale; // 文字位置 this.fx = Math.random() * wW; this.fy = Math.random() * wH; this.fs = Math.random() * 10; this.text = getText(); this.fvel = { x: (Math.random() - .5) * 5, y: (Math.random() - .5) * 5, f: (Math.random() - .5) * 2 } } Heart.prototype.draw = function(){ ctx.save(); ctx.globalAlpha = this.opacity; ctx.drawImage(heartImage, this.x, this.y, this.width, this.height); ctx.scale(this.scale + 1, this.scale + 1); if(!this.type){ // 设置文字属性 ctx.fillStyle = getColor(); ctx.font = 'italic ' + this.fs + 'px sans-serif'; // 填充字符串 ctx.fillText(this.text, this.fx, this.fy); } ctx.restore(); } function render(){ ctx.clearRect(0, 0, wW, wH); for(var i = 0; i < hearts.length; i++){ hearts[i].draw(); hearts[i].update(); } requestAnimationFrame(render); } </script> </body> </html>
❤️ 2.python画动态爱心表白
一行代码画心
import time
words = input(‘请输出想要表达的文字:’)
#例子:words = “Dear lili, Happy Valentine’s Day! Lyon Will Always Love You Till The End! ♥ Forever! ♥”
for item in words.split():
#要想实现打印出字符间的空格效果,此处添加:item = item+’ ’
letterlist = []#letterlist是所有打印字符的总list,里面包含y条子列表list_X
for y in range(12, -12, -1):
list_X = []#list_X是X轴上的打印字符列表,里面装着一个String类的letters
letters = ‘’#letters即为list_X内的字符串,实际是本行要打印的所有字符
for x in range(-30, 30):#是乘法,**是幂次方
expression = ((x0.05)**2+(y0.1)**2-1)**3-(x0.05)*2(y*0.1)**3
if expression <= 0:
letters += item[(x-y) % len(item)]
else:
letters += ’ ’
list_X.append(letters)
letterlist += list_X
print(’\n’.join(letterlist))
time.sleep(1.5);
❤️ 3.3D玫瑰花效果
javascript+HTML5的canvas实现情人节3D玫瑰花效果代码,
使用了html5的canvas技术,可呈现出带有3D效果的玫瑰花渐显效果,非常逼真自然
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>3D玫瑰花</title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> </head> <body> 情人节快到了,这里送大家一枝玫瑰,无论是有对象还是没对象的朋友们,情人节快乐~ 下面的玫瑰绘制用到了HTML 5的canvas,所以你的浏览器需要支持HTML 5。个人还是比较推荐chrome,这个效果在Firefox下也会稍卡。 <div id="demo" style="width:520; height:500px;"><canvas id="c" height="500" width="500"></canvas></div> <script> var b = document.body; var c = document.getElementsByTagName('canvas')[0]; var a = c.getContext('2d'); var canvas = document.getElementsByTagName('canvas')[0]; var ctx = canvas.getContext('2d'); document.body.clientWidth; with(m=Math)C=cos,S=sin,P=pow,R=random; c.width=c.height=f=500;h=-250; function p(a,b,c){ if(c>60) return[S(a*7)*(13+5/(.2+P(b*4,4)))-S(b)*50,b*f+50,625+C(a*7)*(13+5/(.2+P(b*4,4)))+b*400,a*1-b/2,a]; A=a*2-1; B=b*2-1; if(A*A+B*B<1) { if(c>37) { n=(j=c&1)?6:4;o=.5/(a+.01)+C(b*125)*3-a*300; w=b*h; return[o*C(n)+w*S(n)+j*610-390,o*S(n)-w*C(n)+550-j*350,1180+C(B+A)*99-j*300,.4-a*.1+P(1-B*B,-h*6)*.15-a*b*.4+C(a+b)/5+P(C((o*(a+1)+(B>0?w:-w))/25),30)*.1*(1-B*B),o/1e3+.7-o*w*3e-6] } if(c>32) { c=c*1.16-.15;o=a*45-20;w=b*b*h;z=o*S(c)+w*C(c)+620; return[o*C(c)-w*S(c),28+C(B*.5)*99-b*b*b*60-z/2-h,z,(b*b*.3+P((1-(A*A)),7)*.15+.3)*b,b*.7] } o=A*(2-b)*(80-c*2); w=99-C(A)*120-C(b)*(-h-c*4.9)+C(P(1-b,7))*50+c*2;z=o*S(c)+w*C(c)+700; return[o*C(c)-w*S(c),B*99-C(P(b, 7))*50-c/3-z/1.35+450,z,(1-b/1.2)*.9+a*.1, P((1-b),20)/4+.05] } } var draw = setInterval('for(i=0;i<1e4;i++)if(s=p(R(),R(),i%46/.74)){z=s[2];x=~~(s[0]*f/z-h);y=~~(s[1]*f/z-h);if(!m[q=y*f+x]|m[q]>z)m[q]=z,a.fillStyle="rgb("+~(s[3]*h)+","+~(s[4]*h)+","+~(s[3]*s[3]*-80)+")",a.fillRect(x,y,1,1)}',0); var demo = document.getElementById('demo'); function redraw(){ /* var d_c = document.createElement("canvas"); d_c.setAttribute("id","c"); d_c.setAttribute("width","520"); d_c.setAttribute("height","500"); demo.appendChild(d_c); */ draw = setInterval('for(i=0;i<1e4;i++)if(s=p(R(),R(),i%46/.74)){z=s[2];x=~~(s[0]*f/z-h);y=~~(s[1]*f/z-h);if(!m[q=y*f+x]|m[q]>z)m[q]=z,a.fillStyle="rgb("+~(s[3]*h)+","+~(s[4]*h)+","+~(s[3]*s[3]*-80)+")",a.fillRect(x,y,1,1)}',0); //alert(d_c); } function clear_canvas() { ctx.clearRect(0,0,520,500); //canvas.parentNode.removeChild(canvas); //删除 } function stop_draw(obj){ clearInterval(obj); } </script> </body> </html>