1.   阿里云服务器1888元代金券领取
  2.   本网站所有源码包解压密码:www.youhutong.com
登录  帮助问答

基于canvas实现的表白html网页模板

小川 特效组件
郑重声明:
  本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,否则后果请用户自负,下载后请24小时内删除。 本站所有内容均不能保证其完整性,不提供任何技术支持,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用。
查看演示   下载源码  
【温馨提示】源码包解压密码:www.youhutong.com

效果图:

001.gif

描述说明:

代码实现说明

1.创建画布与图片

// 创建画布
var ctx = canvas.getContext('2d');
// 创建图片对象
var heartImage = new Image();

2.创建爱心对象

type参数为0就显示随机文字,否则只显示爱心不显示文字

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
    }
}

3.通过爱心绘制方法在画布上绘制爱心图片的位置

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();
}
// 爱心变动
Heart.prototype.update = function()

4.使用定时器实时绘制爱心

function render(){
    ctx.clearRect(0, 0, wW, wH);
    for(var i = 0; i < hearts.length; i++){
        hearts[i].draw();
        hearts[i].update();
    }
    requestAnimationFrame(render);
}


 浏览器启用弹出窗口过滤功能,将无法跳转到下载页。在浏览器地址栏右边符号提示处点击允许就可以了!
查看演示   下载源码  
【温馨提示】源码包解压密码:www.youhutong.com

转载请注明来源地址:小川编程 » https://www.youhutong.com/index.php/article/index/471.html


  1、本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,下载后请24小时内删除。
  2、本站所有内容均不能保证其完整性,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用
  3、本站联系方式Email:admin@youhutong.com ,收到邮件会第一时间处理。
  4、如侵犯到任何版权问题,请立即告知本站(立即在线告知),本站将及时删除并致以最深的歉意
( 0 )个小伙伴在吐槽
    登录帐号  如果已经登录请刷新! 发表我的评论
    表情