canvas画布,初次使用

这篇博客介绍了作者对canvas画布的初次尝试,通过创建两个趣味动画DEMO展示了canvas的魅力。一个是实现了一个带反弹效果的自由落体动画,另一个则是绘制了奔跑的火柴人动画,深入浅出地展示了canvas的基本用法和动画原理。
摘要由CSDN通过智能技术生成

突然对canvas画布有了兴趣写两个小动画demo,

一个是自由落体动画,带反弹的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Canvas</title>
</head>
<style>
    .tu {
        width: 800px;
        height: 800px;
    }
</style>
<body>
<div class="tu">
    <canvas id="canvas" style="width: 100%;height: 100%;" width="900" height="900"></canvas>
</div>
</body>
<script>
    let el = document.getElementById("canvas");
    let cxt = el.getContext("2d");
    let time = 0;
    let dian = [];
    setInterval(getD, 10)
    function getD() {
        // let ct = new Date().getTime();
        time++;
        if (Math.random() * 50 < 3) {
            dian.push({
                x: Math.floor(Math.random() * 850 + 25),
                time: time,
                d: Math.floor(Math.random() * 10 + 6),
                w: 0,
                v: 0,
                fx: true,
                ft: Math.random()*0.7 + 1.2
            })
        }
        cxt.clearRect(0, 0, 900, 900)
        let nw = []
        dian.forEach(d => {
            d.v = (d.v + (d.fx ? 0.08 : -0.08));
            if (d.v < 0) {
                d.v = 0
                d.fx = true;
                // d.w =0;
            } else {
                let tw = d.v + (d.fx ? -0.04 : 0.04);
                d.w = d.w + (d.fx ? tw : -tw);
                if (d.w > (900 - d.d)) {
                    d.w = (900 - d.d);
                    d.v = d.v / d.ft
                    d.fx = false;
                }
            }
            if (d.v === 0 && d.w > (900 - d.d)-1) {
                return
            }
            nw.push(d)
        })
        cxt.fillStyle = "#ad7c7c";
        nw.forEach(d => {
            cxt.beginPath();
            // cxt.fillRect(d.x, g[time - d.time], d.d, d.d);
            cxt.arc(d.x + d.d / 2, d.w + d.d / 2, d.d / 2, 0, Math.PI * 2, true)
            cxt.closePath();
            cxt.fill()
        })
        dian = nw;
    }
</script>
</html>

另一个是奔跑的火柴人

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hcr</title>
    <style>
        .tu {
            width: 800px;
            height: 800px;
        }
    </style>
</head>
<body>
<div class="tu">
    <canvas id="canvas" style="width: 100%;height: 100%;" width="900" height="900"></canvas>
