Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="myCanvas" width="500" height="500"></canvas>

              
            
!

CSS

              
                
              
            
!

JS

              
                let canvas = document.getElementById("myCanvas");
    let ctx = canvas.getContext("2d");

    const random = (start, end) => {
        return Math.floor(Math.random() * end + start)
    }
    const randomColor = () => {
        const start = 1
        const end = 255
        return [
            random(start, end),
            random(start, end),
            random(start, end),
        ]

    }

    function easeInOutExpo(e, a, g, f) {
        return g * (-Math.pow(2, -10 * e / f) + 1) + a
    }

    const pos = {x: 50, y: 50, w: 100, h: 100}

    const originX = pos.x + pos.w / 2
    const originY = pos.y + pos.h + 200

    let particles = []
    const frameTime = 16.66 // 假设一帧为16.66ms
    const reductionFactor = 5
    for (let x = 0; x < pos.w; x += reductionFactor) {
        for (let y = 0; y < pos.h; y += reductionFactor) {
            let particle = {
                // 初始位置
                x0: originX,
                y0: originY,
                // 终止位置
                x1: x + pos.x,
                y1: y + pos.y,
                // 样式属性
                rgbaColorArr: randomColor(),
                currTime: 0, // 粒子已经运行的时间
                duration: 3000,
                delay: y / 20 * frameTime, // 延迟帧数
                interval: parseInt(Math.random() * 10 * frameTime), // 每个动画的间隔
            };
            particles.push(particle);
        }
    }

    let requestId

    function renderParticles(t) {
        // 最后一个粒子
        const last = particles[particles.length - 1]
        if (last.duration < last.currTime) {
            cancelAnimationFrame(requestId)
            return
        }
        ctx.clearRect(0, 0, canvas.width, canvas.height);

        for (let p of particles) {
            if (p.currTime < p.delay) {
                p.currTime = t
                continue
            }

            const {duration, currTime, interval} = p
            ctx.fillStyle = `rgba(${p.rgbaColorArr.join(',')})`
            if (currTime < duration + interval) {
                if (currTime >= interval) {
                    let x = easeInOutExpo(currTime - interval, p.x0, p.x1 - p.x0, duration)
                    let y = easeInOutExpo(currTime - interval, p.y0, p.y1 - p.y0, duration)
                    ctx.fillRect(x, y, 1, 1)
                }
            } else {
                ctx.fillRect(p.x1, p.y1, 1, 1)
            }
            p.currTime = t
        }

        requestId = requestAnimationFrame(renderParticles)
    }

    const animate = () => {
        requestId = requestAnimationFrame(renderParticles)
    }

    animate()
              
            
!
999px

Console