canvas画出闪瞎眼的简单图形

请添加图片描述
请添加图片描述
当大佬给发这两张图的时候,啊,我的眼睛!定睛一看都要看瞎了好么。

再看跟随的话语,能筛掉一多半来面试前端的人?

好家伙还是一道前端面试题么。

nonono,这不是div堆叠做的,这么多div你想累死浏览器啊。

这个显然是用canvas api画出来的

贴一下canvas中文文档地址

我们都知道,图片绚丽,颜色各不相同,但是实际上,对计算机来说,只是一堆数据罢了。都是一个一个字节,一个一个bit。

计算机可能也能感受到这些数据的美,这谁能知道呢。

图片,不是位图的话,都是一个一个像素点。位图就是类似svg这种,不会因为缩放而失真的图片。

一个像素点由4个字节,分别表示R,G,B,A,各个字节所代表的的意思,如下图。

在这里插入图片描述
虽然是一堆字节,但是也是有排列顺序的。

按照从上到下行的顺序,行内是从左到右的列的顺序。

这样排布下来的,这就是图片在计算机里的真相。

所以,我们就去造出这个真相不就能造出一副图片来了么?

<!DOCTYPE HTML>
<html>
<body>
<canvas id="canvas" height=256 width=256></canvas>
<script>
// 绘制在Canvas上
var context = canvas.getContext('2d');
var imagedata = context.createImageData(256, 256);
// 给对应坐标位置的数据设置色值黑色
for (var x = 0; x < 256; x+=4) {
    for (var y = 0; y <= 256; y+= 4) {
        var index = 4 * (y*256 + x);
        var index1 = 4 * ((y+1)*256 + x);
        var index2 = 4 * (y*256 + (x+1));
        var index3 = 4 * ((y+1)*256 + (x+1));
        // 变为黑色,色值依次是0, 0, 0, 256
        imagedata.data[index] = 0;
        imagedata.data[index + 1] = 0;
        imagedata.data[index + 2] = 0;
        imagedata.data[index + 3] = 255;
        imagedata.data[index1] = 0;
        imagedata.data[index1 + 1] = 0;
        imagedata.data[index1 + 2] = 0;
        imagedata.data[index1 + 3] = 255;
        imagedata.data[index2] = 0;
        imagedata.data[index2 + 1] = 0;
        imagedata.data[index2 + 2] = 0;
        imagedata.data[index2 + 3] = 255;
        imagedata.data[index3] = 0;
        imagedata.data[index3 + 1] = 0;
        imagedata.data[index3 + 2] = 0;
        imagedata.data[index3 + 3] = 255;
    }
}
// 再重绘
context.putImageData(imagedata, 0, 0);
</script>

</body>
</html>

在这里插入图片描述
上述代码,画的是要求里的第三幅图片,也是 最基本的一张图片,因为黑色像素块的边长如果只为1的大小的话,也太小了,所以设置了2为边长,跳跃就是2*2=4

context.createImageData(256, 256);

这个就是canvas api造出的真相,也是原始的图片字节数组。

我们只要修改这个字节数组为我们想要的值,就能造出一幅图。

默认的真相,是白纸一张,并且是透明的,因为数组默认值都为0。

<!DOCTYPE HTML>
<html>
<body>
<canvas id="canvas" height=256 width=256></canvas>
<script>
// 绘制在Canvas上
var context = canvas.getContext('2d');
var imagedata = context.createImageData(256, 256);
// 给对应坐标位置的数据设置色值黑色
for (var x = 0; x < 256; x+=1) {
    for (var y = 0; y <= 256; y+= 1) {
    	if(x<=y){
        	var index = 4 * (y*256 + x);
          // 变为黑色,色值依次是0, 0, 0, 256
          imagedata.data[index] = 0;
          imagedata.data[index + 1] = 0;
          imagedata.data[index + 2] = 0;
          imagedata.data[index + 3] = 255;
        }
        
    }
}
// 再重绘
context.putImageData(imagedata, 0, 0);
</script>

</body>
</html>

在这里插入图片描述
这个对应要求的第二张图咯。

