html5 canvas拖动的流体图片动画效果

html5 canvas拖动的流体图片动画效果

添加时间:2021-02-23 02:12:55
站长推荐丨赞助论坛,可获取海量资源终身免费下载权限奥!
举报 举报
收藏
预览
附件 附件
  • 模板类型模板类型:图形
  • 模板颜色模板颜色:初级
  • 下载积分下载积分:28 米粒
  • 下载权限下载权限:

    赞助会员

一款html5 canvas拖动的流体图片动画效果,可以将指定的图片处理绘制在canvas上,并有鼠标拖动会变形的流体效果,整体效果还时比较酷炫的,喜欢的童鞋请收下吧。
html5 canvas拖动的流体图片动画效果
分类:html/css > 图形 难易:初级

页面的head部分,简单设置一下页面样式即可,代码如下:

<style type="text/css">
html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
}
canvas {
    cursor: none;
}
</style>

页面的body部分,仅需要设置一个canvas画布即可,代码如下:

<canvas id="canvas"></canvas>

页面的底部,将整个canvas特效封装成了一个类,代码比较多,这里仅贴出部分代码:

constructor() {
        // setup a canvas
        this.canvas = document.getElementById('canvas');
        this.dpr = DPR;
        this.ctx = this.canvas.getContext('2d');
        this.ctx.scale(this.dpr, this.dpr);
        
        // method binds
        this.render = this.render.bind(this);
        this.handleDrop = this.handleDrop.bind(this);
        this.handleDragOver = this.handleDragOver.bind(this);
        this.handleDragLeave = this.handleDragLeave.bind(this);
        this.handleMouse = this.handleMouse.bind(this);
        this.handleResize = this.handleResize.bind(this);
        this.handleMousedown = this.handleMousedown.bind(this);
        this.handleMouseup = this.handleMouseup.bind(this);
        
        // state
        this.isDragging = false;
        this.hasLoaded = false;
        this.tick = 0;
        this.mouse = {
            x: window.innerWidth * this.dpr / 2,
            y: window.innerHeight * this.dpr / 2,
            mousedown: false,
        };
        
        // setup
        this.setupListeners();
        this.addInitialImage();
        this.setCanvasSize();
        this.render();
    }
    
    setupListeners() {
        window.addEventListener('resize', this.handleResize);
        this.canvas.addEventListener('drop', this.handleDrop, false);
        this.canvas.addEventListener('dragover', this.handleDragOver, false);
        this.canvas.addEventListener('dragleave', this.handleDragLeave, false);
        window.addEventListener('mousemove', this.handleMouse);
        window.addEventListener('mousedown', this.handleMousedown);
        window.addEventListener('mouseup', this.handleMouseup);
    }
    
    handleResize() {
        this.setCanvasSize();
    }
    
    handleMousedown(event) {
        this.mouse.mousedown = true;
    }
    
    handleMouseup(event) {
        this.mouse.mousedown = false;
    }
    
    handleDrop(event) {
        event.preventDefault();   
        
        const image = event.dataTransfer.getData('text/plain')
        this.isDragging = false;
        this.hasLoaded = false;
        
        const file = event.dataTransfer.files[0];
        
        this.handleImageFile(file);
    }

    handleImageFile(file) {        
        // check for image, if not return
        const imageType = /image.*/;
        if (!file.type.match(imageType)) return;
        
        // create image from file
        this.image = new FluidImage({
            file,
        }, SIZE);
        
        this.image.hasLoaded().then(() => {
            console.log('successfully loaded!');
            this.handleLoad();
        }).catch((e) => {
            console.log('>:-| failed:', e);
        });
    }
    
    handleLoad() {
        this.hasLoaded = true;
        this.centerImageToWindow();
        
        if (!this.demo) {
            this.demoForce();            
        }
    }
    
    handleDragOver(event) {
        event.preventDefault();
        this.isDragging = true;
    }
    
    handleDragLeave(event) {
        event.preventDefault();
        this.isDragging = false;
    }
    
    handleMouse(event) {
        const x = event.clientX * this.dpr;
        const y = event.clientY * this.dpr;
        this.mouse.x = x;
        this.mouse.y = y;
        
        this.applyForce();
    }
    
    addInitialImage() {
        // create image from file
        this.image = new FluidImage({
            src: DEMO_IMAGE,
        }, SIZE);
        
        this.image.hasLoaded().then(() => {
            this.handleLoad();
        }).catch((e) => {
            console.log('>:-| failed:', e);
        });
    }
    
    centerImageToWindow() {
        const { width: w, height: h } = this.image.canvas;
        const { width: cw, height: ch } = this.canvas;
        const tx = cw / 2 - w / 2;
        const ty = ch / 2 - h / 2;
        
        // centers points on this main canvas
        this.image.points.map(p => {
            const x = p.x + tx;
            const y = p.y + ty;
            p.setOrigin(x, y);
            return p;
        });
    }
相关内容推荐
资源求助发帖
查看更多发帖

*

回帖描述:

*

链接类型:

*

下载链接:

密码:
发帖规则:回帖内容为会员之间的私信,普通网友无法查看。
免责声明:回帖中提供的链接内容仅供会员之间学习参考使用,获取内容后请在法律法规范围内使用。回帖提供的内容应符合法律法规要求,不得违反法律法律的要求。
站点权责:回帖内容如违反法律法规,站点有权封停账号使用权利。对用户举报的内容,站点有责任及时删除违规内容。
热点内容推荐
标题:html5 canvas拖动的流体图片动画效果

*

描述:
平均回复时间:3-10分钟
规则介绍:悬赏寻求论坛网友分享资源,站点对分享内容的准确性,合法性,版权等没有足够的监管能力。如果您发现资源不正确,无法使用,不符合法律法律等情况,您可以直接举报资源。站长将尽快核实您的举报,并根据情况,采取封号,退换米粒等处理。

*

回帖描述:

*

链接类型:

*

阅读权限:

*

下载链接:

密码:
发帖规则:回帖内容为会员之间的私信,普通网友无法查看。
免责声明:回帖中提供的链接内容仅供会员之间学习参考使用,获取内容后请在法律法规范围内使用。回帖提供的内容应符合法律法规要求,不得违反法律法律的要求。
站点权责:回帖内容如违反法律法规,站点有权封停账号使用权利。对用户举报的内容,站点有责任及时删除违规内容。
  • 背景波浪
  • 背景波浪
  • 波浪
  • 波浪
客服
在线咨询
周一 至 周日 9:00 ~ 22:00
QQ:1326974360
微信:juyoubuluo6688
客服热线
18205485173
工作日 9:00 ~ 18:00
微信扫码咨询
客户服务
欢迎咨询服务
咨询量较多时,请耐心等待
社群

关注公众号

获取更多资讯

扫码进群(QQ)

与更多大牛交流沟通

0.107121s