您当前的位置: 首页 >  html

彭世瑜

暂无认证

  • 2浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

html:canvas画布绘图简单入门-2

彭世瑜 发布时间:2021-12-31 22:40:45 ,浏览量:2

canvas示例系列:

  • html:canvas画布绘图简单入门-1
  • html:canvas画布绘图简单入门-2
  • html:canvas画布绘图简单入门-绘制时钟-3
  • html:canvas画布绘图简单入门-刮刮乐-4
分辨率和元素尺寸


内容分辨率: 600 * 700 元素尺寸: 800 * 900

示例6:将元素截取后返回画布

像素操作 getImageData() createImageData() putImageData()

在这里插入图片描述




    let canvas = document.querySelector('#canvas');
    let ctx = canvas.getContext('2d');

    ctx.fillStyle = "red";
    ctx.fillRect(0, 0, 50, 50);
    
    let imageData = ctx.getImageData(0, 0, 50, 50);
    console.log(imageData);

    // 转换元素的颜色数组 RGBA  [红色, 绿色, 蓝色, alpha通道]
    // [255, 0, 0, 255]  => [0, 255, 255, 255]
    for (let i = 0; i

    
        let canvas = document.querySelector('#canvas');
        let ctx = canvas.getContext('2d');

        ctx.fillStyle = "red";
        ctx.fillRect(0, 0, 50, 50);

        // 移动原点坐标(0, 0) => (100, 0)
        ctx.translate(100, 0);
        ctx.fillStyle = "green";
        ctx.fillRect(0, 0, 50, 50);
    
示例8:旋转坐标轴

在这里插入图片描述



    
        let canvas = document.querySelector('#canvas');
        let ctx = canvas.getContext('2d');

        // 将坐标轴顺时针旋转45角
        ctx.rotate(Math.PI / 180 * 45);

        ctx.fillStyle = "red";
        ctx.fillRect(25, -25, 50, 50);
    
示例9:缩放坐标轴

在这里插入图片描述



    
        let canvas = document.querySelector('#canvas');
        let ctx = canvas.getContext('2d');

        ctx.fillStyle = "red";
        ctx.fillRect(0, 0, 50, 50);

        // 保存画笔状态
        ctx.save()
        // 坐标轴x, y都放大两倍
        ctx.scale(2, 2);
        ctx.fillStyle = "green";
        ctx.fillRect(50, 0, 50, 50);
        
        // 恢复画笔状态
        ctx.restore()
        ctx.fillRect(250, 0, 50, 50);
    
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0629s