<!DOCTYPE HTML>
<html>
<body>
<canvas id="canvas" height=256 width=256></canvas>
<script>
// 绘制在Canvas上
var context = canvas.getContext('2d');
var imagedata = context.createImageData(256, 256);
// 给对应坐标位置的数据设置色值黑色
for (var x = 0; x < 256; x+=4) {
    for (var y = 0; y <= 256; y+= 4) {
    	var index = 4 * (y*256 + x);
        var index1 = 4 * ((y+1)*256 + x);
          var index2 = 4 * ((y+1)*256 + x);
          var index3 = 4 * (y*256 + (x+1));
          // 变为黑色,色值依次是0, 0, 0, 256
          imagedata.data[index] = 0;
          imagedata.data[index + 1] = 0;
          imagedata.data[index + 2] = 0;
          imagedata.data[index + 3] = 255;
        	imagedata.data[index1] = 0;
          imagedata.data[index1 + 1] = 0;
          imagedata.data[index1 + 2] = 0;
          imagedata.data[index1 + 3] = 255;
          imagedata.data[index2] = 0;
          imagedata.data[index2 + 1] = 0;
          imagedata.data[index2 + 2] = 0;
          imagedata.data[index2 + 3] = 255;
          imagedata.data[index3] = 0;
          imagedata.data[index3 + 1] = 0;
          imagedata.data[index3 + 2] = 0;
          imagedata.data[index3+ 3] = 255;
          var index4 = 4 * ((y+2)*256 + (x+2));
        var index5 = 4 * ((y+3)*256 + (x+2));
          var index6 = 4 * ((y+3)*256 + (x+2));
          var index7 = 4 * ((y+2)*256 + (x+3));
          imagedata.data[index4] = 0;
          imagedata.data[index4 + 1] = 0;
          imagedata.data[index4 + 2] = 0;
          imagedata.data[index4 + 3] = 255;
          imagedata.data[index5] = 0;
          imagedata.data[index5 + 1] = 0;
          imagedata.data[index5 + 2] = 0;
          imagedata.data[index5 + 3] = 255;
          imagedata.data[index6] = 0;
          imagedata.data[index6 + 1] = 0;
          imagedata.data[index6 + 2] = 0;
          imagedata.data[index6 + 3] = 255;
          imagedata.data[index7] = 0;
          imagedata.data[index7 + 1] = 0;
          imagedata.data[index7 + 2] = 0;
          imagedata.data[index7 + 3] = 255;
          
    }
}
// 再重绘
context.putImageData(imagedata, 0, 0);
</script>

</body>
</html>

在这里插入图片描述
第四张图

第一张看不清了,就酱吧。

还是蛮有意思的。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的例子,用canvas一个杯子和水蒸汽的图形: ```html <canvas id="myCanvas" width="400" height="400"></canvas> ``` ```javascript const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); // 绘制杯子 ctx.beginPath(); ctx.moveTo(100, 200); ctx.lineTo(300, 200); ctx.quadraticCurveTo(320, 200, 320, 220); ctx.lineTo(320, 320); ctx.quadraticCurveTo(320, 340, 300, 340); ctx.lineTo(100, 340); ctx.quadraticCurveTo(80, 340, 80, 320); ctx.lineTo(80, 220); ctx.quadraticCurveTo(80, 200, 100, 200); ctx.fillStyle = "#FFDAB9"; ctx.fill(); // 绘制水蒸汽 ctx.beginPath(); ctx.moveTo(150, 150); ctx.quadraticCurveTo(200, 50, 250, 150); ctx.quadraticCurveTo(200, 120, 150, 150); ctx.fillStyle = "#FFFFFF"; ctx.globalAlpha = 0.5; ctx.fill(); ``` 这个例子中,我们首先用 `beginPath()` 开始绘制杯子的路径,然后用 `moveTo()` 和 `lineTo()` 方法绘制杯子的形状,最后用 `quadraticCurveTo()` 方法添加弧度,使其更加接近真实的杯子形状。我们使用 `fillStyle` 属性来设置杯子的颜色,并使用 `fill()` 方法填充区域。 接下来,我们用 `beginPath()` 方法开始绘制水蒸汽的路径,用 `moveTo()` 和 `quadraticCurveTo()` 方法绘制云朵的形状。我们使用 `fillStyle` 属性来设置颜色,并使用 `globalAlpha` 属性来设置透明度,使水蒸汽看起来更加真实。最后,我们使用 `fill()` 方法来填充区域。 这只是一个简单的例子,你可以根据需要自由发挥,调整颜色、大小和形状,以创建你自己的杯子和水蒸汽图形

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rgbhi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值