</div>
</body>
<script>
    let el = document.getElementById("canvas");
    let cxt = el.getContext("2d");
    let time = 0;
    setInterval(getD, 10)
    // getD()
    function getD() {
        cxt.clearRect(0, 0, 900, 900)
        time++;
        // let ct = new Date().getTime();
        cxt.fillStyle = "#ad7c7c";
        // cxt.beginPath();
        // cxt.arc(100,100,20, 0, Math.PI * 2, true)
        // cxt.closePath();
        // cxt.fill()
        cxt.beginPath()
        cxt.arc(300,300,30, 0, Math.PI*2 , true)
        cxt.fill()
        cxt.beginPath()
        let c = Math.sqrt(300*300 + 330*330)
        let d = Math.asin(330/c);

        let xc = Math.sqrt(286*286 + 450*450)
        let xd = Math.asin(450/xc);

        let temp = time%50;
        let jd = 0
        if (temp <=25){
            jd = temp
        }
        if (temp >25){
            jd = 50 - temp
        }
        jd = jd *3

        cxt.rotate(jd*Math.PI/180);
        let number = Math.sin(d -  jd*Math.PI/180);
        let cn = Math.cos(d - jd*Math.PI/180);
        sou(c*cn,c*number)
        jiao(xc* Math.cos(xd - jd*Math.PI/180)+14,xc* Math.sin(xd -  jd*Math.PI/180)-150)
        cxt.rotate(((360 - 2*jd)*Math.PI/180))
        sou(c* Math.cos(d + jd*Math.PI/180),c* Math.sin(d +  jd*Math.PI/180))
        jiao(xc* Math.cos(xd + jd*Math.PI/180)+14,xc* Math.sin(xd +  jd*Math.PI/180)-150)
        cxt.rotate(jd*Math.PI/180);
        // sou(3jd,3jd)
        cxt.closePath()
        shen(300,300)
        // cxt.stroke()
        cxt.fill()
    }

    function sou(x,y) {
        // cxt.beginPath();
        cxt.moveTo(x-10,y+10)
        cxt.lineTo(x+10,y+10)
        cxt.lineTo(x+10,y+80)
        cxt.lineTo(x+40,y+90)
        cxt.lineTo(x+40,y+105)
        cxt.lineTo(x-10,y+90)
        cxt.closePath()
    }

    function shen(x,y) {
        // cxt.beginPath();
        cxt.moveTo(x-14,y+20)
        cxt.lineTo(x+14,y+20)
        cxt.lineTo(x,y+150)
        cxt.lineTo(x-28,y+150)
        cxt.closePath()
    }

    function jiao(x,y) {
        // cxt.beginPath();
        cxt.moveTo(x-28,y+150)
        cxt.lineTo(x,y+150)
        cxt.lineTo(x,y+220)
        cxt.lineTo(x-30,y+270)
        cxt.lineTo(x-58,y+270)
        cxt.lineTo(x-28,y+220)
        cxt.closePath()
    }


</script>
</html>

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
HTML5 的 `<canvas>` 元素允许我们通过脚本来绘制图形到网页上。要将一张图片插入到 `<canvas>` 中,通常需要先加载该图片,然后将其转换为适合绘图的数据格式,最后再使用 `drawImage` 方法绘制到画布上。 以下是详细步骤: 1. **加载图片**: 使用 `fetch` 或者 `XMLHttpRequest` 来异步从服务器或其他来源获取图片文件。这里使用 `fetch` 为例: ```javascript fetch('path/to/your/image.jpg') .then(response => response.blob()) .then(imageBlob => { // 创建图像元素并设置 blob 为源数据 let img = new Image(); img.src = URL.createObjectURL(imageBlob); }); ``` 2. **绘制图片**: 等待图片加载完成后,我们就可以将它绘制到 `<canvas>` 上了: ```javascript function drawImageOnCanvas(canvas) { if (window.URL && window.URL.createObjectURL) { // 图像加载完成之后,使用 createObjectURL 将 Blob 转换为可处理的 URL let img = new Image(); img.onload = () => { // 绘制图片至画布上 let ctx = canvas.getContext('2d'); ctx.drawImage(img, x, y, width, height); // 这里的坐标和大小取决于你想如何放置图片 // 清理资源 URL.revokeObjectURL(img.src); }; img.src = URL.createObjectURL(imageBlob); } else { console.error("Failed to create an object URL for the image."); } } // 找到 canvas 元素并绘制图片 let canvasElement = document.getElementById('myCanvas'); drawImageOnCanvas(canvasElement); ``` 这个示例展示了如何将图片从服务器加载并在 `<canvas>` 上绘制。注意,你需要替换 `'path/to/your/image.jpg'` 为你实际使用的图片链接,并根据需要调整 `x`, `y`, `width`, 和 `height` 参数来控制图片的位置和尺寸。 --- ### 相关问题: 1. **如何优化图片加载速度**? - 使用合适的内容类型和编码压缩图片。 - 利用懒加载策略仅在需要时加载图片。 - 预加载关键的图片资源以提高首次加载的速度。 2. **如何自适应不同屏幕大小下的图片显示**? - 根据设备的屏幕宽度自动调整图片的大小。 - 动态计算图片的缩放比例以维持最佳的清晰度和比例展示。 3. **如何处理跨域请求的问题**? - 确保服务器配置正确,允许跨域资源共享(CORS)。 - 使用 JSONP 等机制绕过同源策略限制。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值