• 【canvas】前端创造的图片粒子动画效果:HTML5 Canvas 技术详解


    前端创造的图片粒子动画效果:HTML5 Canvas 技术详解

    我们将深入探讨如何通过 HTML5 的 Canvas 功能,将上传的图片转换成引人入胜的粒子动画效果。这种效果将图片分解成小粒子,并在用户与它们交互时产生动态变化。我们将分步骤详细解析代码,让你能够理解每一行代码的作用,并自己实现这一效果。
    在这里插入图片描述

    环境准备

    首先,你需要一个简单的 HTML 元素和一些样式设置

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Particle Image Animation from Uploaded Imagetitle>
        <style>
            body {
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
                margin: 0;
                background-color: #f0f0f0;
                overflow: hidden;
            }
            canvas, input {
                display: block;
                margin: auto;
            }
        style>
    head>
    <body>
        <input type="file" id="upload" accept="image/*">
        <canvas id="canvas" hidden>canvas>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    这段 HTML 设置了一个文件输入控件供用户上传图片,以及一个 Canvas 元素用于渲染动画效果。样式使页面内容居中显示,并将背景设置为浅灰色。

    JavaScript 部分

    JavaScript 脚本是这个效果的核心,下面我们逐一解析每个部分的功能。

    1. 初始化和载入图片:
    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');
    let particles = [];
    const numOfParticles = 5000;
    const uploadInput = document.getElementById('upload');
    
    uploadInput.addEventListener('change', function(event) {
        const file = event.target.files[0];
        if (file && file.type.startsWith('image')) {
            const reader = new FileReader();
            reader.onload = function(e) {
                const maxSize = 500; // 最大尺寸
                            let width = img.width;
                            let height = img.height;
                            let scale = Math.min(maxSize / width, maxSize / height);
                            if (scale < 1) {
                                width *= scale;
                                height *= scale;
                            }
                            canvas.width = width;
                            canvas.height = height;
                            ctx.drawImage(img, 0, 0, width, height);
                            canvas.hidden = false;
                            const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
                            createParticles(imageData);
                            animate();
                };
            };
            reader.readAsDataURL(file);
        }
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    在这部分代码中,我们首先获取 Canvas 元素并配置基本画布(context)。监听文件输入控件的变化事件,当用户选择一个图片文件时,使用 FileReader 对象读取文件内容,将其转换为 Base64 编码的 URL,然后载入 元素。图片载入完毕后,把它绘制到 Canvas 上,然后提取图片的像素数据。

    2. 创建粒子:
    function createParticles(imageData) {
        particles = [];
        const { width, height } = imageData;
        for (let i = 0; i < numOfParticles; i++) {
            const x = Math.random() * width;
            const y = Math.random() * height;
            const color = imageData.data[(~~y * width + ~~x) * 4];
            particles.push(new Particle(x, y, color));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    这个函数根据图片的像素数据随机生成指定数量的粒子。每个粒子具有位置(x,y)和基于图片某一点的颜色。粒子的初始位置是随机分布的。

    3. 定义粒子对象:
    function Particle(x, y, color) {
        this.x = x;
        this.originalX = x;
        this.y = y;
        this.originalY = y;
        this.color = `rgba(${color},${color},${color}, 0.5)`;
    
        this.draw = function() {
            ctx.fillStyle = this.color;
            ctx.fillRect(this.x, this.y, 2, 2);
        };
    
        this.update = function() {
            let dx = this.originalX - this.x;
            let dy = this.originalY - this.y;
            this.x += dx * 0.1;
            this.y += dy * 0.1;
    
            this.draw();
        };
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    粒子对象具有 drawupdate 方法。draw 方法用来在 Canvas 上绘制粒子,update 方法则负责更新粒子的位置,使它们逐渐回到原始位置。

    4. 动画循环和鼠标交互:
    function animate() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        particles.forEach(particle => particle.update());
        requestAnimationFrame(animate);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    animate 函数清空画布并更新所有粒子的位置,然后通过 requestAnimationFrame 递归调用自身以形成动画循环。

    完整代码

    复制这段代码到一个.html文件,可以直接在浏览器允许该demo,实际操作一番。

    DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>Particle Image Animation from Uploaded Imagetitle>
        <style>
            body {
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
                margin: 0;
                background-color: #f0f0f0;
                overflow: hidden;
            }
    
            canvas,
            input {
                display: block;
                margin: auto;
            }
        style>
    head>
    
    <body>
        <input type="file" id="upload" accept="image/*">
        <canvas id="canvas" hidden>canvas>
        <script>
            const canvas = document.getElementById('canvas');
            const ctx = canvas.getContext('2d');
            let particles = [];
            const numOfParticles = 5000;
            const uploadInput = document.getElementById('upload');
    
            uploadInput.addEventListener('change', function (event) {
                const file = event.target.files[0];
                if (file && file.type.startsWith('image')) {
                    const reader = new FileReader();
                    reader.onload = function (e) {
                        const img = new Image();
                        img.src = e.target.result;
                        img.onload = function () {
                            const maxSize = 500; // 最大尺寸
                            let width = img.width;
                            let height = img.height;
                            let scale = Math.min(maxSize / width, maxSize / height);
                            if (scale < 1) {
                                width *= scale;
                                height *= scale;
                            }
                            canvas.width = width;
                            canvas.height = height;
                            ctx.drawImage(img, 0, 0, width, height);
                            canvas.hidden = false;
                            const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
                            createParticles(imageData);
                            animate();
                        };
                    };
                    reader.readAsDataURL(file);
                }
            });
    
            function createParticles(imageData) {
                particles = [];
                const { width, height } = imageData;
                for (let i = 0; i < numOfParticles; i++) {
                    const x = Math.random() * width;
                    const y = Math.random() * height;
                    const color = imageData.data[(~~y * width + ~~x) * 4];
                    particles.push(new Particle(x, y, color));
                }
            }
    
            function Particle(x, y, color) {
                this.x = x;
                this.originalX = x;
                this.y = y;
                this.originalY = y;
                this.color = `rgba(${color},${color},${color}, 0.5)`;
    
                this.draw = function () {
                    ctx.fillStyle = this.color;
                    ctx.fillRect(this.x, this.y, 2, 2);
                };
    
                this.update = function () {
                    let dx = this.originalX - this.x;
                    let dy = this.originalY - this.y;
                    this.x += dx * 0.1;
                    this.y += dy * 0.1;
    
                    this.draw();
                };
            }
    
            function animate() {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                particles.forEach(particle => particle.update());
                requestAnimationFrame(animate);
            }
    
            canvas.addEventListener('mousemove', function (e) {
                const rect = canvas.getBoundingClientRect();
                const mouseX = e.clientX - rect.left;
                const mouseY = e.clientY - rect.top;
    
                particles.forEach(particle => {
                    const dx = mouseX - particle.x;
                    const dy = mouseY - particle.y;
                    const dist = Math.sqrt(dx * dx + dy * dy);
    
                    if (dist < 50) {
                        const angle = Math.atan2(dy, dx);
                        particle.x -= Math.cos(angle);
                        particle.y -= Math.sin(angle);
                    }
                });
            });
        script>
    body>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
  • 相关阅读:
    科锐国际(计算机类),快手,CVTE,得物,蓝月亮,蓝禾,奇安信,顺丰,康冠科技,金证科技24春招内推
    自动驾驶与车路协同
    Pytorch实现线性回归
    进程-线程-协程
    排序算法总结-C语言
    企业能源管控平台在工业能效提升行动中的作用
    vue3 Element Plus 基于webstorm练习
    SpringBoot
    Java中的多线程(线程,进程,线程状态的方法,并发,并行,线程调度)
    LeetCode刷题(1)
  • 原文地址:https://blog.csdn.net/qq_41883423/article/details/138195841